ruby-echonest 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/echonest.rb CHANGED
@@ -10,7 +10,10 @@ require 'echonest/element/segment'
10
10
  require 'echonest/element/loudness'
11
11
  require 'echonest/element/tatum'
12
12
 
13
+ require 'pathname'
14
+
13
15
  def Echonest(api_key) Echonest::Api.new(api_key) end
14
16
 
15
17
  module Echonest
18
+ DIRECTORY = Pathname.new("~/.echonest").expand_path
16
19
  end
@@ -3,8 +3,11 @@ require 'open-uri'
3
3
 
4
4
  module Echonest
5
5
  class Analysis
6
+ attr_reader :json
7
+
6
8
  def initialize(json)
7
- @body = JSON.parse(json)
9
+ @json = json
10
+ @body = JSON.parse(@json)
8
11
  end
9
12
 
10
13
  def self.new_from_url(url)
data/lib/echonest/api.rb CHANGED
@@ -2,6 +2,7 @@
2
2
  require 'digest/md5'
3
3
  require 'httpclient'
4
4
  require 'json'
5
+ require 'fileutils'
5
6
 
6
7
  module Echonest
7
8
  class Api
@@ -104,13 +105,23 @@ module Echonest
104
105
  end
105
106
 
106
107
  def analysis(filename)
107
- analysis_url = analysis_url(filename)
108
- Analysis.new_from_url(analysis_url)
109
- end
110
-
111
- def analysis_url(filename)
112
108
  md5 = Digest::MD5.hexdigest(open(filename).read)
109
+ cache_path = DIRECTORY + 'analysis' + md5
110
+
111
+ if File.exists?(cache_path)
112
+ analysis = Analysis.new(open(cache_path).read)
113
+ else
114
+ analysis = Analysis.new_from_url(analysis_url(filename, md5))
115
+ cache_path.dirname.mkpath
116
+ open(cache_path, 'w') do |file|
117
+ file.write analysis.json
118
+ end
119
+ end
120
+
121
+ analysis
122
+ end
113
123
 
124
+ def analysis_url(filename, md5)
114
125
  while true
115
126
  begin
116
127
  response = profile(:md5 => md5)
@@ -1,3 +1,3 @@
1
1
  module Echonest
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
data/spec/track_spec.rb CHANGED
@@ -1,11 +1,17 @@
1
1
  $:.unshift File.dirname(__FILE__)
2
2
 
3
3
  require 'spec_helper'
4
+ require 'fileutils'
4
5
 
5
6
  describe Echonest::ApiMethods::Track do
6
7
  before do
7
8
  @api = Echonest::Api.new('8TPE3VC60ODJTNTFE')
8
9
  @track = Echonest::ApiMethods::Track.new(@api)
10
+ @cache_path = File.expand_path('~/.echonest/analysis/c2ee3cfddf1eb8631a928a4f662b587c')
11
+
12
+ if File.exists?(@cache_path)
13
+ FileUtils.rm @cache_path
14
+ end
9
15
  end
10
16
 
11
17
  describe '#profile' do
@@ -86,17 +92,38 @@ describe Echonest::ApiMethods::Track do
86
92
  with(:filename => fixture('sample.mp3')).
87
93
  and_return(Echonest::Response.new(open(fixture('profile.json')).read))
88
94
 
89
- @track.analysis_url(fixture('sample.mp3')).
95
+ @track.analysis_url(fixture('sample.mp3'), 'c2ee3cfddf1eb8631a928a4f662b587c').
90
96
  should eql('https://echonest-analysis.s3.amazonaws.com:443/TR/TRXXHTJ1294CD8F3B3/3/full.json?Signature=GQMyRUdcO5MUPSHZej72oQHOg3g%3D&Expires=1278605254&AWSAccessKeyId=AKIAJTEJGOTDLQY2E77A')
91
97
  end
92
98
  end
93
99
 
94
100
  describe '#analysis' do
101
+ before do
102
+ @analysis = Echonest::Analysis.new(open(fixture('analysis.json')).read)
103
+ @analysis_url = 'https://echonest-analysis.s3.amazonaws.com:443/TR/TRXXHTJ1294CD8F3B3/3/full.json?Signature=GQMyRUdcO5MUPSHZej72oQHOg3g%3D&Expires=1278605254&AWSAccessKeyId=AKIAJTEJGOTDLQY2E77A'
104
+ end
105
+
95
106
  it 'should return analysis' do
