Class: WCAPI::OpenSearchResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/wcapi/open_search_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ OpenSearchResponse

Returns a new instance of OpenSearchResponse.



6
7
8
9
10
11
12
13
14
# File 'lib/wcapi/open_search_response.rb', line 6

def initialize(doc)
  #super doc
  @raw = doc
  if doc.index('rss')
    parse_rss(doc)
  else
	parse_atom(doc)
  end
end

Instance Attribute Details

#headerObject

include WCAPI::XPath



4
5
6
# File 'lib/wcapi/open_search_response.rb', line 4

def header
  @header
end

#rawObject

include WCAPI::XPath



4
5
6
# File 'lib/wcapi/open_search_response.rb', line 4

def raw
  @raw
end

#recordsObject

include WCAPI::XPath



4
5
6
# File 'lib/wcapi/open_search_response.rb', line 4

def records
  @records
end

Instance Method Details

#parse_atom(xml) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/wcapi/open_search_response.rb', line 71

def parse_atom(xml)
   _title = ""
   #this is an array
   _author = Array.new()
   _link = ""
   _id = ""
   _citation = ""
   _summary = ""
   _xml = xml
   _record = Array.new()
   _x = 0

   doc = Nokogiri::XML(xml)
   namespaces = {'n0' => 'http://www.w3.org/2005/Atom',
                 'opensearch' => 'http://a9.com/-/spec/opensearch/1.1/'
                }


   @header = {}
   @header["totalResults"] = doc.xpath("//opensearch:totalResults").text
   @header["startIndex"] = doc.xpath("//opensearch:startIndex").text
   @header["itemsPerPage"] = doc.xpath("//opensearch:itemsPerPage").text

   nodes = doc.xpath("//*[local-name()='entry']")
   nodes.each { |item |
	 _author = []
      _title = item.xpath("*[local-name() = 'title'][position()=1]").text
_tmpauthor = item.xpath("*[local-name() = 'author'][position()=1]")

      if _tmpauthor != nil
         if item.xpath("*[local-name() = 'author']/*[local-name() = 'name']") != nil
           item.xpath("*[local-name() = 'author']/*[local-name() = 'name']").each { |i|
             _author.push(i.text)
           }
         end
end

      if item.xpath("*[local-name() = 'id']") != nil
        _link = item.xpath("*[local-name() = 'id']").text
      end

      if _link != ''
        _id = _link.slice(_link.rindex("/")+1, _link.length-_link.rindex("/"))
      end
      if item.xpath("*[local-name() = 'content']") != nil
         _citation = item.xpath("*[local-name() = 'content'][position()=1]").text
      end

      if item.xpath("*[local-name() = 'summary']") != nil
         _summary = item.xpath("*[local-name() = 'summary'][position()=1]").text
      end
      _rechash = {:title => _title, :author => _author, :link => _link, :id => _id, :citation => _citation,
                  :summary => _summary, :xml => item.to_s}
      _record.push(_rechash)
   }
   @records = _record
end

#parse_rss(xml) ⇒ Object



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
# File 'lib/wcapi/open_search_response.rb', line 16

def parse_rss(xml)
   _title = ""
   #this is an array
   _author = Array.new()
   _link = ""
   _id = ""
   _citation = ""
   _summary = ""
   _xml = xml
   _record = Array.new()
   _x = 0

   doc = Nokogiri::XML(xml)

   namespaces = {'content' => 'http://purl.org/rss/1.0/modules/content/',
                 'atom' => 'http://www.w3.org/2005/Atom',
   'opensearch' => 'http://a9.com/-/spec/opensearch/1.1/',
   'srw' => 'http://www.loc.gov/zing/srw/'
                }

   
   @header = {}
   @header["totalResults"] = doc.xpath("//opensearch:totalResults").text
   @header["startIndex"] = doc.xpath("//opensearch:startIndex").text
   @header["itemsPerPage"] = doc.xpath("//opensearch:itemsPerPage").text

   nodes = doc.xpath("//item", namespaces)
   nodes.each { |item |
      _title = item.xpath("title[position()=1]", namespaces).text 
      if item.xpath("author/name[position()=1]", namespaces) != nil 
         item.xpath("author/name", namespaces).each { |i|
           _author.push(i.text)
        }
      end
      if item.xpath("link[position()=1]") != nil 
        _link = item.xpath("link[position()=1]", namespaces).text
      end

      if _link != ''
        _id = _link.slice(_link.rindex("/")+1, _link.length-_link.rindex("/"))
      end 
      if item.xpath("content:encoded[position()=1]", namespaces) != nil
         _citation = item.xpath("content:encoded[position()=1]", namespaces).text
      end

      if item.xpath("description[position()=1]", namespaces) != nil
  _summary = item.xpath("description[position()=1]", namespaces).text
	 end
      _rechash = {:title => _title, :author => _author, :link => _link, :id => _id, :citation => _citation, 
    :summary => _summary, :xml => item.to_s}
      _record.push(_rechash)
   }
   @records = _record
end