Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/echonest-ruby-api.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'echonest-ruby-api/config'
require "echonest-ruby-api/version"
require 'echonest-ruby-api/artist'
require 'echonest-ruby-api/song'
Expand All @@ -6,4 +7,28 @@

module Echonest

class ConfigError < StandardError
ERROR_TEXT = <<-eos
You must setup Echonest before you can use! Ex:

Echonest.setup do |config|
config.api_key = 'xxxx'
end
eos
end

def self.config
if @config
@config
else
raise ConfigError, ConfigError::ERROR_TEXT
end
end

def self.setup(&block)
@config = Config.new
yield @config
end


end
8 changes: 4 additions & 4 deletions lib/echonest-ruby-api/artist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class Artist < Echonest::Base

attr_accessor :id, :name, :foreign_ids

def initialize(api_key, name = nil, foreign_ids = nil, id = nil)
def initialize(name = nil, foreign_ids = nil, id = nil)
@id = id
@name = name
@api_key = api_key
@api_key = Echonest.config.api_key
@foreign_ids = ForeignId.parse_array(foreign_ids) if foreign_ids
end

Expand Down Expand Up @@ -61,7 +61,7 @@ def search(options = {})
options = {name: @name}.merge(options)
artists = []
get_response(options)[:artists].each do |a|
artists << Artist.new(@api_key, a[:name], a[:foreign_ids], a[:id])
artists << Artist.new(a[:name], a[:foreign_ids], a[:id])
end
artists
end
Expand All @@ -77,7 +77,7 @@ def songs
def profile(options = {})
options = {name: @name, id: @id}.merge(options)
artist_data = get_response(options)[:artist]
Artist.new(@api_key, artist_data[:name], artist_data[:foreign_ids], artist_data[:id])
Artist.new(artist_data[:name], artist_data[:foreign_ids], artist_data[:id])
end

def terms(options = {})
Expand Down
5 changes: 5 additions & 0 deletions lib/echonest-ruby-api/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Echonest
class Config
attr_accessor :api_key
end
end
6 changes: 3 additions & 3 deletions lib/echonest-ruby-api/playlist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module Echonest

class Playlist < Echonest::Base

def initialize(api_key, artist)
@api_key = api_key
def initialize(artist)
@api_key = Echonest.config.api_key
@artist = artist
@type = 'artist-radio'
response = get('playlist/dynamic/create',
Expand All @@ -17,7 +17,7 @@ def initialize(api_key, artist)

def next
response = get('playlist/dynamic/next', { session_id: session_id })
artist = Echonest::Artist.new(@api_key, response[:songs].first[:artist_name])
artist = Echonest::Artist.new(response[:songs].first[:artist_name])
{ :artist => artist, :track => response[:songs].first[:title] }
end

Expand Down
18 changes: 11 additions & 7 deletions spec/artist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
describe Echonest::Artist do

def create_valid_artist
@a = Echonest::Artist.new('BNOAEBT3IZYZI6WXI', 'Weezer')
@a = Echonest::Artist.new('Weezer')
end

def create_valid_artist_with_id
@a = Echonest::Artist.new('BNOAEBT3IZYZI6WXI', nil, nil, 'ARH6W4X1187B99274F')
@a = Echonest::Artist.new(nil, nil, 'ARH6W4X1187B99274F')
end


it 'should allow an Artist to have a name' do
a = Echonest::Artist.new('12345', 'Weezer')
a = Echonest::Artist.new('Weezer')
a.name.should eql 'Weezer'
end

describe '#entity_name' do

it 'should always return artist' do
VCR.use_cassette('entity_name') do
a = Echonest::Artist.new('Weezer', '12345')
a = Echonest::Artist.new('12345')
a.entity_name.should eql 'artist'
end
end
Expand All @@ -44,8 +44,12 @@ def create_valid_artist_with_id
end

it 'should deal gracefully with an invalid API key' do
Echonest.setup do |config|
config.api_key = 'Weezer'
end

VCR.use_cassette('invalid_api_key_error') do
a = Echonest::Artist.new('Weezer', 'THISISNOTAKEY')
a = Echonest::Artist.new('THISISNOTAKEY')
expect { a.biographies }.to raise_error(Echonest::Error)
end
end
Expand Down Expand Up @@ -106,7 +110,7 @@ def create_valid_artist_with_id

it 'should allow Artists with whitespace in their names' do
VCR.use_cassette('images_whitespace') do
@a = Echonest::Artist.new('BNOAEBT3IZYZI6WXI', 'Bob Marley')
@a = Echonest::Artist.new('Bob Marley')
@a.images.should be_a Array
end
end
Expand Down Expand Up @@ -202,7 +206,7 @@ def create_valid_artist_with_id

it 'should return an artist profile given an id' do
VCR.use_cassette('profile') do
@a = Echonest::Artist.new('BNOAEBT3IZYZI6WXI', nil, nil, 'ARH6W4X1187B99274F')
@a = Echonest::Artist.new(nil, nil, 'ARH6W4X1187B99274F')
artist = @a.profile
artist.should be_a Echonest::Artist
artist.name.should eq "Radiohead"
Expand Down
21 changes: 21 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'

module Echonest

describe Config do

it 'should raise Error if not configured' do
Echonest.instance_variable_set(:@config, nil)
expect { Echonest.config }.to raise_error(ConfigError)
end

it 'should return the api key after configured' do
Echonest.setup do |config|
config.api_key = 'da_key'
end
Echonest.config.api_key.should eql 'da_key'
end

end

end
8 changes: 4 additions & 4 deletions spec/playlist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

it 'should iniaitlize with an API key and an artist ' do
VCR.use_cassette('playlist') do
seed = Echonest::Playlist.new('BNOAEBT3IZYZI6WXI', 'Weezer')
seed = Echonest::Playlist.new('Weezer')
seed.should be
end
end
Expand All @@ -17,7 +17,7 @@

it 'should set a playlist session ID' do
VCR.use_cassette('playlist') do
seed = Echonest::Playlist.new('BNOAEBT3IZYZI6WXI', 'Weezer')
seed = Echonest::Playlist.new('Weezer')
seed.session_id.should be_a String
end
end
Expand All @@ -28,14 +28,14 @@

it 'should return a Hash object' do
VCR.use_cassette('playlist_next') do
seed = Echonest::Playlist.new('BNOAEBT3IZYZI6WXI', 'Weezer')
seed = Echonest::Playlist.new('Weezer')
seed.next.should be_a Hash
end
end

it 'should include the next Song' do
VCR.use_cassette('playlist_next_2') do
seed = Echonest::Playlist.new('BNOAEBT3IZYZI6WXI', 'Weezer')
seed = Echonest::Playlist.new('Weezer')
seed.next[:artist].should be_a Echonest::Artist
seed.next[:track].should be_a String
end
Expand Down
16 changes: 16 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,25 @@

end

Spec::Runner.configure do |config|
config.before(:each) do
configure_with_vcr_api_key
end
end

VCR.configure do |c|
c.cassette_library_dir = 'fixtures/vcr_cassettes'
c.hook_into :webmock
# TODO: Get rid of this when possible!
c.allow_http_connections_when_no_cassette = true
end



def configure_with_vcr_api_key
Echonest.setup do |config|
config.api_key = 'BNOAEBT3IZYZI6WXI'
end
end