Skip to content

Commit 2c9c5a9

Browse files
committed
More docs
1 parent b5c7bc2 commit 2c9c5a9

File tree

9 files changed

+49
-29
lines changed

9 files changed

+49
-29
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,7 @@ docs-clean:
6767

6868
.PHONY: docs-serve
6969
docs-serve:
70-
cd docs-src && hugo server -D
70+
cd docs-src && hugo server -D
71+
72+
godoc-serve:
73+
godoc -http=":9090"

const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const (
66
// DefaultServerAddress is the default hostname where the server is going to
77
// be started and listening.
88
DefaultServerAddress = "localhost"
9+
910
// DefaultServerPort is the default port number where the server is going to
1011
// be started and listening.
1112
DefaultServerPort = "8080"

doc.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
// Copyright 2017 The qurl Authors. All rights reserved.
22

3-
package qurl
3+
/*
4+
Package qurl implements a service to extract the contents of any web page
5+
returning the results as a JSON data structure.
6+
7+
This service exports all its functionality through an HTTP REST API and has
8+
been designed following the principles on microservices so it can be easily
9+
integrated in any existing infrastructure or platform.
10+
11+
In adition of this API reference, there is available also an accurated
12+
documentation for end users and developers in the following URL:
13+
14+
https://repejota.github.com/qurl/
15+
*/
16+
package qurl // import "github.com/repejota/qurl"

qurl.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,34 @@ func (q *QURL) SetURL(u string) error {
3434
return err
3535
}
3636
q.URL = u
37+
q.Request.URL = q.URL
3738
q.Response.URL = q.URL
3839
return nil
3940
}
4041

4142
// Query queries the URL and process all the data we want to fetch.
42-
func (q *QURL) Query(queryParams url.Values) (*Response, error) {
43+
func (q *QURL) Query(queryParams url.Values) error {
4344
// Fetch URL content
44-
statuscode, headers, body, err := q.Request.Fetch(q.URL)
45+
statuscode, headers, body, err := q.Request.Fetch()
4546
if err != nil {
4647
q.Response.Status = statuscode
47-
return q.Response, err
48+
return err
4849
}
4950

5051
err = q.processHeaders(queryParams, *headers)
5152
if err != nil {
5253
q.Response.Status = statuscode
53-
return q.Response, err
54+
return err
5455
}
5556

5657
err = q.processSelectors(queryParams, bytes.NewReader(body))
5758
if err != nil {
5859
q.Response.Status = statuscode
59-
return q.Response, err
60+
return err
6061
}
6162

6263
q.Response.Status = statuscode
63-
return q.Response, nil
64+
return nil
6465
}
6566

6667
// processHeaders ...

request.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import (
77
"net/http"
88
)
99

10-
// Request ...
10+
// Request represents the call being made to retrieve the contents of an URL.
1111
type Request struct {
12+
URL string `json:"url"`
1213
}
1314

14-
// NewRequest ...
15+
// NewRequest returns a new request instance.
1516
func NewRequest() *Request {
1617
r := Request{}
1718
return &r
1819
}
1920

20-
// Fetch ...
21-
func (r *Request) Fetch(url string) (int, *http.Header, []byte, error) {
22-
resp, err := http.Get(url)
21+
// Fetch performs an HTTP GET call to anURL and fetch the contents.
22+
func (r *Request) Fetch() (int, *http.Header, []byte, error) {
23+
resp, err := http.Get(r.URL)
2324
if err != nil {
2425
return http.StatusInternalServerError, nil, nil, err
2526
}

request_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ func TestNewRequest(t *testing.T) {
4141

4242
func TestFetch(t *testing.T) {
4343
request := NewRequest()
44-
status, _, _, err := request.Fetch("http://localhost:7070/")
44+
request.URL = "http://localhost:7070/"
45+
status, _, _, err := request.Fetch()
4546
if err != nil {
4647
t.Fatal(err)
4748
}
@@ -52,7 +53,8 @@ func TestFetch(t *testing.T) {
5253

5354
func TestFetchFail(t *testing.T) {
5455
request := NewRequest()
55-
status, _, _, _ := request.Fetch("http://invalidhost")
56+
request.URL = "http://invalidhost"
57+
status, _, _, _ := request.Fetch()
5658
if status != 500 {
5759
t.Fatalf("Fetch url expected to return code '500' but got %d", status)
5860
}

response.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,35 @@
22

33
package qurl
44

5-
// Attribute ...
5+
// Attribute represents a key, value pair of strrings fromn an HTML node
6+
// property.
67
type Attribute struct {
78
Key string `json:"key"`
89
Value string `json:"value"`
910
}
1011

11-
// Element represents an HTML element from a selector coincidence.
12+
// Element represents a simplified HTML element node. It only supports the node
13+
// name and a list of attributes.
1214
type Element struct {
1315
Text string `json:"text"`
1416
Attributes []*Attribute `json:"attributes"`
1517
}
1618

17-
// Response is the type that defines a query result.
19+
// Response represents the result struct received after querying an URL.
20+
// Contains information about the URL and the data retrieved after proessing
21+
// the content.
1822
type Response struct {
1923
URL string `json:"url"`
2024
Status int `json:"status"`
2125
Headers map[string][]string `json:"headers,omitempty"`
2226
Selectors map[string][]*Element `json:"selectors,omitempty"`
2327
}
2428

25-
// NewResponse returns a response instance.
29+
// NewResponse returns a new response instance.
2630
func NewResponse() *Response {
27-
r := Response{
31+
response := Response{
2832
Headers: make(map[string][]string),
2933
Selectors: make(map[string][]*Element),
3034
}
31-
return &r
35+
return &response
3236
}

routes/query.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ func Query(w http.ResponseWriter, r *http.Request) {
2222
return
2323
}
2424

25-
// Query the target URL and obtain data.
26-
response, err := qurl.Query(queryParams)
25+
// Query the target URL.
26+
err = qurl.Query(queryParams)
2727
if err != nil {
2828
http.Error(w, "INTERNAL_ERROR", http.StatusInternalServerError)
2929
return
3030
}
3131

3232
// Builds the response with the obtained data.
33-
responseJSON, err := json.Marshal(response)
33+
responseJSON, err := json.Marshal(qurl.Response)
3434
if err != nil {
3535
http.Error(w, "INTERNAL_ERROR", http.StatusInternalServerError)
3636
return

server/server.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ import (
99
"github.com/repejota/qurl/routes"
1010
)
1111

12-
// QURLService ...
13-
type QURLService struct {
14-
URL string
15-
}
16-
1712
// Start starts the HTTP server for the qurl API microservice.
1813
func Start(address string, port string) {
1914

0 commit comments

Comments
 (0)