shuffler_fm 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/.gitignore +21 -0
  2. data/.rspec +1 -0
  3. data/.travis.yml +10 -0
  4. data/.yardopts +2 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +22 -0
  7. data/README.md +121 -0
  8. data/Rakefile +37 -0
  9. data/lib/shuffler_fm/api/artists.rb +53 -0
  10. data/lib/shuffler_fm/api/blogs.rb +52 -0
  11. data/lib/shuffler_fm/api/channels.rb +88 -0
  12. data/lib/shuffler_fm/api/charts.rb +28 -0
  13. data/lib/shuffler_fm/api/genres.rb +28 -0
  14. data/lib/shuffler_fm/api/tracks.rb +33 -0
  15. data/lib/shuffler_fm/api.rb +54 -0
  16. data/lib/shuffler_fm/configuration.rb +44 -0
  17. data/lib/shuffler_fm/errors.rb +37 -0
  18. data/lib/shuffler_fm/http.rb +25 -0
  19. data/lib/shuffler_fm/middleware/error_handler.rb +49 -0
  20. data/lib/shuffler_fm/request.rb +25 -0
  21. data/lib/shuffler_fm/version.rb +3 -0
  22. data/lib/shuffler_fm.rb +67 -0
  23. data/shuffler_fm.gemspec +37 -0
  24. data/spec/config.yml.sample +3 -0
  25. data/spec/fixtures/vcr_cassettes/api.yml +26 -0
  26. data/spec/fixtures/vcr_cassettes/artists.yml +187 -0
  27. data/spec/fixtures/vcr_cassettes/blogs.yml +187 -0
  28. data/spec/fixtures/vcr_cassettes/channels.yml +95 -0
  29. data/spec/fixtures/vcr_cassettes/charts.yml +95 -0
  30. data/spec/fixtures/vcr_cassettes/genres.yml +95 -0
  31. data/spec/fixtures/vcr_cassettes/tracks.yml +95 -0
  32. data/spec/helper.rb +39 -0
  33. data/spec/shuffler_fm/api/artists_spec.rb +25 -0
  34. data/spec/shuffler_fm/api/blogs_spec.rb +25 -0
  35. data/spec/shuffler_fm/api/channels_spec.rb +23 -0
  36. data/spec/shuffler_fm/api/charts_spec.rb +15 -0
  37. data/spec/shuffler_fm/api/genres_spec.rb +15 -0
  38. data/spec/shuffler_fm/api/tracks_spec.rb +15 -0
  39. data/spec/shuffler_fm/api_spec.rb +15 -0
  40. data/spec/shuffler_fm/configuration_spec.rb +9 -0
  41. data/spec/shuffler_fm/middleware/error_handler_spec.rb +29 -0
  42. data/spec/shufflerfm_spec.rb +29 -0
  43. metadata +315 -0
