Class: Echonest::Api
- Inherits:
-
Object
- Object
- Echonest::Api
- Defined in:
- lib/echonest/api.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
'3'
- URL =
'http://developer.echonest.com/api/'
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
- #build_params(params) ⇒ Object
- #get_analysys(method, filename) ⇒ Object
- #get_bars(filename) ⇒ Object
- #get_beats(filename) ⇒ Object
- #get_duration(filename) ⇒ Object
- #get_end_of_fade_in(filename) ⇒ Object
- #get_key(filename) ⇒ Object
- #get_loudness(filename) ⇒ Object
- #get_metadata(filename) ⇒ Object
- #get_mode(filename) ⇒ Object
- #get_sections(filename) ⇒ Object
- #get_segments(filename) ⇒ Object
- #get_start_of_fade_out(filename) ⇒ Object
- #get_tatums(filename) ⇒ Object
- #get_tempo(filename) ⇒ Object
- #get_time_signature(filename) ⇒ Object
- #get_trackinfo(method, filename, &block) ⇒ Object
-
#initialize(api_key) ⇒ Api
constructor
A new instance of Api.
- #request(name, params) ⇒ Object
- #upload(filename) ⇒ Object
Constructor Details
#initialize(api_key) ⇒ Api
Returns a new instance of Api.
12 13 14 15 |
# File 'lib/echonest/api.rb', line 12 def initialize(api_key) @api_key = api_key @connection = Connection.new(URL) end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
10 11 12 |
# File 'lib/echonest/api.rb', line 10 def connection @connection end |
Instance Method Details
#build_params(params) ⇒ Object
142 143 144 145 146 |
# File 'lib/echonest/api.rb', line 142 def build_params(params) params = params. merge(:version => VERSION). merge(:api_key => @api_key) end |
#get_analysys(method, filename) ⇒ Object
148 149 150 151 152 |
# File 'lib/echonest/api.rb', line 148 def get_analysys(method, filename) get_trackinfo(method, filename) do |response| yield response.xml.find('/response/analysis').first end end |
#get_bars(filename) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/echonest/api.rb', line 17 def (filename) get_analysys(:get_bars, filename) do |analysis| analysis.map do || Bar.new(.content.to_f, ['confidence'].to_f) end end end |
#get_beats(filename) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/echonest/api.rb', line 25 def get_beats(filename) get_analysys(:get_beats, filename) do |analysis| analysis.map do |beat| Beat.new(beat.content.to_f, beat['confidence'].to_f) end end end |
#get_duration(filename) ⇒ Object
83 84 85 86 87 |
# File 'lib/echonest/api.rb', line 83 def get_duration(filename) get_analysys(:get_duration, filename) do |analysis| analysis.first.content.to_f end end |
#get_end_of_fade_in(filename) ⇒ Object
89 90 91 92 93 |
# File 'lib/echonest/api.rb', line 89 def get_end_of_fade_in(filename) get_analysys(:get_end_of_fade_in, filename) do |analysis| analysis.first.content.to_f end end |
#get_key(filename) ⇒ Object
95 96 97 98 99 |
# File 'lib/echonest/api.rb', line 95 def get_key(filename) get_analysys(:get_key, filename) do |analysis| ValueWithConfidence.new(analysis.first.content.to_i, analysis.first['confidence'].to_f) end end |
#get_loudness(filename) ⇒ Object
101 102 103 104 105 |
# File 'lib/echonest/api.rb', line 101 def get_loudness(filename) get_analysys(:get_loudness, filename) do |analysis| analysis.first.content.to_f end end |
#get_metadata(filename) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/echonest/api.rb', line 107 def (filename) get_analysys(:get_metadata, filename) do |analysis| analysis.inject({}) do |memo, key| memo[key.name] = key.content memo end end end |
#get_mode(filename) ⇒ Object
116 117 118 119 120 |
# File 'lib/echonest/api.rb', line 116 def get_mode(filename) get_analysys(:get_mode, filename) do |analysis| ValueWithConfidence.new(analysis.first.content.to_i, analysis.first['confidence'].to_f) end end |
#get_sections(filename) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/echonest/api.rb', line 72 def get_sections(filename) get_analysys(:get_sections, filename) do |analysis| analysis.map do |section| Section.new( section['start'].to_f, section['duration'].to_f ) end end end |
#get_segments(filename) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/echonest/api.rb', line 33 def get_segments(filename) get_analysys(:get_segments, filename) do |analysis| analysis.map do |segment| max_loudness = loudness = nil segment.find('loudness/dB').map do |db| if db['type'] == 'max' max_loudness = Loudness.new(db['time'].to_f, db.content.to_f) else loudness = Loudness.new(db['time'].to_f, db.content.to_f) end end pitches = segment.find('pitches/pitch').map do |pitch| pitch.content.to_f end timbre = segment.find('timbre/coeff').map do |coeff| coeff.content.to_f end Segment.new( segment['start'].to_f, segment['duration'].to_f, loudness, max_loudness, pitches, timbre ) end end end |
#get_start_of_fade_out(filename) ⇒ Object
122 123 124 125 126 |
# File 'lib/echonest/api.rb', line 122 def get_start_of_fade_out(filename) get_analysys(:get_start_of_fade_out, filename) do |analysis| analysis.first.content.to_f end end |
#get_tatums(filename) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/echonest/api.rb', line 128 def get_tatums(filename) get_analysys(:get_tatums, filename) do |analysis| analysis.map do |tatum| Tatum.new(tatum.content.to_f, tatum['confidence'].to_f) end end end |
#get_tempo(filename) ⇒ Object
66 67 68 69 70 |
# File 'lib/echonest/api.rb', line 66 def get_tempo(filename) get_analysys(:get_tempo, filename) do |analysis| analysis.first.content.to_f end end |
#get_time_signature(filename) ⇒ Object
136 137 138 139 140 |
# File 'lib/echonest/api.rb', line 136 def get_time_signature(filename) get_analysys(:get_time_signature, filename) do |analysis| ValueWithConfidence.new(analysis.first.content.to_i, analysis.first['confidence'].to_f) end end |
#get_trackinfo(method, filename, &block) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/echonest/api.rb', line 154 def get_trackinfo(method, filename, &block) content = open(filename).read md5 = Digest::MD5.hexdigest(content) begin response = request(method, :md5 => md5) block.call(response) rescue Error => e if e. == 'Invalid parameter: unknown MD5 file hash' upload(filename) sleep 60 # wait for serverside analysis get_trackinfo(method, filename, &block) else raise end end end |
#request(name, params) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/echonest/api.rb', line 179 def request(name, params) response_body = @connection.__send__( name == :upload ? :post : :get, name, build_params(params)) response = Response.new(response_body) unless response.success? raise Error.new(response.status.) end response end |
#upload(filename) ⇒ Object
173 174 175 176 177 |
# File 'lib/echonest/api.rb', line 173 def upload(filename) content = open(filename).read request(:upload, :file => content) end |