Sha256: ddcd47c024e8c0b61c34f41559c3283df9d27ae56772e548d9aa182a01d08147

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

require "rexml/document"

module Echonest
  class Response
    attr_reader :xml

    def initialize(body)
      @xml = REXML::Document.new(body)
    end

    def status
      @status ||= Status.new(@xml)
    end

    def success?
      status.code == Status::SUCCESS
    end

    def query
      @query ||= Query.new(@xml)
    end

    class Status
      UNKNOWN_ERROR = -1
      SUCCESS = 0
      INVALID_API_KEY = 1
      PERMISSION_DENIED = 2
      RATE_LIMIT_EXCEEDED = 3
      MISSING_PARAMETER = 4
      INVALID_PARAMETER = 5

      attr_reader :code, :message

      def initialize(xml)
        @code = xml.elements['response/status/code'][0].to_s.to_i
        @message = xml.elements['response/status/message'][0].to_s
      end
    end

    class Query
      def initialize(xml)
        @parameters = {}

        xml.elements.each('response/query/parameter') do |parameter|
          @parameters[parameter.attributes['name'].to_sym] = parameter.text
        end
      end

      def [](parameter_name)
        @parameters[parameter_name]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
youpy-ruby-echonest-0.0.1 lib/echonest/response.rb
youpy-ruby-echonest-0.0.2 lib/echonest/response.rb