Exception: Echonest::Error

Inherits:
Exception
  • Object
show all
Defined in:
lib/echonest-ruby-api/error.rb

Constant Summary collapse

ERRORS =
{ '-1' => 'Unknown Error',
  '0' => 'Success',
  '1' => 'Invalid/Missing API Key',
  '2' => 'This API key is not allowed to call that method',
  '3' => 'Rate limit exceeded',
  '4' => 'Missing parameter',
  '5' => 'Invalid parameter',
  '6' => 'Missing echoprint-codegen binary on $PATH'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error_code, response = nil) ⇒ Error

Returns a new instance of Error.



16
17
18
19
# File 'lib/echonest-ruby-api/error.rb', line 16

def initialize(error_code, response = nil)
  @error_code = error_code
  @response = response
end

Instance Attribute Details

#error_codeObject (readonly)

Returns the value of attribute error_code.



4
5
6
# File 'lib/echonest-ruby-api/error.rb', line 4

def error_code
  @error_code
end

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/echonest-ruby-api/error.rb', line 4

def response
  @response
end

Instance Method Details

#descriptionObject



21
22
23
# File 'lib/echonest-ruby-api/error.rb', line 21

def description
  ERRORS[@error_code.to_s]
end

#detailsObject



25
26
27
28
29
30
31
# File 'lib/echonest-ruby-api/error.rb', line 25

def details
  if error_code == 3
    # Returns a hash similar to {"remaining"=>0, "limit"=>20, "used"=>29}.
    Hash[response.headers.select{|k,v| k =~ /x-ratelimit/}.
      map{ |k,v| [k.sub('x-ratelimit-', ''), v.first.to_i] }]
  end
end