another_echonest_ruby_api 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a953da50f123e3908a36bbc5e89295e0e41649a1
4
- data.tar.gz: 3fcd0f71d3e8cd02afdd903118c5ead6b7d2e655
3
+ metadata.gz: 61d265155ada680cd72b62e0e90835dab4ddacdf
4
+ data.tar.gz: 34cbbbb1d058d59789df2262cddfc37e7071a70f
5
5
  SHA512:
6
- metadata.gz: 097da692d6790e2a975fae1ec5bc5ef92263a325095fd6df60efad9d36d8b26389703a58d21a58015b32f1493e90eab77eb14efe41ddb893cb61b158876d40b9
7
- data.tar.gz: 47db52521f4c139be3b18105c3f5d6f0282f0d7cf00dafb3cc9d73cfad7c090a8e479b6e2090292aeafce6df76772782fe1a96561e11f1c3f9da12794124b7eb
6
+ metadata.gz: 85e4a788d9e80033a28ed64c408cb98c0f850463c481e483d221f3a8d1d1ceefae6f9950467afe86d24923d136ce7f96e8b32f51129870640a3e1ef1b70bcb01
7
+ data.tar.gz: c28e09862fcff0f731c8519f9b8caef6c0ac120d445e1ea8ced3124a1627007ce163f6dc7b5fbd41f63338f51685b69370426f5d97fa64799d1b89d6507e2388
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'rest-client'
4
+ gem 'json'
5
+ gem 'hashie'
6
+
7
+ group :development do
8
+ gem "shoulda", ">= 0"
9
+ gem "rdoc", "~> 3.12"
10
+ gem "bundler"
11
+ gem "jeweler", "~> 1.8.4"
12
+ gem 'webmock'
13
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "another_echonest_ruby_api"
18
+ gem.homepage = "http://github.com/spilliton/another_echonest_ruby_api"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Yet another ruby client for Echonest API}
21
+ gem.description = %Q{Yet another ruby client for Echonest API}
22
+ gem.email = "kloeppingzd@gmail.com"
23
+ gem.authors = ["Zachary Kloepping"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "another_echonest_ruby_api #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,78 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: another_echonest_ruby_api 0.0.2 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "another_echonest_ruby_api"
9
+ s.version = "0.0.2"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.authors = ["Zachary Kloepping"]
13
+ s.date = "2014-01-22"
14
+ s.description = "Yet another ruby client for Echonest API"
15
+ s.email = "kloeppingzd@gmail.com"
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.md"
19
+ ]
20
+ s.files = [
21
+ ".travis.yml",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "another_echonest_ruby_api.gemspec",
28
+ "lib/another_echonest_ruby_api.rb",
29
+ "lib/another_echonest_ruby_api/config.rb",
30
+ "lib/another_echonest_ruby_api/query.rb",
31
+ "lib/another_echonest_ruby_api/request.rb",
32
+ "lib/another_echonest_ruby_api/response.rb",
33
+ "test/config_test.rb",
34
+ "test/helper.rb",
35
+ "test/request_test.rb",
36
+ "test/response_test.rb",
37
+ "test/webmock/bad_credentials.json"
38
+ ]
39
+ s.homepage = "http://github.com/spilliton/another_echonest_ruby_api"
40
+ s.licenses = ["MIT"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = "2.1.10"
43
+ s.summary = "Yet another ruby client for Echonest API"
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 4
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<rest-client>, [">= 0"])
50
+ s.add_runtime_dependency(%q<json>, [">= 0"])
51
+ s.add_runtime_dependency(%q<hashie>, [">= 0"])
52
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
53
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
54
+ s.add_development_dependency(%q<bundler>, [">= 0"])
55
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
56
+ s.add_development_dependency(%q<webmock>, [">= 0"])
57
+ else
58
+ s.add_dependency(%q<rest-client>, [">= 0"])
59
+ s.add_dependency(%q<json>, [">= 0"])
60
+ s.add_dependency(%q<hashie>, [">= 0"])
61
+ s.add_dependency(%q<shoulda>, [">= 0"])
62
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
63
+ s.add_dependency(%q<bundler>, [">= 0"])
64
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
65
+ s.add_dependency(%q<webmock>, [">= 0"])
66
+ end
67
+ else
68
+ s.add_dependency(%q<rest-client>, [">= 0"])
69
+ s.add_dependency(%q<json>, [">= 0"])
70
+ s.add_dependency(%q<hashie>, [">= 0"])
71
+ s.add_dependency(%q<shoulda>, [">= 0"])
72
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
73
+ s.add_dependency(%q<bundler>, [">= 0"])
74
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
75
+ s.add_dependency(%q<webmock>, [">= 0"])
76
+ end
77
+ end
78
+
@@ -0,0 +1,30 @@
1
+ module EchoNest
2
+ autoload :Config, 'another_echonest_ruby_api/config'
3
+ autoload :Request, 'another_echonest_ruby_api/request'
4
+ autoload :Query, 'another_echonest_ruby_api/query'
5
+ autoload :RequestException, 'another_echonest_ruby_api/request'
6
+ autoload :Response, 'another_echonest_ruby_api/response'
7
+
8
+ class ConfigException < Exception
9
+ EXCEPTION_TEXT = <<-eos
10
+ You must setup EchoNest before you can use! Ex:
11
+
12
+ EchoNest.setup do |config|
13
+ config.api_key = 'xxxxxxxx'
14
+ end
15
+ eos
16
+ end
17
+
18
+ def self.config
19
+ if @config
20
+ @config
21
+ else
22
+ raise ConfigException, ConfigException::EXCEPTION_TEXT
23
+ end
24
+ end
25
+
26
+ def self.setup(&block)
27
+ @config = Config.new
28
+ yield @config
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ module EchoNest
2
+ class Config
3
+ attr_accessor :api_key
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ module EchoNest
2
+ class Query
3
+
4
+ def self.search(params)
5
+ response = Request.get(params)
6
+ response.results
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,79 @@
1
+ require 'rest_client'
2
+
3
+ module EchoNest
4
+
5
+ class RequestException < Exception; end
6
+
7
+ class Request
8
+
9
+ BASE_URL = "http://developer.echonest.com/api/v4/"
10
+
11
+ attr_reader :params, :path, :bucket_params
12
+
13
+ def initialize(path, opts={})
14
+ @path = path
15
+ @params = opts
16
+ @bucket_params = if bucket = opts.delete(:bucket)
17
+ bucket.is_a?(Array) ? bucket : [bucket]
18
+ else
19
+ []
20
+ end
21
+ end
22
+
23
+ def self.get(path, opts={})
24
+ request = Request.new(path, opts)
25
+ request.get_response
26
+ end
27
+
28
+ def get_response
29
+ response = RestClient.get(build_url) do |response, request, result, &block|
30
+ # puts "response.code: #{response.code.inspect}"
31
+ # puts "response.body: #{response.body.inspect}"
32
+ if [200, 400].include? response.code
33
+ build_response(response.body)
34
+ else
35
+ raise RequestException, "Unexpected response code: #{response.code}"
36
+ end
37
+ end
38
+
39
+ if response.error?
40
+ raise RequestException, "The following errors were returned: #{response.message.inspect}"
41
+ else
42
+ response
43
+ end
44
+ end
45
+
46
+ def build_response(data)
47
+ Response.new(:raw_json => data.to_str, :request => self)
48
+ end
49
+
50
+ def build_params
51
+ {param_name => params}
52
+ end
53
+
54
+ def build_url
55
+ url = url_with_creds
56
+ if params.any?
57
+ parts = []
58
+ params.each_pair do |name, value|
59
+ parts << "#{URI.escape(name.to_s)}=#{URI.escape(value.to_s)}"
60
+ end
61
+ bucket_params.each do |value|
62
+ parts << "bucket=#{value}"
63
+ end
64
+ url = "#{url}&#{parts.join('&')}"
65
+ end
66
+ # puts "url: #{url.inspect}"
67
+ url
68
+ end
69
+
70
+ def api_key
71
+ EchoNest.config.api_key
72
+ end
73
+
74
+ def url_with_creds
75
+ "#{BASE_URL}#{path}?api_key=#{api_key}&format=json"
76
+ end
77
+
78
+ end
79
+ end
@@ -0,0 +1,33 @@
1
+ require 'json'
2
+ require 'hashie'
3
+
4
+ module EchoNest
5
+ class Response
6
+
7
+ # data attr is a Hashie::Mash you can use to get at json attributes returned
8
+ # ex: response.data.artist.name, response.data.artist.name etc
9
+ attr_reader :request, :raw_json, :data, :status
10
+
11
+ def initialize(opts={})
12
+ @request = opts[:request]
13
+ @raw_json = opts[:raw_json]
14
+ h = JSON.parse(@raw_json)
15
+ @data = Hashie::Mash.new(h['response'])
16
+ @status = data.status
17
+ end
18
+
19
+ def error?
20
+ status.code != 0
21
+ end
22
+
23
+ def message
24
+ status.message
25
+ end
26
+
27
+ def version
28
+ status.version
29
+ end
30
+
31
+
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ require 'helper'
2
+
3
+ class ConfigTest < Test::Unit::TestCase
4
+
5
+ should 'raise exception if not configured' do
6
+ assert_raise EchoNest::ConfigException do
7
+ EchoNest::Request.get(:artist => 'sdf')
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+
14
+ unless ENV['NO_MOCKING']
15
+ require 'webmock/test_unit'
16
+ end
17
+
18
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
19
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
20
+ require 'another_echonest_ruby_api'
21
+
22
+ class Test::Unit::TestCase
23
+
24
+ def setup_test_credentials
25
+ EchoNest.setup do |config|
26
+ config.api_key = 'fart'
27
+ end
28
+ end
29
+
30
+ def mock(name, path_and_query, status=200)
31
+ dir = File.dirname(__FILE__) + "/webmock/#{name}.json"
32
+ file = File.open(dir)
33
+ body = file.read
34
+ url = "#{EchoNest::Request::BASE_URL}#{path_and_query}"
35
+ stub_request(:get, url).to_return(:status => status, :body => body)
36
+ end
37
+
38
+ def teardown
39
+ EchoNest.instance_variable_set(:@config, nil)
40
+ WebMock.reset!
41
+ end
42
+
43
+ end
@@ -0,0 +1,34 @@
1
+ $:.unshift '.';require File.dirname(__FILE__) + '/helper'
2
+
3
+ module EchoNest
4
+ class RequestTest < Test::Unit::TestCase
5
+
6
+ context 'with credentials' do
7
+ setup do
8
+ setup_test_credentials
9
+ end
10
+
11
+ should 'raise if errors in response' do
12
+ mock(:bad_credentials, "artist/profile?api_key=fart&format=json&id=ARZGTK71187B9AC7F5", 400)
13
+ ex = assert_raise(RequestException) do
14
+ Request.get("artist/profile", id: "ARZGTK71187B9AC7F5")
15
+ end
16
+ assert ex.message =~ /Invalid key: Unknown/i
17
+ end
18
+
19
+ should 'correctly build_url' do
20
+ request = Request.new("artist/profile", blah: 'huh', id: 'meh')
21
+ assert_equal 'http://developer.echonest.com/api/v4/artist/profile?api_key=fart&format=json&blah=huh&id=meh', request.build_url
22
+ end
23
+
24
+ should 'correctly build url with single bucket params' do
25
+ request = Request.new("artist/profile", id: 'meh', bucket: 'biographies')
26
+ assert_equal 'http://developer.echonest.com/api/v4/artist/profile?api_key=fart&format=json&id=meh&bucket=biographies', request.build_url
27
+ request = Request.new("artist/profile", id: 'meh', bucket: %w(biographies songs id:spotify-WW))
28
+ assert_equal 'http://developer.echonest.com/api/v4/artist/profile?api_key=fart&format=json&id=meh&bucket=biographies&bucket=songs&bucket=id:spotify-WW', request.build_url
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,15 @@
1
+ $:.unshift '.';require File.dirname(__FILE__) + '/helper'
2
+
3
+ module EchoNest
4
+ class ResponseTest < Test::Unit::TestCase
5
+
6
+ should "create a hashie mash of data" do
7
+ json = '{"response": {"status": {"version": "4.2", "code": 0, "message": "Success"}, "artist": {"id": "ARZGTK71187B9AC7F5", "name": "Eels"}}}'
8
+ response = Response.new(:raw_json => json)
9
+ artist = response.data.artist
10
+ assert_equal 'Eels', artist.name
11
+ assert_equal 'ARZGTK71187B9AC7F5', artist.id
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1 @@
1
+ {"response": {"status": {"version": "4.2", "code": 1, "message": "1|Invalid key: Unknown"}}}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: another_echonest_ruby_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zachary Kloepping
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-19 00:00:00.000000000 Z
11
+ date: 2014-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -130,8 +130,23 @@ extra_rdoc_files:
130
130
  - LICENSE.txt
131
131
  - README.md
132
132
  files:
133
- - README.md
133
+ - .travis.yml
134
+ - Gemfile
134
135
  - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - VERSION
139
+ - another_echonest_ruby_api.gemspec
140
+ - lib/another_echonest_ruby_api.rb
141
+ - lib/another_echonest_ruby_api/config.rb
142
+ - lib/another_echonest_ruby_api/query.rb
143
+ - lib/another_echonest_ruby_api/request.rb
144
+ - lib/another_echonest_ruby_api/response.rb
145
+ - test/config_test.rb
146
+ - test/helper.rb
147
+ - test/request_test.rb
148
+ - test/response_test.rb
149
+ - test/webmock/bad_credentials.json
135
150
  homepage: http://github.com/spilliton/another_echonest_ruby_api
136
151
  licenses:
137
152
  - MIT