-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch.go
66 lines (56 loc) · 1.47 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package gol
import (
"fmt"
"strings"
)
// SearchURL holds information about the Search URL
type SearchURL struct {
url string
all string
title string
author string
subject string
}
// Search returns search API data from a constructed url
func Search(s string) (Container, error) {
container, err := Request(s)
if err != nil {
return nil, err
}
if err := HasError(container); err != nil {
return nil, err
}
return container, nil
}
// Url returns OpenLibrary's search url
func SearchUrl() SearchURL {
return SearchURL{url: "https://openlibrary.org/search.json?"}
}
// All allows to search in everything
func (s SearchURL) All(q string) SearchURL {
q = strings.ReplaceAll(q, " ", "+")
s.all = fmt.Sprintf("&q=%s", q)
return s
}
// Title allows to only search in titles
func (s SearchURL) Title(t string) SearchURL {
t = strings.ReplaceAll(t, " ", "+")
s.title = fmt.Sprintf("&title=%s", t)
return s
}
// Author allows to only search in authors
func (s SearchURL) Author(a string) SearchURL {
a = strings.ReplaceAll(a, " ", "+")
s.author = fmt.Sprintf("&author=%s", a)
return s
}
// Subject allow to search in a specific subject
func (s SearchURL) Subject(sbj string) SearchURL {
sbj = strings.ReplaceAll(sbj, " ", "+")
s.subject = fmt.Sprintf("&subject=%s", sbj)
return s
}
// Construct constructs and returns a complete searchable url
func (su SearchURL) Construct() string {
return fmt.Sprintf("%s%s%s%s%s", su.url, su.all, su.title, su.author, su.subject)
}