Class: Response
- Inherits:
-
Object
- Object
- Response
- Defined in:
- lib/response.rb
Instance Attribute Summary collapse
-
#annotations ⇒ Object
readonly
Returns the value of attribute annotations.
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
-
#ids ⇒ Object
Returns the value of attribute ids.
-
#images ⇒ Object
readonly
Returns the value of attribute images.
-
#objects ⇒ Object
return an Array of XML elements whose root is an object element.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#get_int(element_name) ⇒ Object
get the Integer of the first match.
-
#get_text(element_name) ⇒ Object
get the String of the first match.
-
#hashified_objects ⇒ Object
TODO: the has conversion is NOT optimal (no attributes, children of root only), see extension in rubyMorpbank.
-
#initialize(request = Request.new) ⇒ Response
constructor
A new instance of Response.
-
#link_back? ⇒ Boolean
pagination.
-
#link_forward? ⇒ Boolean
pagination.
-
#mb_image_ids ⇒ Object
return an Array of ids representing morphbankIds to images.
Constructor Details
#initialize(request = Request.new) ⇒ Response
Returns a new instance of Response.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/response.rb', line 16 def initialize(request = Request.new) begin @response = Net::HTTP.get_response(URI.parse(request.request_url)).body rescue SocketError raise "can not connect to socket, check SERVICES_URI" end # TODO: check that we're getting a legit XML document back before trying to parse it @doc = REXML::Document.new(@response) @request = request # TODO: can this be aliased some how? self end |
Instance Attribute Details
#annotations ⇒ Object (readonly)
Returns the value of attribute annotations.
13 14 15 |
# File 'lib/response.rb', line 13 def annotations @annotations end |
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
3 4 5 |
# File 'lib/response.rb', line 3 def doc @doc end |
#ids ⇒ Object
Returns the value of attribute ids.
11 12 13 |
# File 'lib/response.rb', line 11 def ids @ids end |
#images ⇒ Object (readonly)
Returns the value of attribute images.
14 15 16 |
# File 'lib/response.rb', line 14 def images @images end |
#objects ⇒ Object
return an Array of XML elements whose root is an object element
10 11 12 |
# File 'lib/response.rb', line 10 def objects @objects end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
3 4 5 |
# File 'lib/response.rb', line 3 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
3 4 5 |
# File 'lib/response.rb', line 3 def response @response end |
Instance Method Details
#get_int(element_name) ⇒ Object
get the Integer of the first match
41 42 43 |
# File 'lib/response.rb', line 41 def get_int(element_name) REXML::XPath.first(@doc, "//#{element_name}").nil? ? 0 : REXML::XPath.first(@doc, "//#{element_name}").text.to_i end |
#get_text(element_name) ⇒ Object
get the String of the first match
46 47 48 |
# File 'lib/response.rb', line 46 def get_text(element_name) REXML::XPath.first(@doc, "//#{element_name}").nil? ? 0 : REXML::XPath.first(@doc, "//#{element_name}").text end |
#hashified_objects ⇒ Object
TODO: the has conversion is NOT optimal (no attributes, children of root only), see extension in rubyMorpbank
56 57 58 |
# File 'lib/response.rb', line 56 def hashified_objects self.doc.records("//object") end |
#link_back? ⇒ Boolean
pagination
67 68 69 70 |
# File 'lib/response.rb', line 67 def link_back? (get_int('firstResult') > @request.[:limit].to_i) and return true false end |
#link_forward? ⇒ Boolean
pagination
61 62 63 64 |
# File 'lib/response.rb', line 61 def link_forward? ((get_int('numResultsReturned') + get_int('firstResult')) < get_int('numResults')) and return true false end |
#mb_image_ids ⇒ Object
return an Array of ids representing morphbankIds to images
32 33 34 35 36 37 38 |
# File 'lib/response.rb', line 32 def mb_image_ids if request.[:format] == 'id' REXML::XPath.match(@doc, "//id").collect{|e| e.text.to_s} else raise RubyMorphbankError, "you must use method='image' in requests to use the mb_image_ids method" end end |