Skip to content

Commit 3ff2127

Browse files
Abstracted sites from the interface.
1 parent 56d80d6 commit 3ff2127

3 files changed

Lines changed: 19 additions & 16 deletions

File tree

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package find
33
import (
44
"log"
55
"sync"
6-
7-
"github.com/goinaction/code/src/chapter2/search/feeds"
86
)
97

108
type (
@@ -16,23 +14,21 @@ type (
1614

1715
// Matcher defines the behavior required by the Search function.
1816
Matcher interface {
19-
Match(site *feeds.Site, searchTerm string) ([]Result, error)
17+
Match(searchTerm string) ([]Result, error)
2018
}
2119
)
2220

2321
// Search pulls down each feed looking for the search term.
24-
func Search(matcher Matcher, searchTerm string, site feeds.Site, result chan Result, waitGroup *sync.WaitGroup) {
22+
func Search(matcher Matcher, searchTerm string, result chan Result, waitGroup *sync.WaitGroup) {
2523
// Call done so we can report we are finished processing.
2624
defer func() {
2725
waitGroup.Done()
2826
}()
2927

30-
log.Printf("Search Feed Type[%s] Site[%s] For Uri[%s]\n", site.Type, site.Name, site.Uri)
31-
3228
// Search the data for the search term.
33-
searchResults, err := matcher.Match(&site, searchTerm)
29+
searchResults, err := matcher.Match(searchTerm)
3430
if err != nil {
35-
log.Printf("%s : %s", site.Uri, err)
31+
log.Println(err)
3632
return
3733
}
3834

‎src/chapter2/search/main.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@ func main() {
4242
// Create the right type of matcher for this search.
4343
switch site.Type {
4444
case "rss":
45-
matcher = new(rss.Search)
45+
matcher = &rss.Search{
46+
Site: site,
47+
}
4648

4749
default:
4850
log.Fatalln("Invalid Type")
4951
}
5052

5153
// Launch the goroutine to perform the search.
52-
go find.Search(matcher, searchTerm, site, result, &waitGroup)
54+
go find.Search(matcher, searchTerm, result, &waitGroup)
5355
}
5456

5557
// Wait for everything to be processed.

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package rss
33
import (
44
"encoding/xml"
55
"errors"
6+
"log"
67
"net/http"
78
"regexp"
89

@@ -55,15 +56,19 @@ type (
5556

5657
type (
5758
// Implements the searcher interface.
58-
Search struct{}
59+
Search struct {
60+
Site feeds.Site
61+
}
5962
)
6063

6164
// Match looks at the document for the specified search term.
62-
func (s *Search) Match(site *feeds.Site, searchTerm string) ([]find.Result, error) {
65+
func (s *Search) Match(searchTerm string) ([]find.Result, error) {
6366
var results []find.Result
6467

68+
log.Printf("Search Feed Type[%s] Site[%s] For Uri[%s]\n", s.Site.Type, s.Site.Name, s.Site.Uri)
69+
6570
// Retrieve the data to search.
66-
document, err := retrieve(site)
71+
document, err := s.retrieve()
6772
if err != nil {
6873
return nil, err
6974
}
@@ -102,13 +107,13 @@ func (s *Search) Match(site *feeds.Site, searchTerm string) ([]find.Result, erro
102107
}
103108

104109
// retrieve performs a HTTP Get request for the rss feed and unmarshals the results.
105-
func retrieve(site *feeds.Site) (*Document, error) {
106-
if site.Uri == "" {
110+
func (s *Search) retrieve() (*Document, error) {
111+
if s.Site.Uri == "" {
107112
return nil, errors.New("No RSS Feed Uri Provided")
108113
}
109114

110115
// Retrieve the rss feed document from the web.
111-
resp, err := http.Get(site.Uri)
116+
resp, err := http.Get(s.Site.Uri)
112117
if err != nil {
113118
return nil, err
114119
}

0 commit comments

Comments
 (0)