Class: Fabulous::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#docObject (readonly)

Returns the value of attribute doc.



5
6
7
# File 'lib/fabulous/response.rb', line 5

def doc
  @doc
end

#raw_xmlObject (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_pageObject



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

#dataObject



28
29
30
# File 'lib/fabulous/response.rb', line 28

def data
  @data ||= parse_data
end

#page_countObject



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

Returns:

  • (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_codeObject



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_messageObject



22
23
24
25
26
# File 'lib/fabulous/response.rb', line 22

def status_message
  # Try both old and new format  
  @status_message ||= doc.at_xpath("//statusText")&.text ||
                      doc.at_xpath("//response/reason")&.text
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  status_code && status_code.to_i == 200
end