class SoapyCake::Response

Constants

ELEMENTS_DEPTH
SHORT_ELEMENT_DEPTH

Attributes

body[R]
short_response[R]
time_converter[R]

Public Class Methods

new(body, short_response, time_converter) click to toggle source
# File lib/soapy_cake/response.rb, line 12
def initialize(body, short_response, time_converter)
  @body = body
  @short_response = short_response
  @time_converter = time_converter
end

Public Instance Methods

to_enum() click to toggle source
# File lib/soapy_cake/response.rb, line 18
def to_enum
  check_errors!

  return typed_element(sax.at_depth(SHORT_ELEMENT_DEPTH).first) if short_response

  Enumerator.new do |y|
    sax.at_depth(ELEMENTS_DEPTH).each do |element|
      y << typed_element(element)
    end
  end
end
to_xml() click to toggle source
# File lib/soapy_cake/response.rb, line 30
def to_xml
  check_errors!

  (empty? ? [] : [body.to_s]).to_enum
end

Private Instance Methods

check_errors!() click to toggle source
# File lib/soapy_cake/response.rb, line 55
def check_errors!
  # If we get a lot of data in our response, we can assume there was no error.
  # This saves a lot of time because we don't have to scan the whole XML tree for errors.
  return if body.length > 8192

  error_check_fault!
  return if error_check_special_case?

  error_check_success!
end
empty?() click to toggle source
# File lib/soapy_cake/response.rb, line 38
def empty?
  sax.at_depth(ELEMENTS_DEPTH).first.nil?
end
error_check_fault!() click to toggle source
# File lib/soapy_cake/response.rb, line 66
def error_check_fault!
  fault = sax.for_tag(:fault).first
  raise RequestFailed, fault[:reason][:text] if fault
end
error_check_special_case?() click to toggle source
# File lib/soapy_cake/response.rb, line 78
def error_check_special_case?
  # Don't ask...
  # As you might imagine, CAKE simply does not return the success element
  # for this specific request. Also, this is the only request with a tag depth
  # of 4, not 3 or 5 like ALL OTHER requests.
  # BTW: There is a 10$ reward if anyone can find a worse designed API.
  return true if sax.for_tag(:MediaType).count.positive?

  false
end
error_check_success!() click to toggle source
# File lib/soapy_cake/response.rb, line 71
def error_check_success!
  return if sax.for_tag(:success).first == 'true'
  raise RateLimitError if error_message == 'Restricted'

  raise RequestFailed, error_message
end
error_message() click to toggle source
# File lib/soapy_cake/response.rb, line 89
def error_message
  @error_message ||= sax.for_tag(:message).first || sax.for_tag(:Text).first || 'Unknown error'
end
sax() click to toggle source
# File lib/soapy_cake/response.rb, line 48
def sax
  @sax ||= Saxerator.parser(StringIO.new(body)) do |config|
    config.symbolize_keys!
    config.ignore_namespaces!
  end
end
typed_element(element) click to toggle source
# File lib/soapy_cake/response.rb, line 42
def typed_element(element)
  walk_tree(element) do |value, key|
    ResponseValue.new(key, value, time_converter).parse
  end
end