Class: Fabulous::Response
- Inherits:
-
Object
- Object
- Fabulous::Response
- Defined in:
- lib/fabulous/response.rb
Instance Attribute Summary collapse
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
-
#raw_xml ⇒ Object
readonly
Returns the value of attribute raw_xml.
Instance Method Summary collapse
- #current_page ⇒ Object
- #data ⇒ Object
-
#initialize(xml_string) ⇒ Response
constructor
A new instance of Response.
- #page_count ⇒ Object
- #paginated? ⇒ Boolean
- #status_code ⇒ Object
- #status_message ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(xml_string) ⇒ Response
Returns a new instance of Response.
7 8 9 10 |
# File 'lib/fabulous/response.rb', line 7 def initialize(xml_string) @raw_xml = xml_string @doc = Nokogiri::XML(xml_string) end |
Instance Attribute Details
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
5 6 7 |
# File 'lib/fabulous/response.rb', line 5 def doc @doc end |
#raw_xml ⇒ Object (readonly)
Returns the value of attribute raw_xml.
5 6 7 |
# File 'lib/fabulous/response.rb', line 5 def raw_xml @raw_xml end |
Instance Method Details
#current_page ⇒ Object
47 48 49 50 |
# File 'lib/fabulous/response.rb', line 47 def current_page # Try to get from request params doc.at_xpath("//request/params/param[@name='page']")&.text&.to_i || 1 end |
#data ⇒ Object
28 29 30 |
# File 'lib/fabulous/response.rb', line 28 def data @data ||= parse_data end |
#page_count ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/fabulous/response.rb', line 39 def page_count # Calculate based on total results and page size total = doc.at_xpath("//results")&.attr("count")&.to_i || 0 page_size = doc.xpath("//results/result").length return 1 if page_size == 0 (total.to_f / page_size).ceil end |
#paginated? ⇒ Boolean
32 33 34 35 36 37 |
# File 'lib/fabulous/response.rb', line 32 def paginated? # Check if results count exceeds what's shown (pagination needed) total = doc.at_xpath("//results")&.attr("count")&.to_i || 0 shown = doc.xpath("//results/result").length total > shown && shown > 0 end |
#status_code ⇒ Object
16 17 18 19 20 |
# File 'lib/fabulous/response.rb', line 16 def status_code # Try both old and new format @status_code ||= doc.at_xpath("//statusCode")&.text&.to_i || doc.at_xpath("//response/status")&.text&.to_i end |
#status_message ⇒ Object
22 23 24 25 26 |
# File 'lib/fabulous/response.rb', line 22 def # Try both old and new format ||= doc.at_xpath("//statusText")&.text || doc.at_xpath("//response/reason")&.text end |
#success? ⇒ Boolean
12 13 14 |
# File 'lib/fabulous/response.rb', line 12 def success? status_code && status_code.to_i == 200 end |