mirror of
https://github.com/kjanat/articulate-parser.git
synced 2026-01-16 09:42:09 +01:00
refactor(core)!: Add context, config, and structured logging
Introduces `context.Context` to the `FetchCourse` method and its call chain, allowing for cancellable network requests and timeouts. This improves application robustness when fetching remote course data. A new configuration package centralizes application settings, loading them from environment variables with sensible defaults for base URL, request timeout, and logging. Standard `log` and `fmt` calls are replaced with a structured logging system built on `slog`, supporting both JSON and human-readable text formats. This change also includes: - Extensive benchmarks and example tests. - Simplified Go doc comments across several packages. BREAKING CHANGE: The `NewArticulateParser` constructor signature has been updated to accept a logger, base URL, and timeout, which are now supplied via the new configuration system.
This commit is contained in:
96
internal/services/example_test.go
Normal file
96
internal/services/example_test.go
Normal file
@ -0,0 +1,96 @@
|
||||
// Package services_test provides examples for the services package.
|
||||
package services_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/kjanat/articulate-parser/internal/services"
|
||||
)
|
||||
|
||||
// ExampleNewArticulateParser demonstrates creating a new parser.
|
||||
func ExampleNewArticulateParser() {
|
||||
// Create a no-op logger for this example
|
||||
logger := services.NewNoOpLogger()
|
||||
|
||||
// Create parser with defaults
|
||||
parser := services.NewArticulateParser(logger, "", 0)
|
||||
|
||||
fmt.Printf("Parser created: %T\n", parser)
|
||||
// Output: Parser created: *services.ArticulateParser
|
||||
}
|
||||
|
||||
// ExampleNewArticulateParser_custom demonstrates creating a parser with custom configuration.
|
||||
func ExampleNewArticulateParser_custom() {
|
||||
logger := services.NewNoOpLogger()
|
||||
|
||||
// Create parser with custom base URL and timeout
|
||||
parser := services.NewArticulateParser(
|
||||
logger,
|
||||
"https://custom.articulate.com",
|
||||
60_000_000_000, // 60 seconds in nanoseconds
|
||||
)
|
||||
|
||||
fmt.Printf("Parser configured: %T\n", parser)
|
||||
// Output: Parser configured: *services.ArticulateParser
|
||||
}
|
||||
|
||||
// ExampleArticulateParser_LoadCourseFromFile demonstrates loading a course from a file.
|
||||
func ExampleArticulateParser_LoadCourseFromFile() {
|
||||
logger := services.NewNoOpLogger()
|
||||
parser := services.NewArticulateParser(logger, "", 0)
|
||||
|
||||
// In a real scenario, you'd have an actual file
|
||||
// This example shows the API usage
|
||||
_, err := parser.LoadCourseFromFile("course.json")
|
||||
if err != nil {
|
||||
log.Printf("Failed to load course: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// ExampleArticulateParser_FetchCourse demonstrates fetching a course from a URI.
|
||||
func ExampleArticulateParser_FetchCourse() {
|
||||
logger := services.NewNoOpLogger()
|
||||
parser := services.NewArticulateParser(logger, "", 0)
|
||||
|
||||
// Create a context with timeout
|
||||
ctx := context.Background()
|
||||
|
||||
// In a real scenario, you'd use an actual share URL
|
||||
_, err := parser.FetchCourse(ctx, "https://rise.articulate.com/share/YOUR_SHARE_ID")
|
||||
if err != nil {
|
||||
log.Printf("Failed to fetch course: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// ExampleHTMLCleaner demonstrates cleaning HTML content.
|
||||
func ExampleHTMLCleaner() {
|
||||
cleaner := services.NewHTMLCleaner()
|
||||
|
||||
html := "<p>This is <strong>bold</strong> text with entities.</p>"
|
||||
clean := cleaner.CleanHTML(html)
|
||||
|
||||
fmt.Println(clean)
|
||||
// Output: This is bold text with entities.
|
||||
}
|
||||
|
||||
// ExampleHTMLCleaner_CleanHTML demonstrates complex HTML cleaning.
|
||||
func ExampleHTMLCleaner_CleanHTML() {
|
||||
cleaner := services.NewHTMLCleaner()
|
||||
|
||||
html := `
|
||||
<div>
|
||||
<h1>Title</h1>
|
||||
<p>Paragraph with <a href="#">link</a> and & entity.</p>
|
||||
<ul>
|
||||
<li>Item 1</li>
|
||||
<li>Item 2</li>
|
||||
</ul>
|
||||
</div>
|
||||
`
|
||||
clean := cleaner.CleanHTML(html)
|
||||
|
||||
fmt.Println(clean)
|
||||
// Output: Title Paragraph with link and & entity. Item 1 Item 2
|
||||
}
|
||||
Reference in New Issue
Block a user