echonest_resource 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in echonest_resource.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # Echonest gem
2
+
3
+ ## Installation
4
+
5
+ Add the gem to your Gemfile
6
+
7
+ ``` ruby
8
+ gem "echonest_resource"
9
+ ```
10
+
11
+ Obtain an API key from echonest and create a file called 'echonest.yml' in your /config directory
12
+
13
+ ``` ruby
14
+ api_key = "YOUR_API_KEY"
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ Right now the gem supports two Echonest resources: Artist and Song
20
+
21
+ ### Artist
22
+
23
+ ``` ruby
24
+ class Artist < ActiveResource::Base
25
+ include EchonestResource::Base
26
+ echonest_resource :artist
27
+ end
28
+ ```
29
+
30
+ ``` ruby
31
+ class Artist < ActiveResource::Base
32
+ include EchonestResource::Base
33
+ echonest_resource :song
34
+ end
35
+ ```
36
+
37
+ Now you can search for an artist:
38
+
39
+ ``` ruby
40
+ artists = Artist.search("Macklemore")
41
+ ```
42
+
43
+ The search method also accepts an options hash where you can specify things like: results, bucket, sort
44
+
45
+
46
+ ``` ruby
47
+ artists = Artist.search("Macklemore", {:results => 20, :sort => "hotttnesss-desc"})
48
+ ```
49
+
50
+ ### Song
51
+
52
+ You can search for songs for an artist name
53
+
54
+ ``` ruby
55
+ songs = Song.find_by_artist_name("Macklemore")
56
+ ```
57
+
58
+ The find_by_artist_name method also accepts an options hash
59
+
60
+ ``` ruby
61
+ songs = Song.find_by_artist_name("Macklemore", :sort => "song_hotttnesss")
62
+ ```
63
+
64
+ You can retrieve additional information by specifying a bucket like so:
65
+
66
+ ``` ruby
67
+ songs = Song.find_by_artist_name("Macklemore", :sort => "duration-desc", :bucket => "audio_summary")
68
+ ```
69
+
70
+
71
+ All the attributes on the songs use the same name as the Echonest API (http://developer.echonest.com/docs/v4)
72
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "echonest_resource/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "echonest_resource"
7
+ s.version = EchonestResource::VERSION
8
+ s.authors = ["tzumby"]
9
+ s.email = ["tzumby@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Echonest API Wrapper}
12
+ s.description = %q{Use this gem to search the Echonest API for Artists and Songs}
13
+
14
+ s.rubyforge_project = "echonest_resource"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ s.add_runtime_dependency "httparty"
24
+ s.add_runtime_dependency "hashie"
25
+ end
@@ -0,0 +1,75 @@
1
+ module EchonestResource
2
+ module Base
3
+
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def request_params
11
+ {
12
+ :api_key => EchonestResource.api_key,
13
+ :results => 1
14
+ }
15
+ end
16
+
17
+ def echonest_resource(resource)
18
+ raise StandardError if resource.blank?
19
+
20
+ include HTTParty
21
+ base_uri 'http://developer.echonest.com/api/v4'
22
+ format :json
23
+
24
+ case resource
25
+ when :artist
26
+ instance_eval <<-EOV
27
+ def search(name, options={})
28
+ raise StandardError if name.blank?
29
+
30
+ results = Hashie::Mash.new(self.get('/artist/search', :query =>
31
+ request_params.merge({:name => name}).merge(options)
32
+ ))
33
+ if results.response && results.response.artists; results.response.artists; end;
34
+ end
35
+ EOV
36
+ when :song
37
+ instance_eval <<-EOV
38
+
39
+ def find_by_artist_id(id, options={})
40
+ raise StandardError if id.blank?
41
+
42
+ results = Hashie::Mash.new(self.get('/song/search', :query =>
43
+ request_params.merge(options).merge({
44
+ :artist_id => id
45
+ })
46
+ ))
47
+ if results.response && results.response.songs; results.response.songs; end;
48
+ end
49
+
50
+ def find_by_artist_name(name, options={})
51
+ raise StandardError if name.blank?
52
+
53
+ if artist = Artist.search(:name => name)
54
+ Song.find_by_id(artist.first.id, options)
55
+ end
56
+ end
57
+
58
+ def search(title, options={})
59
+ raise StandardError if title.blank?
60
+
61
+ results = Hashie::Mash.new(self.get('/song/search', :query =>
62
+ request_params.merge({:title => title}).merge(options)
63
+ ))
64
+ results
65
+ end
66
+
67
+
68
+ EOV
69
+ end
70
+
71
+ end
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module EchonestResource
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ require "echonest_resource/version"
2
+ require 'active_support'
3
+ require 'httparty'
4
+ require 'hashie'
5
+
6
+ class APIKeyMissing < StandardError; end
7
+ class MissingConfigFile < StandardError; end
8
+
9
+ module EchonestResource
10
+ def self.api_key
11
+ echonest_config = YAML.load_file("#{Rails.root}/config/echonest.yml")
12
+ raise MissingConfigFile if echonest_config.blank?
13
+ echonest_config["api_key"]
14
+ end
15
+ end
16
+
17
+ directory = File.expand_path(File.dirname(__FILE__))
18
+
19
+ require File.join(directory, 'echonest_resource', 'base')
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: echonest_resource
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - tzumby
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: &70308245661760 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70308245661760
25
+ - !ruby/object:Gem::Dependency
26
+ name: hashie
27
+ requirement: &70308245660520 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70308245660520
36
+ description: Use this gem to search the Echonest API for Artists and Songs
37
+ email:
38
+ - tzumby@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - README.md
46
+ - Rakefile
47
+ - echonest_resource.gemspec
48
+ - lib/echonest_resource.rb
49
+ - lib/echonest_resource/base.rb
50
+ - lib/echonest_resource/version.rb
51
+ homepage: ''
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project: echonest_resource
71
+ rubygems_version: 1.8.11
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Echonest API Wrapper
75
+ test_files: []