lib/echonest/api.rb in youpy-ruby-echonest-0.0.5 vs lib/echonest/api.rb in youpy-ruby-echonest-0.0.6
- old
+ new
@@ -1,19 +1,21 @@
require 'digest/md5'
+require 'httpclient'
module Echonest
class Api
VERSION = '3'
- URL = 'http://developer.echonest.com/api/'
+ BASE_URL = 'http://developer.echonest.com/api/'
+ USER_AGENT = '%s/%s' % ['ruby-echonest', ::Echonest::VERSION]
class Error < StandardError; end
- attr_reader :connection
+ attr_reader :user_agent
def initialize(api_key)
@api_key = api_key
- @connection = Connection.new(URL)
+ @user_agent = HTTPClient.new(:agent_name => USER_AGENT)
end
def get_bars(filename)
get_analysys(:get_bars, filename) do |analysis|
analysis.map do |bar|
@@ -145,11 +147,11 @@
merge(:api_key => @api_key)
end
def get_analysys(method, filename)
get_trackinfo(method, filename) do |response|
- yield response.xml.find('/response/analysis').first
+ yield response.xml.find_first('/response/analysis')
end
end
def get_trackinfo(method, filename, &block)
content = open(filename).read
@@ -173,25 +175,29 @@
end
end
end
def upload(filename)
- content = open(filename).read
-
- request(:upload, :file => content)
+ open(filename) do |f|
+ request(:upload, :file => f)
+ end
end
def request(name, params)
- response_body = @connection.__send__(
- name == :upload ? :post : :get,
- name,
- build_params(params))
+ method = (name == :upload ? 'post' : 'get')
+ response_body = @user_agent.__send__(method + '_content', URI.join(BASE_URL, name.to_s), build_params(params))
response = Response.new(response_body)
unless response.success?
raise Error.new(response.status.message)
end
response
end
+ end
+end
+
+class HTTPClient
+ def agent_name
+ @session_manager.agent_name
end
end