Documentation
¶
Overview ¶
Package snake provides functions for parsing information in a HTML head tag.
The snake.ProcessURL function takes a url string and a *http.Client, makes a GET request to the url using the http client and returns a new *snake.Object from the returned HTML. If client is nil, a default client is used.
object, err := snake.ProcessURL("http://ogp.me", nil)
The snake.ParseHTML function takes an io.Reader, reads HTML, parses the HTML and returns a new *snake.Object.
resp, _ := http.Get("http://ogp.me") // ignoring the error and other response attributes (like status code) // for simplicity defer resp.Body.Close() object, err := snake.ParseHTML(resp.Body)
Index ¶
- func DefaultGetHTML(r *http.Request) (string, error)
- func DefaultGetURL(r *http.Request) (string, error)
- func DefaultWriteResponse(w http.ResponseWriter, obj *Object)
- type ContentTypeNotHTMLError
- type HTMLHandler
- type InvalidContentTypeError
- type InvalidStatusCodeError
- type Link
- type Object
- type URLHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultGetHTML ¶
DefaultGetHTML is used to extract the HTML from an HTTP request. It simply reads the HTTP request body and returns it.
func DefaultGetURL ¶
DefaultGetURL is used to extract the URL from an HTTP request. It first tries to read the `url` query paramter and then the HTTP request body. Whichever is present is used as the URL that needs to be processed.
func DefaultWriteResponse ¶
func DefaultWriteResponse(w http.ResponseWriter, obj *Object)
DefaultWriteResponse serializes *Object as JSON and writes it to w.
Types ¶
type ContentTypeNotHTMLError ¶
type ContentTypeNotHTMLError string
ContentTypeNotHTMLError is returned when the Content-Type HTTP response header indicates that the content is not HTML.
func (ContentTypeNotHTMLError) Error ¶
func (e ContentTypeNotHTMLError) Error() string
type HTMLHandler ¶
type HTMLHandler struct { // GetHTML is used to extract the HTML string from an HTTP request. If // GetHTML is nil, DefaultGetHTML is used. GetHTML func(r *http.Request) (string, error) // WriteResponse is used by HTMLHandler to produce an HTTP response. It // transforms and serializes *Object and writes it to w. If WriteResponse // is nil, DefaultWriteResponse is used. WriteResponse func(w http.ResponseWriter, obj *Object) }
HTMLHandler is an HTTP handler that responds with a serialized *Object after processing an HTML string's <head> tag. This HTML string is obtained from the HTTP request. It can provided as query parameter or in the HTTP body. GetHTML function determines how the HTML string is extracted from an HTTP request.
func (*HTMLHandler) ServeHTTP ¶
func (h *HTMLHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type InvalidContentTypeError ¶
type InvalidContentTypeError string
InvalidContentTypeError is returned when the Content-Type HTTP response header is invalid and cannot be parsed.
func (InvalidContentTypeError) Error ¶
func (e InvalidContentTypeError) Error() string
type InvalidStatusCodeError ¶
type InvalidStatusCodeError int
InvalidStatusCodeError is returned when the HTTP response status code is < 200 or >= 300. This is the final HTTP response after the http.Client has followed the redirect policy.
func (InvalidStatusCodeError) Error ¶
func (e InvalidStatusCodeError) Error() string
type Object ¶
type Object struct { Title string `json:"title"` Base string `json:"base"` Charset string `json:"charset"` Links map[string][]*Link `json:"links"` Metas map[string]string `json:"metas"` }
Object represents parsed HTML elements inside the <head> tag.
type URLHandler ¶
type URLHandler struct { // Client is the *http.Client used to make HTTP requests. Client *http.Client // GetURL is used to extract the URL from an HTTP request. If GetURL is // nil, DefaultGetURL is used. GetURL func(r *http.Request) (string, error) // WriteResponse is used by URLHandler to produce an HTTP response. It // transforms and serializes *Object and writes it to w. If WriteResponse // is nil, DefaultWriteResponse is used. WriteResponse func(w http.ResponseWriter, obj *Object) }
URLHandler is an HTTP handler that responds with a serialized *Object after processing a URL's <head> tag. This URL is obtained from the HTTP request. It can provided as query parameter or in the HTTP body. GetURL function determines how the URL is extracted from an HTTP request.
func (*URLHandler) ServeHTTP ¶
func (h *URLHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)