Class: Echonest::Base
- Inherits:
-
Object
- Object
- Echonest::Base
- Defined in:
- lib/echonest-ruby-api/base.rb
Defined Under Namespace
Classes: EchonestConnectionError
Class Method Summary collapse
-
.base_uri ⇒ Object
Gets the base URI for all API calls.
-
.get_api_endpoint(api_key, endpoint, options = {}) ⇒ Object
Gets an arbitrary Echonest endpoint.
-
.version ⇒ Object
The current version of the Echonest API to be supported.
Instance Method Summary collapse
- #endpoint ⇒ Object
- #entity_name ⇒ Object
-
#get(endpoint, options = {}) ⇒ Object
Performs a simple HTTP get on an API endpoint.
- #get_response(options = {}) ⇒ Object
-
#initialize(api_key) ⇒ Base
constructor
A new instance of Base.
-
#which(cmd) ⇒ Object
Cross-platform way of finding an executable in the $PATH.
Constructor Details
#initialize(api_key) ⇒ Base
Returns a new instance of Base.
8 9 10 11 |
# File 'lib/echonest-ruby-api/base.rb', line 8 def initialize(api_key) @api_key = api_key @base_uri = "http://developer.echonest.com/api/v4/" end |
Class Method Details
.base_uri ⇒ Object
Gets the base URI for all API calls
Returns a String
60 61 62 |
# File 'lib/echonest-ruby-api/base.rb', line 60 def self.base_uri "http://developer.echonest.com/api/v#{version}/" end |
.get_api_endpoint(api_key, endpoint, options = {}) ⇒ Object
Gets an arbitrary Echonest endpoint
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/echonest-ruby-api/base.rb', line 65 def self.get_api_endpoint(api_key, endpoint, = {}) .merge!(api_key: api_key, format: "json") = { query_string_normalizer: HTTParty::Request::NON_RAILS_QUERY_STRING_NORMALIZER, query: } response = HTTParty.get("#{ base_uri }#{ endpoint }", ) json = MultiJson.load(response.body, symbolize_keys: true) response_code = json[:response][:status][:code] if response_code.eql?(0) json[:response] else error = Echonest::Error.new(response_code, response) raise error, "Error code #{response_code}: #{error.description}" end end |
.version ⇒ Object
The current version of the Echonest API to be supported.
Returns a Fixnum
86 87 88 |
# File 'lib/echonest-ruby-api/base.rb', line 86 def self.version 4 end |
Instance Method Details
#endpoint ⇒ Object
21 22 23 24 |
# File 'lib/echonest-ruby-api/base.rb', line 21 def endpoint calling_method = caller[1].split('`').last[0..-2] "#{ entity_name }/#{ calling_method }" end |
#entity_name ⇒ Object
17 18 19 |
# File 'lib/echonest-ruby-api/base.rb', line 17 def entity_name self.class.to_s.split('::').last.downcase end |
#get(endpoint, options = {}) ⇒ Object
Performs a simple HTTP get on an API endpoint.
Examples:
get('artist/biographies', results: 10)
#=> Array of Biography objects.
Raises an ArgumentError
if the Echonest API responds with an error.
-
endpoint
- The name of an API endpoint as a String -
options
- A Hash of options to pass to the end point.
Returns a response as a Hash
39 40 41 |
# File 'lib/echonest-ruby-api/base.rb', line 39 def get(endpoint, = {}) Base.get_api_endpoint(@api_key, endpoint, ) end |
#get_response(options = {}) ⇒ Object
13 14 15 |
# File 'lib/echonest-ruby-api/base.rb', line 13 def get_response( = {}) get(endpoint, ) end |
#which(cmd) ⇒ Object
Cross-platform way of finding an executable in the $PATH.
which('ruby') #=> /usr/bin/ruby
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/echonest-ruby-api/base.rb', line 46 def which(cmd) exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| exts.each { |ext| exe = File.join(path, "#{ cmd }#{ ext }") return exe if File.executable? exe } end return nil end |