-
Notifications
You must be signed in to change notification settings - Fork 1
/
retailer.go
74 lines (65 loc) · 1.76 KB
/
retailer.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
67
68
69
70
71
72
73
74
package labo
import (
"strings"
"github.com/PuerkitoBio/goquery"
)
// Retailer is a snapshot of a first or third party retailer that stocks and sells Nintendo Labo products.
type Retailer struct {
Href *Href `json:"href"`
Image *Image `json:"image"`
Name string `json:"name"`
}
var (
retailerFn = [](func(*goquery.Selection, *Retailer)){
getRetailerHref,
getRetailerImage,
getRetailerName}
)
// getRetailerHref searches the *goquery.Selection for the *labo.Href required for a labo.Toycon
func getRetailerHref(s *goquery.Selection, r *Retailer) {
r.Href = newHref(s)
}
// getRetailerImage searches the *goquery.Selection for the *labo.Image struct required for a labo.Retailer
func getRetailerImage(s *goquery.Selection, r *Retailer) {
r.Image = newImage(s)
}
// getRetailerName searches the *goquery.Selection for the name of the retailer required for a labo.Retailer
func getRetailerName(s *goquery.Selection, r *Retailer) {
var (
name = defaultRetailerName
ok bool
substring string
)
substring, ok = s.Attr(attrClass)
if ok {
name = strings.TrimSpace(substring)
}
r.Name = name
}
// newRetailer is a constructor function that instantiates and returns a new *labo.Retailer.
func newRetailer(s *goquery.Selection) *Retailer {
var (
retailer = &Retailer{}
)
for _, fn := range retailerFn {
fn(s, retailer)
}
return retailer
}
// newRetailers is a constructor function that instantiates and returns a new slice of *labo.Retailer.
func newRetailers(s *goquery.Selection) []*Retailer {
var (
retailer *Retailer
retailers []*Retailer
ok bool
)
s.Each(func(i int, s *goquery.Selection) {
retailer = newRetailer(s)
ok = (retailer != nil)
if !ok {
return
}
retailers = append(retailers, retailer)
})
return retailers
}