nokia_music 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nokia_music.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 David Anderson
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.
@@ -0,0 +1,43 @@
1
+ # NokiaMusic
2
+
3
+ A simple ruby wrapper around the Nokia Music API
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'nokia_music'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install nokia_music
18
+
19
+ ## Usage
20
+
21
+ Set your client_id:
22
+
23
+ NokiaMusic.client_id = ""
24
+
25
+ Search for music:
26
+
27
+ tracks = NokiaMusic.search("Knights of Cydonia", NokiaMusic::Product::TRACK)
28
+
29
+ Get either an artist, album, track, or single from an ID
30
+
31
+ product = NokiaMusic.product(tracks.items.first.id)
32
+
33
+ Get a track sample from an ID:
34
+
35
+ preview = NokiaMusic.product_sample(id)
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,34 @@
1
+ require 'faraday_middleware'
2
+ require "nokia_music/version"
3
+ require 'nokia_music/configuration'
4
+ require 'nokia_music/client'
5
+
6
+ module NokiaMusic
7
+ extend Configuration
8
+
9
+ mattr_accessor :client_id
10
+
11
+ # Alias for NokiaMusic::Client.new
12
+ #
13
+ # @return [NokiaMusic::Client]
14
+ def self.client(options={})
15
+ NokiaMusic::Client.new(options)
16
+ end
17
+
18
+ # Alias for NokiaMusic::Client.new
19
+ #
20
+ # @return [NokiaMusic::Client]
21
+ def self.new(options={})
22
+ NokiaMusic::Client.new(options)
23
+ end
24
+
25
+ # Delegate to NokiaMusic::Client
26
+ def self.method_missing(method, *args, &block)
27
+ return super unless new.respond_to?(method)
28
+ new.send(method, *args, &block)
29
+ end
30
+
31
+ def self.respond_to?(method, include_private = false)
32
+ new.respond_to?(method, include_private) || super(method, include_private)
33
+ end
34
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path('../request', __FILE__)
2
+
3
+ module NokiaMusic
4
+ class Client
5
+
6
+ # @private
7
+ attr_accessor *Configuration::VALID_OPTIONS_KEYS
8
+
9
+ # Creates a new Client
10
+ def initialize(options={})
11
+ options = NokiaMusic.options.merge(options)
12
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
13
+ send("#{key}=", options[key])
14
+ end
15
+ end
16
+
17
+ Dir[File.expand_path('../client/*.rb', __FILE__)].each{|f| require f}
18
+
19
+ alias :api_endpoint :endpoint
20
+
21
+ include Request
22
+ include Search
23
+ include Product
24
+ include ProductSample
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ module NokiaMusic
2
+ module Product
3
+
4
+ ARTIST = "artist"
5
+ ALBUM = "album"
6
+ SINGLE = "single"
7
+ TRACK = "track"
8
+
9
+ def product(id, options={})
10
+ params = {}
11
+ params.merge!(options)
12
+
13
+ # clear empty key/value pairs
14
+ params.reject! { |key, value| value.nil? }
15
+
16
+ request("products/#{id}", params)
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ module NokiaMusic
2
+ module ProductSample
3
+
4
+ def product_sample(product_id)
5
+ request = construct_request("products/#{product_id}/sample")
6
+ Hashie::Mash.new(product_id: product_id, url: "#{request[:url]}?#{request[:params].to_query}")
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,15 @@
1
+ module NokiaMusic
2
+ module Search
3
+
4
+ def search(query, category, options={})
5
+ params = {q: query, category: category}
6
+ params.merge!(options)
7
+
8
+ # clear empty key/value pairs
9
+ params.reject! { |key, value| value.nil? }
10
+
11
+ request('', params)
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,66 @@
1
+ require 'faraday'
2
+ require File.expand_path('../version', __FILE__)
3
+
4
+ module NokiaMusic
5
+ # Defines constants and methods related to configuration
6
+ module Configuration
7
+
8
+ # An array of valid keys in the options hash when configuring
9
+ VALID_OPTIONS_KEYS = [:adapter, :endpoint, :itemsperpage, :user_agent, :request_options].freeze
10
+
11
+ # The adapter that will be used to connect if none is set
12
+ #
13
+ # @note The default faraday adapter is Net::HTTP.
14
+ DEFAULT_ADAPTER = Faraday.default_adapter
15
+
16
+ # The endpoint that will be used to connect if none is set
17
+ DEFAULT_ENDPOINT = 'http://api.ent.nokia.com/1.x'.freeze
18
+
19
+ # The country code that will be used if none is set
20
+ DEFAULT_COUNTRY_CODE = 'us'.freeze
21
+
22
+ # The domain that sill be used if none is set
23
+ DEFAULT_DOMAIN = 'music'.freeze
24
+
25
+ # The user agent that will be sent to the API endpoint if none is set
26
+ DEFAULT_USER_AGENT = "Nokia Music Ruby Gem #{NokiaMusic::VERSION}".freeze
27
+
28
+ # The default number of results to return from the API
29
+ DEFAULT_ITEMSPERPAGE = nil
30
+
31
+ # The default request options for Faraday
32
+ DEFAULT_REQUEST_OPTIONS = {
33
+ :timeout => 5,
34
+ :open_timeout => 5
35
+ }
36
+
37
+ # @private
38
+ attr_accessor *VALID_OPTIONS_KEYS
39
+
40
+ # When this module is extended, set all configuration options to their default values
41
+ def self.extended(base)
42
+ base.reset
43
+ end
44
+
45
+ # Convenience method to allow configuration options to be set in a block
46
+ def configure
47
+ yield self
48
+ end
49
+
50
+ # Create a hash of options and their values
51
+ def options
52
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
53
+ option.merge!(key => send(key))
54
+ end
55
+ end
56
+
57
+ # Reset all configuration options to defaults
58
+ def reset
59
+ self.adapter = DEFAULT_ADAPTER
60
+ self.endpoint = DEFAULT_ENDPOINT
61
+ self.user_agent = DEFAULT_USER_AGENT
62
+ self.request_options = DEFAULT_REQUEST_OPTIONS
63
+ self.itemsperpage = DEFAULT_ITEMSPERPAGE
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,38 @@
1
+ module NokiaMusic
2
+ module Request
3
+
4
+ # @private
5
+ private
6
+
7
+ # Perform an HTTP GET request
8
+ def request(resource_path, params = {})
9
+ request = construct_request(resource_path, params)
10
+
11
+ response = connection.get do |req|
12
+ req.url request[:url], request[:params]
13
+ req.options = request_options
14
+ end
15
+ response.body
16
+ end
17
+
18
+ def construct_request(resource_path, params={})
19
+ request = {}
20
+ request[:url] = "#{Configuration::DEFAULT_ENDPOINT}/#{Configuration::DEFAULT_COUNTRY_CODE}/#{resource_path}"
21
+ request[:params] = params.merge({client_id: NokiaMusic.client_id, domain: Configuration::DEFAULT_DOMAIN})
22
+ request
23
+ end
24
+
25
+ def connection
26
+ options = {
27
+ :headers => {'Accept' => 'application/json', 'User-Agent' => user_agent},
28
+ :url => api_endpoint,
29
+ }
30
+
31
+ Faraday.new(options) do |builder|
32
+ builder.use Faraday::Response::Rashify
33
+ builder.use Faraday::Response::ParseJson
34
+ builder.adapter(adapter)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module NokiaMusic
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nokia_music/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "nokia_music"
8
+ gem.version = NokiaMusic::VERSION
9
+ gem.authors = ["David Anderson"]
10
+ gem.email = ["davidmartinanderson@gmail.com"]
11
+ gem.description = "A simple ruby wrapper around the Nokia Music API"
12
+ gem.summary = "A gem to get me musical info from Nokia's API"
13
+ gem.homepage = "https://github.com/davidmanderson/nokia_music"
14
+
15
+ gem.add_runtime_dependency('rash', '~> 0.3')
16
+ gem.add_runtime_dependency('faraday_middleware', '~> 0.7')
17
+ gem.add_runtime_dependency('multi_json', '~> 1.0')
18
+
19
+ gem.files = `git ls-files`.split($/)
20
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
21
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
22
+ gem.require_paths = ["lib"]
23
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nokia_music
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Anderson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-03-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rash
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: faraday_middleware
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.7'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.7'
46
+ - !ruby/object:Gem::Dependency
47
+ name: multi_json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ description: A simple ruby wrapper around the Nokia Music API
63
+ email:
64
+ - davidmartinanderson@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/nokia_music.rb
75
+ - lib/nokia_music/client.rb
76
+ - lib/nokia_music/client/product.rb
77
+ - lib/nokia_music/client/product_sample.rb
78
+ - lib/nokia_music/client/search.rb
79
+ - lib/nokia_music/configuration.rb
80
+ - lib/nokia_music/request.rb
81
+ - lib/nokia_music/version.rb
82
+ - nokia_music.gemspec
83
+ homepage: https://github.com/davidmanderson/nokia_music
84
+ licenses: []
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.23
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: A gem to get me musical info from Nokia's API
107
+ test_files: []
108
+ has_rdoc: