class UnderFire::APIResponse
Wraps query response, providing to_h
and to_s
methods for easy processing.
Attributes
response[R]
@return [Hash] Response as Hash.
Public Class Methods
new(response)
click to toggle source
@param [String] response XML string.
# File lib/under_fire/api_response.rb, line 10 def initialize(response) @response = parse_response(response) end
Public Instance Methods
success?()
click to toggle source
@return [Boolean] Did the query return something?
# File lib/under_fire/api_response.rb, line 25 def success? response[:responses][:response][:@status] == 'OK' end
to_h()
click to toggle source
@return [Hash] Hash representation of response.
# File lib/under_fire/api_response.rb, line 15 def to_h response[:responses] end
to_s()
click to toggle source
@return [String] String represenation suitable for command line.
# File lib/under_fire/api_response.rb, line 20 def to_s recursive_to_s(to_h) end
Private Instance Methods
parse_response(response)
click to toggle source
Builds hash from XML response. @param [String] response XML response string. @return [Hash] Hash representation of response.
# File lib/under_fire/api_response.rb, line 52 def parse_response(response) parser = Nori.new(:convert_tags_to => lambda {|tag| tag.snakecase.to_sym }) parser.parse(response) end
recursive_to_s(hash)
click to toggle source
Recursively walks nested hash structure to return string representation. @return [String] Flat string representation of nest Hash.
# File lib/under_fire/api_response.rb, line 33 def recursive_to_s(hash) output = "" hash.each do |k,v| if v.is_a? Hash output << "\n" output << "#{k}:\n#{recursive_to_s(v)}\n" elsif v.is_a? Array output << "\n" v.each {|i| output << "#{recursive_to_s(i)}\n" } else output << "#{k}: #{v}\n" end end output end