youpy-ruby-echonest 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,5 @@
1
1
  require 'echonest/version'
2
2
  require 'echonest/api'
3
- require 'echonest/connection'
4
3
  require 'echonest/response'
5
4
  require 'echonest/element/value_with_confidence'
6
5
  require 'echonest/element/bar'
@@ -1,17 +1,19 @@
1
1
  require 'digest/md5'
2
+ require 'httpclient'
2
3
 
3
4
  module Echonest
4
5
  class Api
5
6
  VERSION = '3'
6
- URL = 'http://developer.echonest.com/api/'
7
+ BASE_URL = 'http://developer.echonest.com/api/'
8
+ USER_AGENT = '%s/%s' % ['ruby-echonest', ::Echonest::VERSION]
7
9
 
8
10
  class Error < StandardError; end
9
11
 
10
- attr_reader :connection
12
+ attr_reader :user_agent
11
13
 
12
14
  def initialize(api_key)
13
15
  @api_key = api_key
14
- @connection = Connection.new(URL)
16
+ @user_agent = HTTPClient.new(:agent_name => USER_AGENT)
15
17
  end
16
18
 
17
19
  def get_bars(filename)
@@ -147,7 +149,7 @@ module Echonest
147
149
 
148
150
  def get_analysys(method, filename)
149
151
  get_trackinfo(method, filename) do |response|
150
- yield response.xml.find('/response/analysis').first
152
+ yield response.xml.find_first('/response/analysis')
151
153
  end
152
154
  end
153
155
 
@@ -175,16 +177,14 @@ module Echonest
175
177
  end
176
178
 
177
179
  def upload(filename)
178
- content = open(filename).read
179
-
180
- request(:upload, :file => content)
180
+ open(filename) do |f|
181
+ request(:upload, :file => f)
182
+ end
181
183
  end
182
184
 
183
185
  def request(name, params)
184
- response_body = @connection.__send__(
185
- name == :upload ? :post : :get,
186
- name,
187
- build_params(params))
186
+ method = (name == :upload ? 'post' : 'get')
187
+ response_body = @user_agent.__send__(method + '_content', URI.join(BASE_URL, name.to_s), build_params(params))
188
188
  response = Response.new(response_body)
189
189
 
190
190
  unless response.success?
@@ -195,3 +195,9 @@ module Echonest
195
195
  end
196
196
  end
197
197
  end
198
+
199
+ class HTTPClient
200
+ def agent_name
201
+ @session_manager.agent_name
202
+ end
203
+ end
@@ -1,3 +1,3 @@
1
1
  module Echonest
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -209,7 +209,11 @@ EOM
209
209
  time_signature.value.should eql(4)
210
210
  end
211
211
 
212
+ it "should make http request with agent name" do
213
+ @api.user_agent.agent_name.should eql('ruby-echonest/' + Echonest::VERSION)
214
+ end
215
+
212
216
  def make_connection_stub(xml)
213
- @api.connection.stub!(:request).and_return(xml)
217
+ @api.user_agent.stub!(:get_content).and_return(xml)
214
218
  end
215
219
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: youpy-ruby-echonest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - youpy
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-31 00:00:00 -07:00
12
+ date: 2009-09-23 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -36,7 +36,6 @@ files:
36
36
  - ChangeLog
37
37
  - Rakefile
38
38
  - spec/api_spec.rb
39
- - spec/connection_spec.rb
40
39
  - spec/echonest_spec.rb
41
40
  - spec/fixtures
42
41
  - spec/fixtures/get_bars.xml
@@ -59,7 +58,6 @@ files:
59
58
  - spec/spec_helper.rb
60
59
  - lib/echonest
61
60
  - lib/echonest/api.rb
62
- - lib/echonest/connection.rb
63
61
  - lib/echonest/element
64
62
  - lib/echonest/element/bar.rb
65
63
  - lib/echonest/element/beat.rb
@@ -1,89 +0,0 @@
1
- require 'cgi'
2
- require 'net/https'
3
-
4
- module Echonest
5
- class Connection
6
- def initialize(base_url)
7
- @base_url = base_url
8
- end
9
-
10
- def get(resource, args = nil)
11
- url = url(resource.to_s)
12
-
13
- if args
14
- url.query = query(args)
15
- end
16
-
17
- req = make_request(url, 'get')
18
-
19
- request(req, url)
20
- end
21
-
22
- def post(resource, args = nil)
23
- url = url(resource.to_s)
24
- req = make_request(url, 'post')
25
-
26
- if args
27
- data = post_data(args)
28
- req['Content-Length'] = data.size.to_s
29
- req['Content-Type'] = "multipart/form-data; boundary=#{boundary}"
30
- end
31
-
32
- request(req, url, data)
33
- end
34
-
35
- def url(path)
36
- URI.join(@base_url, path)
37
- end
38
-
39
- def post_data(params)
40
- data = params.inject([]) do |memo, param|
41
- name, value = param
42
-
43
- memo << "--#{boundary}"
44
-
45
- if name.to_s == 'file'
46
- memo << "Content-Disposition: form-data; name=\"#{name}\"; filename=\"file.mp3\""
47
- memo << "Content-Type: application/octet-stream"
48
- else
49
- memo << "Content-Disposition: form-data; name=\"#{name}\""
50
- end
51
-
52
- memo << ''
53
- memo << value
54
- end
55
-
56
- data << "--#{boundary}--"
57
- data << ''
58
-
59
- data.join("\r\n")
60
- end
61
-
62
- def boundary
63
- '----BOUNDARYBOUNDARY----'
64
- end
65
-
66
- def request(req, url, data = nil)
67
- http = Net::HTTP.new(url.host, url.port)
68
- http.use_ssl = (url.port == 443)
69
-
70
- res = http.start() { |conn| conn.request(req, data) }
71
- res.body
72
- end
73
-
74
- def query(params)
75
- params.map { |k,v| "%s=%s" % [CGI.escape(k.to_s), CGI.escape(v.to_s)] }.join("&")
76
- end
77
-
78
- def make_request(uri, method)
79
- req = (method == 'post' ? Net::HTTP::Post : Net::HTTP::Get).new(uri.request_uri)
80
- req['User-Agent'] = user_agent
81
-
82
- req
83
- end
84
-
85
- def user_agent
86
- '%s/%s' % ['ruby-echonest', VERSION]
87
- end
88
- end
89
- end
@@ -1,19 +0,0 @@
1
- $:.unshift File.dirname(__FILE__)
2
-
3
- require 'spec_helper'
4
- require "echonest"
5
-
6
- include SpecHelper
7
-
8
- describe Echonest::Connection do
9
- before do
10
- @connection = Echonest::Connection.new(Echonest::Api::URL)
11
- end
12
-
13
- it "should make http request with user agent" do
14
- req = @connection.make_request(URI('http://example.com/xxx'), 'get')
15
-
16
- req.method.should eql('GET')
17
- req['User-Agent'].should eql('ruby-echonest/' + Echonest::VERSION)
18
- end
19
- end