Sha256: a66ef985a130bd93da9412b03c005a7f9169653e49102916570e22df96b7d043

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

module EVEApi
  # Handling of requests and response from the EVE Online API
  class Request
    attr_accessor :data
    attr_accessor :result
    attr_accessor :response

    def initialize(response = nil)
      @response = response
      fail "HTTP: #{response.status}" unless response.status == 200
      @data = parse_xml
      @result = convert_hash_keys(parse_result)
      fail error if error
    end

    def error
      data['eveapi'].key?('error') ? data['eveapi']['error'] : false
    end

    def parse_xml
      Crack::XML.parse(response.body)
    end

    def process_hash(data)
      data.each_value do |v|
        v.process_rows if v.is_a?(Hash)
      end.process_rows
    end

    def process_array(data)
      data.each do |v|
        v.process_rows if v.is_a?(Hash)
      end
    end

    def parse_result
      api_result = data['eveapi']['result']
      case api_result['rowset']['row']
      when Array
        return process_array api_result['rowset']['row']
      else
        return api_result['rowset']['row']
      end
    rescue TypeError, NoMethodError
      return process_hash(api_result)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
eveapi-0.1.2 lib/eveapi/request.rb
eveapi-0.1.0 lib/eveapi/request.rb