@@ -0,0 +1,49 @@
1
+ require 'faraday'
2
+
3
+ module ShufflerFM
4
+ # @private
5
+ module Middleware
6
+ class ErrorHandler < Faraday::Response::Middleware
7
+ def on_complete(response)
8
+ case response[:status].to_i
9
+ when 400
10
+ raise ShufflerFM::BadRequest, error_message(response)
11
+ when 401
12
+ raise ShufflerFM::Unauthorized, error_message(response)
13
+ when 403
14
+ raise ShufflerFM::Forbidden, error_message(response)
15
+ when 404
16
+ raise ShufflerFM::NotFound, error_message(response)
17
+ when 406
18
+ raise ShufflerFM::NotAcceptable, error_message(response)
19
+ when 422
20
+ raise ShufflerFM::UnprocessableEntity, error_message(response)
21
+ when 429
22
+ raise ShufflerFM::TooManyRequests, error_message(response)
23
+ when 500
24
+ raise ShufflerFM::InternalServerError, error_message(response)
25
+ when 501
26
+ raise ShufflerFM::NotImplemented, error_message(response)
27
+ when 502
28
+ raise ShufflerFM::BadGateway, error_message(response)
29
+ when 503
30
+ raise ShufflerFM::ServiceUnavailable, error_message(response)
31
+ end
32
+ end
33
+
34
+ private
35
+ def error_message(response)
36
+ msg = case response[:status].to_i
37
+ when 400
38
+ "The requested URL was not found on the server."
39
+ when 429
40
+ "Hourly rate-limit exceded."
41
+ else
42
+ response[:body].nil? ? "" : response[:body]
43
+ end
44
+
45
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]} #{msg}"
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,25 @@
1
+ module ShufflerFM
2
+ # @private
3
+ module Request
4
+ # Performs an HTTP GET request
5
+ #
6
+ # == Parameters:
7
+ # @param [String] path The request path
8
+ # @param [Hash] options The operation options
9
+ #
10
+ # @return The HTTP response
11
+ #
12
+ def get(path, options = {})
13
+ request(:get, path, options)
14
+ end
15
+
16
+ private
17
+ def request(method, path, options = {})
18
+ response = http_connection.send(method) do |request|
19
+ request.url("#{API.version}/#{path}", options)
20
+ end
21
+
22
+ response.body
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module ShufflerFM
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,67 @@
1
+ require 'shuffler_fm/version'
2
+ require 'shuffler_fm/configuration'
3
+ require 'shuffler_fm/api'
4
+
5
+ #
6
+ # shuffler_fm is a thin ruby API wrapper for http://shuffler.fm REST API
7
+ #
8
+ module ShufflerFM
9
+ class << self
10
+ # Provides an API for a given API key
11
+ #
12
+ # @overload api(key)
13
+ # @param [String] key An api key provided by http://shuffler.fm
14
+ #
15
+ # @return [ShufflerFM::API]
16
+ #
17
+ # @example get an API instance
18
+ # api = ShufflerFM.api("your-api-key")
19
+ # api.tracks # gets 1st page of the track list
20
+ #
21
+ # @overload api(key, configuration)
22
+ # @param [String] key An api key provided by http://shuffler.fm
23
+ # @param [Hash] configuration The configuration descriptor
24
+ #
25
+ # @option configuration [Integer] :connection_timeout The connection timeout in seconds. Defaults to 2.
26
+ # @option configuration [Integer] :read_timeout The open/read timeout in seconds. Defaults to 5.
27
+ # @option configuration [String] :proxy The proxy. Is unset by default. (e.g. 'http://192.168.1.1:8080')
28
+ #
29
+ # @return [ShufflerFM::API]
30
+ #
31
+ # @example
32
+ # api = ShufflerFM.api("your-api-key", connection_timeout: 2, read_timeout: 3, proxy: 'http://192.168.1.1:8080')
33
+ # api.tracks # gets 1st page of the track list
34
+ #
35
+ # @see ShufflerFM.configure
36
+ # @see ShufflerFM::Configuration
37
+ #
38
+ def api(key, configuration = {})
39
+ self.configuration = Configuration.new(configuration)
40
+ ShufflerFM::API.new(key)
41
+ end
42
+
43
+ # @!attribute [rw] configuration
44
+ # Access to the configuration descriptor
45
+ attr_accessor :configuration
46
+
47
+ # API wide configrations
48
+ #
49
+ # @yield [config] the configuration descriptor
50
+ #
51
+ # @see ShufflerFM.api
52
+ # @see ShufflerFM::Configuration
53
+ #
54
+ # @example
55
+ # ShufflerFM.configure do |config|
56
+ # config.connection_timeout = 1 #seconds
57
+ # config.read_timeout = 5 #seconds
58
+ # config.proxy = { uri: 'http://192.168.1.1:8080', user: 'user1', password: 'passwd'}
59
+ # end
60
+ #
61
+ def configure
62
+ yield(self.configuration)
63
+ end
64
+ end
65
+
66
+ self.configuration = Configuration.new
67
+ end
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'shuffler_fm/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "shuffler_fm"
8
+ gem.version = ShufflerFM::VERSION
9
+ gem.authors = ["Miguel Guinada"]
10
+ gem.email = ["mguinada@gmail.com"]
11
+ gem.description = %q{ shuffler_fm wraps shuffler.fm's REST api on a thin layer of ruby. }
12
+ gem.summary = %q{ Ruby API wrapper for http://shuffler.fm }
13
+ gem.homepage = "https://github.com/mguinada/shuffler_fm"
14
+ gem.date = Time.now.utc.strftime("%Y-%m-%d")
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ #package
22
+ gem.add_dependency 'faraday', '~> 0.8'
23
+ gem.add_dependency 'faraday_middleware', '~> 0.8'
24
+ gem.add_dependency 'rash', '~> 0.3'
25
+ gem.add_dependency 'multi_json', '~> 1.3'
26
+
27
+ #development
28
+ gem.add_development_dependency 'json'
29
+ gem.add_development_dependency 'rake'
30
+ gem.add_development_dependency 'pry'
31
+ gem.add_development_dependency 'rspec'
32
+ gem.add_development_dependency 'webmock'
33
+ gem.add_development_dependency 'vcr'
34
+ gem.add_development_dependency 'simplecov'
35
+ gem.add_development_dependency 'yard'
36
+ gem.add_development_dependency 'maruku'
37
+ end
@@ -0,0 +1,3 @@
1
+ #Copy this file naming it as: config.yml
2
+ #Set to your api key from shuffle.fm and run: rspec (or rake)
3
+ key: 'A KEY'
@@ -0,0 +1,26 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://api.shuffler.fm/v1/tracks?api-key=NO-SUCH-KEY&page=1
6
+ body:
7
+ string: ""
8
+ headers:
9
+ Accept:
10
+ - "*/*"
11
+ response:
12
+ status:
13
+ code: 403
14
+ message: FORBIDDEN
15
+ headers:
16
+ Content-Type:
17
+ - text/html; charset=utf-8
18
+ Content-Length:
19
+ - "39"
20
+ Server:
21
+ - TornadoServer/2.4
22
+ body:
23
+ string: That API-key is invalid my dear friend.
24
+ http_version:
25
+ recorded_at: Tue, 25 Sep 2012 20:54:36 GMT
26
+ recorded_with: VCR 2.2.5