Class: EchoNest::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/another_echonest_ruby_api/request.rb

Constant Summary collapse

BASE_URL =
"http://developer.echonest.com/api/v4/"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_paramsObject (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

#paramsObject (readonly)

Returns the value of attribute params.



11
12
13
# File 'lib/another_echonest_ruby_api/request.rb', line 11

def params
  @params
end

#pathObject (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

.get(path, opts = {}) ⇒ Object



23
24
25
26
# File 'lib/another_echonest_ruby_api/request.rb', line 23

def self.get(path, opts={})
  request = Request.new(path, opts)
  request.get_response
end

Instance Method Details

#api_keyObject



70
71
72
# File 'lib/another_echonest_ruby_api/request.rb', line 70

def api_key
  EchoNest.config.api_key
end

#build_paramsObject



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_urlObject



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_responseObject



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.message.inspect}"
  else
    response
  end
end

#url_with_credsObject



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