96
- analysis_url = 'https://echonest-analysis.s3.amazonaws.com:443/TR/TRXXHTJ1294CD8F3B3/3/full.json?Signature=GQMyRUdcO5MUPSHZej72oQHOg3g%3D&Expires=1278605254&AWSAccessKeyId=AKIAJTEJGOTDLQY2E77A'
97
- @track.should_receive(:analysis_url).with(fixture('sample.mp3')).and_return(analysis_url)
98
- Echonest::Analysis.should_receive(:new_from_url).with(analysis_url)
107
+ @track.should_receive(:analysis_url).with(fixture('sample.mp3'), 'c2ee3cfddf1eb8631a928a4f662b587c').and_return(@analysis_url)
108
+ Echonest::Analysis.should_receive(:new_from_url).with(@analysis_url).and_return(@analysis)
109
+
110
+ @track.analysis(fixture('sample.mp3'))
111
+ end
112
+
113
+ it 'should cache analysis' do
114
+ File.exist?(@cache_path).should_not be
115
+
116
+ # 1st (not cached)
117
+ @track.should_receive(:analysis_url).with(fixture('sample.mp3'), 'c2ee3cfddf1eb8631a928a4f662b587c').and_return(@analysis_url)
118
+ Echonest::Analysis.should_receive(:new_from_url).with(@analysis_url).and_return(@analysis)
119
+ @track.analysis(fixture('sample.mp3'))
120
+
121
+ File.exist?(@cache_path).should be
122
+ open(@cache_path).read.should eql(open(fixture('analysis.json')).read)
99
123
 
124
+ # 2nd (cached)
125
+ @track.should_not_receive(:analysis_url)
126
+ Echonest::Analysis.should_receive(:new).with(open(@cache_path).read).and_return(@analysis)
100
127
  @track.analysis(fixture('sample.mp3'))
101
128
  end
102
129
  end
metadata CHANGED
@@ -1,68 +1,82 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ruby-echonest
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.2
5
- prerelease:
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
6
10
  platform: ruby
7
- authors:
11
+ authors:
8
12
  - youpy
9
13
  autorequire:
10
14
  bindir: bin
11
15
  cert_chain: []
12
- date: 2011-11-07 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
16
+
17
+ date: 2011-12-30 00:00:00 +09:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: rspec
16
- requirement: &70182523091860 !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ - 0
21
31
  version: 2.0.0
22
32
  type: :development
23
- prerelease: false
24
- version_requirements: *70182523091860
25
- - !ruby/object:Gem::Dependency
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
26
35
  name: httpclient
27
- requirement: &70182523106840 !ruby/object:Gem::Requirement
28
- none: false
29
- requirements:
30
- - - ! '>='
31
- - !ruby/object:Gem::Version
32
- version: '0'
33
- type: :runtime
34
36
  prerelease: false
35
- version_requirements: *70182523106840
36
- - !ruby/object:Gem::Dependency
37
- name: hashie
38
- requirement: &70182523106220 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ! '>='
42
- - !ruby/object:Gem::Version
43
- version: '0'
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
44
  type: :runtime
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: hashie
45
48
  prerelease: false
46
- version_requirements: *70182523106220
47
- - !ruby/object:Gem::Dependency
48
- name: libxml-ruby
49
- requirement: &70182523105580 !ruby/object:Gem::Requirement
50
- none: false
51
- requirements:
52
- - - ! '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
55
56
  type: :runtime
57
+ version_requirements: *id003
58
+ - !ruby/object:Gem::Dependency
59
+ name: libxml-ruby
56
60
  prerelease: false
57
- version_requirements: *70182523105580
61
+ requirement: &id004 !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ type: :runtime
69
+ version_requirements: *id004
58
70
  description: An Ruby interface for Echo Nest Developer API
59
71
  email: youpy@buycheapviagraonlinenow.com
60
72
  executables: []
73
+
61
74
  extensions: []
62
- extra_rdoc_files:
75
+
76
+ extra_rdoc_files:
63
77
  - ChangeLog
64
78
  - README.rdoc
65
- files:
79
+ files:
66
80
  - .rspec
67
81
  - ChangeLog
68
82
  - README.rdoc
@@ -91,28 +105,35 @@ files:
91
105
  - spec/response_spec.rb
92
106
  - spec/spec_helper.rb
93
107
  - spec/track_spec.rb
108
+ has_rdoc: true
94
109
  homepage: http://github.com/youpy/ruby-echonest
95
110
  licenses: []
111
+
96
112
  post_install_message:
97
113
  rdoc_options: []
98
- require_paths:
114
+
115
+ require_paths:
99
116
  - lib
100
- required_ruby_version: !ruby/object:Gem::Requirement
101
- none: false
102
- requirements:
103
- - - ! '>='
104
- - !ruby/object:Gem::Version
105
- version: '0'
106
- required_rubygems_version: !ruby/object:Gem::Requirement
107
- none: false
108
- requirements:
109
- - - ! '>='
110
- - !ruby/object:Gem::Version
111
- version: '0'
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ segments:
129
+ - 0
130
+ version: "0"
112
131
  requirements: []
132
+
113
133
  rubyforge_project:
114
- rubygems_version: 1.8.10
134
+ rubygems_version: 1.3.6
115
135
  signing_key:
116
136
  specification_version: 3
117
137
  summary: An Ruby interface for Echo Nest Developer API
118
138
  test_files: []
139
+