1- package rss
1+ package main
22
33import (
44 "encoding/xml"
55 "errors"
66 "log"
77 "net/http"
88 "regexp"
9-
10- "github.com/goinaction/code/src/chapter2/search/data"
11- "github.com/goinaction/code/src/chapter2/search/feed"
129)
1310
1411type (
@@ -54,25 +51,16 @@ type (
5451 }
5552)
5653
57- type (
58- // Implements the Matcher interface.
59- matcher struct {
60- Feed * data.Feed
61- }
62- )
63-
64- // NewMatcher creates a value of matcher for use.
65- func NewMatcher (feed * data.Feed ) feed.Matcher {
66- return & matcher {
67- Feed : feed ,
68- }
54+ // rssMatcher implements the Matcher interface.
55+ type rssMatcher struct {
56+ * Feed
6957}
7058
7159// Match looks at the document for the specified search term.
72- func (m * matcher ) Match (searchTerm string ) ([]feed. Result , error ) {
73- var results []feed. Result
60+ func (m * rssMatcher ) Match (searchTerm string ) ([]Result , error ) {
61+ var results []Result
7462
75- log .Printf ("Search Feed Type[%s] Site[%s] For Uri[%s]\n " , m .Feed . Type , m .Feed . Name , m . Feed .Uri )
63+ log .Printf ("Search Feed Type[%s] Site[%s] For Uri[%s]\n " , m .Type , m .Name , m .Uri )
7664
7765 // Retrieve the data to search.
7866 document , err := m .retrieve ()
@@ -89,7 +77,7 @@ func (m *matcher) Match(searchTerm string) ([]feed.Result, error) {
8977
9078 // If we found a match save the result.
9179 if matched {
92- results = append (results , feed. Result {
80+ results = append (results , Result {
9381 Field : "Title" ,
9482 Content : channelItem .Title ,
9583 })
@@ -103,7 +91,7 @@ func (m *matcher) Match(searchTerm string) ([]feed.Result, error) {
10391
10492 // If we found a match save the result.
10593 if matched {
106- results = append (results , feed. Result {
94+ results = append (results , Result {
10795 Field : "Description" ,
10896 Content : channelItem .Description ,
10997 })
@@ -114,24 +102,25 @@ func (m *matcher) Match(searchTerm string) ([]feed.Result, error) {
114102}
115103
116104// retrieve performs a HTTP Get request for the rss feed and unmarshals the results.
117- func (m * matcher ) retrieve () (* document , error ) {
118- if m .Feed . Uri == "" {
105+ func (m * rssMatcher ) retrieve () (* document , error ) {
106+ if m .Uri == "" {
119107 return nil , errors .New ("No rss feed uri provided" )
120108 }
121109
122110 // Retrieve the rss feed document from the web.
123- resp , err := http .Get (m .Feed . Uri )
111+ resp , err := http .Get (m .Uri )
124112 if err != nil {
125113 return nil , err
126114 }
127115
128116 // Close the response once we return from the function.
129117 defer resp .Body .Close ()
130118
119+ // TODO: check response status, don't process a non 200 response body.
120+
131121 // Unmarshal the rss feed document into our struct type.
132122 var document document
133- err = xml .NewDecoder (resp .Body ).Decode (& document )
134- if err != nil {
123+ if err := xml .NewDecoder (resp .Body ).Decode (& document ); err != nil {
135124 return nil , err
136125 }
137126
0 commit comments