Class: WCAPI::OpenSearchResponse
- Inherits:
-
Object
- Object
- WCAPI::OpenSearchResponse
- Includes:
- XPath
- Defined in:
- lib/wcapi/open_search_response.rb
Instance Attribute Summary collapse
-
#header ⇒ Object
Returns the value of attribute header.
-
#raw ⇒ Object
Returns the value of attribute raw.
-
#records ⇒ Object
Returns the value of attribute records.
Instance Method Summary collapse
-
#initialize(doc) ⇒ OpenSearchResponse
constructor
A new instance of OpenSearchResponse.
- #parse_atom(xml) ⇒ Object
- #parse_rss(xml) ⇒ Object
Methods included from XPath
#get_attribute, #xpath, #xpath_all, #xpath_first, #xpath_get_all_text, #xpath_get_name, #xpath_get_text
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
#header ⇒ Object
Returns the value of attribute header.
4 5 6 |
# File 'lib/wcapi/open_search_response.rb', line 4 def header @header end |
#raw ⇒ Object
Returns the value of attribute raw.
4 5 6 |
# File 'lib/wcapi/open_search_response.rb', line 4 def raw @raw end |
#records ⇒ Object
Returns the value of attribute records.
4 5 6 |
# File 'lib/wcapi/open_search_response.rb', line 4 def records @records end |
Instance Method Details
#parse_atom(xml) ⇒ Object
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/wcapi/open_search_response.rb', line 85 def parse_atom(xml) _title = "" #this is an array = Array.new() _link = "" _id = "" _citation = "" _summary = "" _xml = xml _record = Array.new() _x = 0 begin require 'xml/libxml' #_parser = LibXML::XML::Parser.new() #_parser.string = xml #doc = LibXML::XML::Document.new() #doc = _parser.parse doc = LibXML::XML::Document.string(xml) rescue begin require 'rexml/document' doc = REXML::Document.new(xml) rescue #likely some kind of xml error end end namespaces = {'n0' => 'http://www.w3.org/2005/Atom', 'opensearch' => 'http://a9.com/-/spec/opensearch/1.1/' } @header = {} @header["totalResults"] = xpath_get_text(xpath_first(doc, "//opensearch:totalResults")) @header["startIndex"] = xpath_get_text(xpath_first(doc, "//opensearch:startIndex")) @header["itemsPerPage"] = xpath_get_text(xpath_first(doc, "//opensearch:itemsPerPage")) nodes = xpath_all(doc, "//*[local-name()='entry']") nodes.each { |item | = [] _title = xpath_get_text(xpath_first(item, "*[local-name() = 'title']")) = xpath_first(item, "*[local-name() = 'author']") if != nil if xpath_first(item, "*[local-name() = 'author']/*[local-name() = 'name']") != nil xpath_all(item, "*[local-name() = 'author']/*[local-name() = 'name']").each { |i| .push(xpath_get_text(i)) } end end if xpath_first(item, "*[local-name() = 'id']") != nil _link = xpath_get_text(xpath_first(item, "*[local-name() = 'id']")) end if _link != '' _id = _link.slice(_link.rindex("/")+1, _link.length-_link.rindex("/")) end if xpath_first(item, "*[local-name() = 'content']") != nil _citation = xpath_get_text(xpath_first(item, "*[local-name() = 'content']")) end if xpath_first(item, "*[local-name() = 'summary']") != nil _summary = xpath_get_text(xpath_first(item, "*[local-name() = 'summary']")) end _rechash = {:title => _title, :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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/wcapi/open_search_response.rb', line 16 def parse_rss(xml) _title = "" #this is an array = Array.new() _link = "" _id = "" _citation = "" _summary = "" _xml = xml _record = Array.new() _x = 0 begin require 'xml/libxml' #_parser = LibXML::XML::Parser.new() #_parser.string = xml #doc = LibXML::XML::Document.new() #doc = _parser.parse doc = LibXML::XML::Document.string(xml) rescue begin require 'rexml/document' doc = REXML::Document.new(xml) rescue #likely some kind of xml error end end 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"] = xpath_get_text(xpath_first(doc, "//opensearch:totalResults")) @header["startIndex"] = xpath_get_text(xpath_first(doc, "//opensearch:startIndex")) @header["itemsPerPage"] = xpath_get_text(xpath_first(doc, "//opensearch:itemsPerPage")) nodes = xpath_all(doc, "//item", namespaces) nodes.each { |item | _title = xpath_get_text(xpath_first(item, "title")) if xpath_first(item, "author/name", namespaces) != nil xpath_all(item, "author/name", namespaces).each { |i| .push(xpath_get_text(i)) } end if xpath_first(item, "link", namespaces) != nil _link = xpath_get_text(xpath_first(item, "link", namespaces)) end if _link != '' _id = _link.slice(_link.rindex("/")+1, _link.length-_link.rindex("/")) end if xpath_first(item, "content:encoded", namespaces) != nil _citation = xpath_get_text(xpath_first(item, "content:encoded", namespaces)) end if xpath_first(item, "description", namespaces) != nil _summary = xpath_get_text(xpath_first(item, "description", namespaces)) end _rechash = {:title => _title, :author => , :link => _link, :id => _id, :citation => _citation, :summary => _summary, :xml => item.to_s} _record.push(_rechash) } @records = _record end |