lib/echonest/api.rb in ruby-echonest-0.2.0 vs lib/echonest/api.rb in ruby-echonest-0.3.0
- old
+ new
@@ -1,10 +1,11 @@
# -*- coding: utf-8 -*-
require 'digest/md5'
require 'httpclient'
require 'json'
require 'fileutils'
+require 'cgi'
module Echonest
class Api
BASE_URL = 'http://developer.echonest.com/api/v4/'
USER_AGENT = '%s/%s' % ['ruby-echonest', ::Echonest::VERSION]
@@ -13,13 +14,17 @@
class Error < StandardError; end
attr_reader :user_agent
- def initialize(api_key)
- @api_key = api_key
+ def initialize(api_key = nil)
+ @api_key = api_key || read_api_key_from_file
@user_agent = HTTPClient.new(:agent_name => USER_AGENT)
+
+ # for big files
+ @user_agent.send_timeout = 60 * 30
+ @user_agent.receive_timeout = 60 * 10
end
def track
ApiMethods::Track.new(self)
end
@@ -33,11 +38,11 @@
def request(name, method, params, file = nil)
if file
query = build_params(params).sort_by do |param|
param[0].to_s
end.inject([]) do |m, param|
- m << [URI.encode(param[0].to_s), URI.encode(param[1])].join('=')
+ m << [CGI.escape(param[0].to_s), CGI.escape(param[1])].join('=')
end.join('&')
uri = URI.join(BASE_URL, name.to_s)
uri.query = query
@@ -61,9 +66,19 @@
end
response
rescue HTTPClient::BadResponseError => e
raise Error.new('%s: %s' % [name, e.message])
+ end
+
+ def self.api_key_file
+ DIRECTORY + 'api_key'
+ end
+
+ private
+
+ def read_api_key_from_file
+ self.class.api_key_file.read.chomp
end
end
module ApiMethods
class Base