Class: Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#annotationsObject (readonly)

Returns the value of attribute annotations.



13
14
15
# File 'lib/response.rb', line 13

def annotations
  @annotations
end

#docObject (readonly)

Returns the value of attribute doc.



3
4
5
# File 'lib/response.rb', line 3

def doc
  @doc
end

#idsObject

Returns the value of attribute ids.



11
12
13
# File 'lib/response.rb', line 11

def ids
  @ids
end

#imagesObject (readonly)

Returns the value of attribute images.



14
15
16
# File 'lib/response.rb', line 14

def images
  @images
end

#objectsObject

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

#requestObject (readonly)

Returns the value of attribute request.



3
4
5
# File 'lib/response.rb', line 3

def request
  @request
end

#responseObject (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_objectsObject

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

pagination

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/response.rb', line 67

def link_back?
  (get_int('firstResult') > @request.request_options[:limit].to_i) and return true
  false
end

pagination

Returns:

  • (Boolean)


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_idsObject

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.request_options[: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