freemusicarchive 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +12 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +52 -0
- data/Rakefile +8 -0
- data/freemusicarchive.gemspec +25 -0
- data/lib/faraday/raise_http_exception.rb +45 -0
- data/lib/freemusicarchive.rb +30 -0
- data/lib/freemusicarchive/client.rb +31 -0
- data/lib/freemusicarchive/client/albums.rb +11 -0
- data/lib/freemusicarchive/client/artists.rb +11 -0
- data/lib/freemusicarchive/client/curators.rb +11 -0
- data/lib/freemusicarchive/client/genres.rb +11 -0
- data/lib/freemusicarchive/client/tracks.rb +11 -0
- data/lib/freemusicarchive/configuration.rb +69 -0
- data/lib/freemusicarchive/error.rb +19 -0
- data/lib/freemusicarchive/request.rb +43 -0
- data/lib/freemusicarchive/version.rb +3 -0
- data/spec/fixtures/albums.xml +36 -0
- data/spec/fixtures/albums_by_artist.xml +2 -0
- data/spec/fixtures/artists.xml +20 -0
- data/spec/fixtures/curators.xml +117 -0
- data/spec/fixtures/genres.xml +2 -0
- data/spec/fixtures/tracks.xml +2 -0
- data/spec/fixtures/tracks_by_artist.xml +2 -0
- data/spec/freemusicarchive/client/albums_spec.rb +53 -0
- data/spec/freemusicarchive/client/artists_spec.rb +33 -0
- data/spec/freemusicarchive/client/curators_spec.rb +34 -0
- data/spec/freemusicarchive/client/genres_spec.rb +33 -0
- data/spec/freemusicarchive/client/tracks_spec.rb +51 -0
- data/spec/freemusicarchive/client_spec.rb +75 -0
- data/spec/freemusicarchive_spec.rb +78 -0
- data/spec/spec_helper.rb +44 -0
- metadata +171 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Liane Nakamura
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# The Free Music Archive Ruby Gem
|
2
|
+
|
3
|
+
A Ruby wrapper for the Free Music Archive API
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'freemusicarchive'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install freemusicarchive
|
18
|
+
|
19
|
+
## Usage Examples
|
20
|
+
|
21
|
+
See the [API Docs](http://freemusicarchive.org/api/docs/) for more information
|
22
|
+
|
23
|
+
require "rubygems"
|
24
|
+
require "freemusicarchive"
|
25
|
+
|
26
|
+
# All methods require authentication by api key.
|
27
|
+
FreeMusicArchive.configure do |config|
|
28
|
+
config.api_key = YOUR_API_KEY
|
29
|
+
end
|
30
|
+
|
31
|
+
# Get a list of all curators
|
32
|
+
puts FreeMusicArchive.curators
|
33
|
+
|
34
|
+
# Get a list of all genres sorted by genre_id
|
35
|
+
puts FreeMusicArchive.genres(:sort_by => "genre_id")
|
36
|
+
|
37
|
+
# Get a list of 10 artists
|
38
|
+
puts FreeMusicArchive.artists(:limit => 10)
|
39
|
+
|
40
|
+
# Get a list albums by Arcade Fire
|
41
|
+
puts FreeMusicArchive.albums(:artist_handle => "Arcade_Fire")
|
42
|
+
|
43
|
+
# Get a list of tracks that are permitted for commercial and remix purposes
|
44
|
+
puts FreeMusicArchive.tracks(:commercial => true, :remix => true)
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/freemusicarchive/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Liane Nakamura"]
|
6
|
+
gem.email = ["idiofox555+github@gmail.com"]
|
7
|
+
gem.description = %q{A ruby wrapper for the Free Music Archive API}
|
8
|
+
gem.summary = %q{The freemusicarchive gem is a wrapper for the Free Music Archive API. It provides simple methods to execute HTTP calls.}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "freemusicarchive"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = FreeMusicArchive::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'faraday', '~> 0.7'
|
19
|
+
gem.add_runtime_dependency('faraday_middleware', '~> 0.8')
|
20
|
+
gem.add_runtime_dependency('hashie', '>= 0.4.0')
|
21
|
+
gem.add_runtime_dependency('multi_xml', '~> 0.4.4')
|
22
|
+
gem.add_development_dependency 'rake'
|
23
|
+
gem.add_development_dependency 'rspec'
|
24
|
+
gem.add_development_dependency('webmock', '~> 1.6')
|
25
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
# @private
|
4
|
+
module FaradayMiddleware
|
5
|
+
# @private
|
6
|
+
class RaiseHttpException < Faraday::Middleware
|
7
|
+
def call(env)
|
8
|
+
@app.call(env).on_complete do |response|
|
9
|
+
case response[:status].to_i
|
10
|
+
when 400
|
11
|
+
raise FreeMusicArchive::BadRequest, error_message_400(response)
|
12
|
+
when 404
|
13
|
+
raise FreeMusicArchive::NotFound, error_message_400(response)
|
14
|
+
when 500
|
15
|
+
raise FreeMusicArchive::InternalServerError, error_message_500(response, "Something is technically wrong.")
|
16
|
+
when 503
|
17
|
+
raise FreeMusicArchive::ServiceUnavailable, error_message_500(response, "Free Music Archive is rate limiting your requests.")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(app)
|
23
|
+
super app
|
24
|
+
@parser = nil
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def error_message_400(response)
|
30
|
+
"#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{error_body(response[:body])}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def error_body(body)
|
34
|
+
if body.nil?
|
35
|
+
nil
|
36
|
+
elsif body['meta'] and body['meta']['error_message'] and not body['meta']['error_message'].empty?
|
37
|
+
": #{body['meta']['error_message']}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def error_message_500(response, body=nil)
|
42
|
+
"#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# Core modules
|
2
|
+
require File.expand_path('../freemusicarchive/client', __FILE__)
|
3
|
+
require File.expand_path('../freemusicarchive/configuration', __FILE__)
|
4
|
+
|
5
|
+
module FreeMusicArchive
|
6
|
+
extend Configuration
|
7
|
+
|
8
|
+
class << self
|
9
|
+
|
10
|
+
# Alias for FreeMusicArchive::Client.new
|
11
|
+
#
|
12
|
+
# @return [FreeMusicArchive::Client]
|
13
|
+
def client (options={})
|
14
|
+
FreeMusicArchive::Client.new(options)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Delegate to FreeMusicArchive::Client
|
18
|
+
def method_missing(method, *args, &block)
|
19
|
+
return super unless client.respond_to?(method)
|
20
|
+
client.send(method, *args, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Delegate to FreeMusicArchive::Client
|
24
|
+
def respond_to?(method)
|
25
|
+
return client.respond_to?(method) || super
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'freemusicarchive/configuration'
|
2
|
+
require 'freemusicarchive/request'
|
3
|
+
|
4
|
+
module FreeMusicArchive
|
5
|
+
class Client
|
6
|
+
Dir[File.expand_path('../client/*.rb', __FILE__)].each{|f| require f}
|
7
|
+
|
8
|
+
include FreeMusicArchive::Request
|
9
|
+
|
10
|
+
include FreeMusicArchive::Client::Albums
|
11
|
+
include FreeMusicArchive::Client::Artists
|
12
|
+
include FreeMusicArchive::Client::Curators
|
13
|
+
include FreeMusicArchive::Client::Genres
|
14
|
+
include FreeMusicArchive::Client::Tracks
|
15
|
+
|
16
|
+
attr_accessor *Configuration::VALID_OPTIONS_KEYS
|
17
|
+
|
18
|
+
# Initializes a new API object
|
19
|
+
#
|
20
|
+
# @param attrs [Hash]
|
21
|
+
# @return [FreeMusicArchive::Client]
|
22
|
+
def initialize(attrs={})
|
23
|
+
attrs = FreeMusicArchive.options.merge(attrs)
|
24
|
+
Configuration::VALID_OPTIONS_KEYS.each do |key|
|
25
|
+
instance_variable_set("@#{key}".to_sym, attrs[key])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require File.expand_path('../version', __FILE__)
|
3
|
+
|
4
|
+
module FreeMusicArchive
|
5
|
+
# Defines constants and methods related to configuration
|
6
|
+
module Configuration
|
7
|
+
# An array of valid keys in the options hash when configuring a {FreeMusicArchive::API}
|
8
|
+
VALID_OPTIONS_KEYS = [
|
9
|
+
:adapter,
|
10
|
+
:api_key,
|
11
|
+
:endpoint,
|
12
|
+
:format,
|
13
|
+
:user_agent,
|
14
|
+
:proxy
|
15
|
+
].freeze
|
16
|
+
|
17
|
+
# The adapter that will be used to connect if none is set
|
18
|
+
#
|
19
|
+
# @note The default faraday adapter is Net::HTTP.
|
20
|
+
DEFAULT_ADAPTER = Faraday.default_adapter
|
21
|
+
|
22
|
+
# By default, don't set an application ID
|
23
|
+
DEFAULT_API_KEY = nil
|
24
|
+
|
25
|
+
# The endpoint that will be used to connect if none is set
|
26
|
+
#
|
27
|
+
# @note There is no reason to use any other endpoint at this time
|
28
|
+
DEFAULT_ENDPOINT = 'http://freemusicarchive.org'.freeze
|
29
|
+
|
30
|
+
# The response format appended to the path and sent in the 'Accept' header if none is set
|
31
|
+
DEFAULT_FORMAT = :xml
|
32
|
+
|
33
|
+
# By default, don't use a proxy server
|
34
|
+
DEFAULT_PROXY = nil
|
35
|
+
|
36
|
+
# The user agent that will be sent to the API endpoint if none is set
|
37
|
+
DEFAULT_USER_AGENT = "Free Music Archive Ruby Gem #{FreeMusicArchive::VERSION}".freeze
|
38
|
+
|
39
|
+
# @private
|
40
|
+
attr_accessor *VALID_OPTIONS_KEYS
|
41
|
+
|
42
|
+
# When this module is extended, set all configuration options to their default values
|
43
|
+
def self.extended(base)
|
44
|
+
base.reset
|
45
|
+
end
|
46
|
+
|
47
|
+
# Convenience method to allow configuration options to be set in a block
|
48
|
+
def configure
|
49
|
+
yield self
|
50
|
+
end
|
51
|
+
|
52
|
+
# Create a hash of options and their values
|
53
|
+
def options
|
54
|
+
options = {}
|
55
|
+
VALID_OPTIONS_KEYS.each{|k| options[k] = send(k)}
|
56
|
+
options
|
57
|
+
end
|
58
|
+
|
59
|
+
# Reset all configuration options to defaults
|
60
|
+
def reset
|
61
|
+
self.adapter = DEFAULT_ADAPTER
|
62
|
+
self.api_key = DEFAULT_API_KEY
|
63
|
+
self.endpoint = DEFAULT_ENDPOINT
|
64
|
+
self.format = DEFAULT_FORMAT
|
65
|
+
self.user_agent = DEFAULT_USER_AGENT
|
66
|
+
self.proxy = DEFAULT_PROXY
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module FreeMusicArchive
|
2
|
+
# Custom error class for rescuing from all Free Music Archive errors
|
3
|
+
class Error < StandardError; end
|
4
|
+
|
5
|
+
# Raised when Free Music Archive returns the HTTP status code 400
|
6
|
+
class BadRequest < Error; end
|
7
|
+
|
8
|
+
# Raised when Free Music Archive returns the HTTP status code 404
|
9
|
+
class NotFound < Error; end
|
10
|
+
|
11
|
+
# Raised when Free Music Archive returns the HTTP status code 500
|
12
|
+
class InternalServerError < Error; end
|
13
|
+
|
14
|
+
# Raised when Free Music Archive returns the HTTP status code 503
|
15
|
+
class ServiceUnavailable < Error; end
|
16
|
+
|
17
|
+
# Raised when a subscription payload hash is invalid
|
18
|
+
class InvalidSignature < Error; end
|
19
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'faraday_middleware'
|
2
|
+
Dir[File.expand_path('../../faraday/*.rb', __FILE__)].each{|f| require f}
|
3
|
+
|
4
|
+
module FreeMusicArchive
|
5
|
+
# Defines HTTP request methods
|
6
|
+
module Request
|
7
|
+
# Perform an HTTP GET request
|
8
|
+
def get(path, options={}, unformatted=false)
|
9
|
+
response = connection.get do |req|
|
10
|
+
req.url(formatted_path(path), options)
|
11
|
+
if api_key != nil
|
12
|
+
req.params['api_key'] = api_key
|
13
|
+
end
|
14
|
+
end
|
15
|
+
response.body.data
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def formatted_path(path)
|
21
|
+
[path, format].compact.join('.')
|
22
|
+
end
|
23
|
+
|
24
|
+
def connection()
|
25
|
+
options = {
|
26
|
+
:headers => {:accept => "application/#{format}; charset=utf-8", :user_agent => user_agent},
|
27
|
+
:proxy => proxy,
|
28
|
+
:ssl => { :verify => false },
|
29
|
+
:url => endpoint,
|
30
|
+
}
|
31
|
+
|
32
|
+
Faraday.new(options) do |builder|
|
33
|
+
#builder.use Faraday::Response::Logger
|
34
|
+
builder.use Faraday::Request::UrlEncoded
|
35
|
+
builder.use FaradayMiddleware::Mashify
|
36
|
+
builder.use FaradayMiddleware::ParseXml
|
37
|
+
builder.adapter(adapter)
|
38
|
+
builder.use FaradayMiddleware::RaiseHttpException
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|