Class: EchoNest::Request
- Inherits:
-
Object
- Object
- EchoNest::Request
- Defined in:
- lib/another_echonest_ruby_api/request.rb
Constant Summary collapse
- BASE_URL =
"http://developer.echonest.com/api/v4/"
Instance Attribute Summary collapse
-
#bucket_params ⇒ Object
readonly
Returns the value of attribute bucket_params.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #api_key ⇒ Object
- #build_params ⇒ Object
- #build_response(data) ⇒ Object
- #build_url ⇒ Object
- #get_response ⇒ Object
-
#initialize(path, opts = {}) ⇒ Request
constructor
A new instance of Request.
- #url_with_creds ⇒ Object
Constructor Details
#initialize(path, opts = {}) ⇒ Request
Returns a new instance of Request.
13 14 15 16 17 18 19 20 21 |
# File 'lib/another_echonest_ruby_api/request.rb', line 13 def initialize(path, opts={}) @path = path @params = opts @bucket_params = if bucket = opts.delete(:bucket) bucket.is_a?(Array) ? bucket : [bucket] else [] end end |
Instance Attribute Details
#bucket_params ⇒ Object (readonly)
Returns the value of attribute bucket_params.
11 12 13 |
# File 'lib/another_echonest_ruby_api/request.rb', line 11 def bucket_params @bucket_params end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
11 12 13 |
# File 'lib/another_echonest_ruby_api/request.rb', line 11 def params @params end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/another_echonest_ruby_api/request.rb', line 11 def path @path end |
Class Method Details
Instance Method Details
#api_key ⇒ Object
70 71 72 |
# File 'lib/another_echonest_ruby_api/request.rb', line 70 def api_key EchoNest.config.api_key end |
#build_params ⇒ Object
50 51 52 |
# File 'lib/another_echonest_ruby_api/request.rb', line 50 def build_params {param_name => params} end |
#build_response(data) ⇒ Object
46 47 48 |
# File 'lib/another_echonest_ruby_api/request.rb', line 46 def build_response(data) Response.new(:raw_json => data.to_str, :request => self) end |
#build_url ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/another_echonest_ruby_api/request.rb', line 54 def build_url url = url_with_creds if params.any? parts = [] params.each_pair do |name, value| parts << "#{URI.escape(name.to_s)}=#{URI.escape(value.to_s)}" end bucket_params.each do |value| parts << "bucket=#{value}" end url = "#{url}&#{parts.join('&')}" end # puts "url: #{url.inspect}" url end |
#get_response ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/another_echonest_ruby_api/request.rb', line 28 def get_response response = RestClient.get(build_url) do |response, request, result, &block| # puts "response.code: #{response.code.inspect}" # puts "response.body: #{response.body.inspect}" if [200, 400].include? response.code build_response(response.body) else raise RequestException, "Unexpected response code: #{response.code}" end end if response.error? raise RequestException, "The following errors were returned: #{response..inspect}" else response end end |
#url_with_creds ⇒ Object
74 75 76 |
# File 'lib/another_echonest_ruby_api/request.rb', line 74 def url_with_creds "#{BASE_URL}#{path}?api_key=#{api_key}&format=json" end |