Skip to content

Commit 58d85e4

Browse files
Fixed interface and way data is retrieved
1 parent e2b6e41 commit 58d85e4

2 files changed

Lines changed: 48 additions & 49 deletions

File tree

‎src/chapter2/search/find/find.go‎

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package find
22

33
import (
44
"log"
5-
"regexp"
65
"sync"
76

87
"github.com/goinaction/code/src/chapter2/search/feeds"
@@ -25,7 +24,7 @@ type (
2524
type (
2625
// Matcher defines the behavior required by the Search function.
2726
Matcher interface {
28-
Retrieve(site feeds.Site) ([]SearchData, error)
27+
Match(site *feeds.Site, searchTerm string) ([]Result, error)
2928
}
3029
)
3130

@@ -38,15 +37,8 @@ func Search(matcher Matcher, searchTerm string, site feeds.Site, result chan Res
3837

3938
log.Printf("Search Feed Type[%s] Site[%s] For Uri[%s]\n", site.Type, site.Name, site.Uri)
4039

41-
// Retrieve the search data.
42-
searchData, err := matcher.Retrieve(site)
43-
if err != nil {
44-
log.Printf("%s : %s", site.Uri, err)
45-
return
46-
}
47-
4840
// Search the data for the search term.
49-
searchResults, err := match(searchData, searchTerm)
41+
searchResults, err := matcher.Match(&site, searchTerm)
5042
if err != nil {
5143
log.Printf("%s : %s", site.Uri, err)
5244
return
@@ -57,40 +49,3 @@ func Search(matcher Matcher, searchTerm string, site feeds.Site, result chan Res
5749
result <- searchResult
5850
}
5951
}
60-
61-
// match looks at the document for the specified search term.
62-
func match(searchData []SearchData, searchTerm string) ([]Result, error) {
63-
var results []Result
64-
65-
for _, data := range searchData {
66-
// Check the title for the search term.
67-
matched, err := regexp.MatchString(searchTerm, data.Title)
68-
if err != nil {
69-
return nil, err
70-
}
71-
72-
// If we found a match save the result.
73-
if matched {
74-
results = append(results, Result{
75-
Field: "Title",
76-
Content: data.Title,
77-
})
78-
}
79-
80-
// Check the description for the search term.
81-
matched, err = regexp.MatchString(searchTerm, data.Description)
82-
if err != nil {
83-
return nil, err
84-
}
85-
86-
// If we found a match save the result.
87-
if matched {
88-
results = append(results, Result{
89-
Field: "Description",
90-
Content: data.Description,
91-
})
92-
}
93-
}
94-
95-
return results, nil
96-
}

‎src/chapter2/search/rss/rss.go‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/xml"
55
"errors"
66
"net/http"
7+
"regexp"
78

89
"github.com/goinaction/code/src/chapter2/search/feeds"
910
"github.com/goinaction/code/src/chapter2/search/find"
@@ -57,8 +58,51 @@ type (
5758
Search struct{}
5859
)
5960

60-
// Retrieve performs a HTTP Get request for the rss feed and unmarshals the results.
61-
func (s *Search) Retrieve(site feeds.Site) ([]find.SearchData, error) {
61+
// Match looks at the document for the specified search term.
62+
func (s *Search) Match(site *feeds.Site, searchTerm string) ([]find.Result, error) {
63+
var results []find.Result
64+
65+
// Retrieve the data to search.
66+
searchData, err := s.retrieve(site)
67+
if err != nil {
68+
return nil, err
69+
}
70+
71+
for _, data := range searchData {
72+
// Check the title for the search term.
73+
matched, err := regexp.MatchString(searchTerm, data.Title)
74+
if err != nil {
75+
return nil, err
76+
}
77+
78+
// If we found a match save the result.
79+
if matched {
80+
results = append(results, find.Result{
81+
Field: "Title",
82+
Content: data.Title,
83+
})
84+
}
85+
86+
// Check the description for the search term.
87+
matched, err = regexp.MatchString(searchTerm, data.Description)
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
// If we found a match save the result.
93+
if matched {
94+
results = append(results, find.Result{
95+
Field: "Description",
96+
Content: data.Description,
97+
})
98+
}
99+
}
100+
101+
return results, nil
102+
}
103+
104+
// retrieve performs a HTTP Get request for the rss feed and unmarshals the results.
105+
func (s *Search) retrieve(site *feeds.Site) ([]find.SearchData, error) {
62106
if site.Uri == "" {
63107
return nil, errors.New("No RSS Feed Uri Provided")
64108
}

0 commit comments

Comments
 (0)