fanart_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: 0cc51d31ba7daa204e738fa4f2b17b6a958aa2c3
4
- data.tar.gz: e6f60127a92926645c8513b1e4413255ca6bff03
3
+ metadata.gz: fb1a89e7dd959723e7d8858d0c62adbb23adbd8a
4
+ data.tar.gz: c1024c52abcacb924cdcae44aefe3b45596f34d7
5
5
  SHA512:
6
- metadata.gz: a056d1dde47a823944baf30359604ae61988ccc431a650d0b0426ccc688c2277d6815e1abb98f04648c916753df606ba9545a4b3e2e3c075e7f8c7e9dd25133b
7
- data.tar.gz: 31f209ac4d5de3b42bb9f310c6f0a63af832bc5d05cfcc77ed2f646e69b7b8e007d87289cf111f33ef9428ead5326cd6a04c64f2bf33d76c9ed55f26362e1a62
6
+ metadata.gz: 3bee85b645cf8cba13c2d1e63af1c9ef8e35ac358efdeb9619ef78071cfffdd7d253a0b6357afe8761c378add72b686493d9e5ed4096d4ab3d43ab3904d190f8
7
+ data.tar.gz: ba3e2106f429644761c2f7567b27dc4a649281d8a23df2a1802a361e6af0a6d9c8615152e7cf9cc9d26eb80e4c716888440c009ebe5f92c5edc542bc362788e1
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
  [![Dependency Status](https://gemnasium.com/wafcio/fanart_api.png)](https://gemnasium.com/wafcio/fanart_api)
3
3
  [![Code Climate](https://codeclimate.com/github/wafcio/fanart_api.png)](https://codeclimate.com/github/wafcio/fanart_api)
4
4
  [![Coverage Status](https://coveralls.io/repos/wafcio/fanart_api/badge.png)](https://coveralls.io/r/wafcio/fanart_api)
5
+ [![Gem Version](https://badge.fury.io/rb/fanart_api.png)](http://badge.fury.io/rb/fanart_api)
5
6
 
6
7
  # FanartApi
7
8
 
@@ -28,7 +29,7 @@ The generator will install an initializer where you must past your api_key.
28
29
  There is one entry point, in initialize you can past hash with api_key value, or leave empty:
29
30
 
30
31
  ```ruby
31
- client = FanartApi::Client.new('API_KEY')
32
+ client = FanartApi::Client.new(api_key: 'API_KEY')
32
33
  ```
33
34
 
34
35
  ## Usage
data/fanart_api.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_runtime_dependency 'confiture', '>= 0.1.4'
22
22
  spec.add_runtime_dependency 'httparty', '>= 0.12.0'
23
+ spec.add_runtime_dependency 'uri_template', '~> 0.6.0'
23
24
 
24
25
  spec.add_development_dependency 'bundler', '~> 1.3'
25
26
  spec.add_development_dependency 'rake'
@@ -1,4 +1,5 @@
1
1
  require 'httparty'
2
+ require 'uri_template'
2
3
 
3
4
  class FanartApi::Base
4
5
  include HTTParty
@@ -6,13 +7,41 @@ class FanartApi::Base
6
7
 
7
8
  def initialize(client)
8
9
  @client = client
10
+ @params = {}
9
11
  end
10
12
 
11
- def api_key
12
- @client.api_key
13
+ def get(uri)
14
+ @uri_template = URITemplate.new(uri)
15
+
16
+ self
17
+ end
18
+
19
+ def params(options)
20
+ @params = options
21
+
22
+ self
23
+ end
24
+
25
+ def response
26
+ @uri_template ? self.class.get(uri, body: @options) : nil
27
+ end
28
+
29
+ def prepare_uri
30
+ @uri_template ? @uri_template.expand(@params.merge(api_key: @client.api_key)) : nil
31
+ end
32
+
33
+ def uri
34
+ uri = prepare_uri
35
+ @params.reject!{ |param| restful_param_keys(uri).include?(param.to_s) }
36
+
37
+ uri
38
+ end
39
+
40
+ def restful_param_keys(uri_expanded)
41
+ @uri_template.extract(uri_expanded).keys
13
42
  end
14
43
 
15
- def shared_uri(id, type, sort, limit)
16
- "/#{api_key}/#{id}/json/#{type}/#{sort}/#{limit}"
44
+ def shared_uri
45
+ '{api_key}/{id}/json/{type}/{sort}/{limit}'
17
46
  end
18
47
  end
@@ -3,8 +3,8 @@ require 'httparty'
3
3
  class FanartApi::Client
4
4
  attr_reader :api_key
5
5
 
6
- def initialize(api_key = nil)
7
- @api_key = api_key ? api_key : FanartApi::Configuration.api_key
6
+ def initialize(options = {})
7
+ @api_key = options[:api_key] ? options[:api_key] : FanartApi::Configuration.api_key
8
8
  end
9
9
 
10
10
  def movie
@@ -1,10 +1,10 @@
1
1
  # documentation: http://fanart.tv/api-docs/movie-api
2
2
  class FanartApi::Movie < FanartApi::Base
3
3
  def find(id, type = 'all', sort = '1', limit = '2')
4
- self.class.get("movie#{shared_uri(id, type, sort, limit)}")
4
+ get("movies/#{shared_uri}").params(id: id, type: type, sort: sort, limit: limit).response
5
5
  end
6
6
 
7
7
  def update(timestamp = (Time.now - 172800).to_i)
8
- self.class.get("newmovies/#{api_key}/#{timestamp}")
8
+ get('newmovies/{api_key}/{timestamp}').params(timestamp: timestamp).response
9
9
  end
10
10
  end
@@ -2,11 +2,11 @@
2
2
  class FanartApi::Music < FanartApi::Base
3
3
  [:artist, :album, :label].each do |method_name|
4
4
  define_method(method_name) do |id, type = 'all', sort = '1', limit = '2'|
5
- self.class.get("#{method_name}#{shared_uri(id, type, sort, limit)}")
5
+ get("#{method_name}/#{shared_uri}").params(id: id, type: type, sort: sort, limit: limit).response
6
6
  end
7
7
  end
8
8
 
9
9
  def update(timestamp = (Time.now - 172800).to_i)
10
- self.class.get("newmusic/#{api_key}/#{timestamp}")
10
+ get('newmusic/{api_key}/{timestamp}').params(timestamp: timestamp).response
11
11
  end
12
12
  end
data/lib/fanart_api/tv.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  # documentation: http://fanart.tv/api-docs/tv-api
2
2
  class FanartApi::Tv < FanartApi::Base
3
3
  def find(id, type = 'all', sort = '1', limit = '2')
4
- self.class.get("series#{shared_uri(id, type, sort, limit)}")
4
+ get("series/#{shared_uri}").params(id: id, type: type, sort: sort, limit: limit).response
5
5
  end
6
6
 
7
7
  def update(timestamp = (Time.now - 172800).to_i)
8
- self.class.get("newtv/#{api_key}/#{timestamp}")
8
+ get('newtv/{api_key}/{timestamp}').params(timestamp: timestamp).response
9
9
  end
10
10
  end
@@ -1,3 +1,3 @@
1
1
  module FanartApi
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -7,21 +7,68 @@ describe FanartApi::Base do
7
7
  let(:klass) { ExampleClass }
8
8
  let(:model) { klass.new(FanartApi::Client.new) }
9
9
 
10
- describe '.api_key' do
11
- it 'should use default api_key' do
12
- model.api_key.should == FanartApi::Configuration.api_key
10
+ describe '.get' do
11
+ it 'should set @uri_template' do
12
+ model.get('http://example.com')
13
+
14
+ model.instance_variable_get('@uri_template').class.should == URITemplate::RFC6570
15
+ end
16
+
17
+ it 'should return self' do
18
+ model.get('http://example.com').should == model
19
+ end
20
+ end
21
+
22
+ describe '.params' do
23
+ it 'should set @params' do
24
+ model.params(sample: 'test')
25
+
26
+ model.instance_variable_get('@params').should == { sample: 'test' }
27
+ end
28
+
29
+ it 'should return self' do
30
+ model.params(sample: 'test').should == model
31
+ end
32
+ end
33
+
34
+ describe '.response' do
35
+ it 'should call get klass method' do
36
+ model.instance_variable_set('@uri_template', URITemplate.new('{api_key}/series/{id}'))
37
+ klass.should_receive(:get)
38
+
39
+ model.response
40
+ end
41
+ end
42
+
43
+ describe '.prepare_uri' do
44
+ it 'should receive correct uri string' do
45
+ model.instance_variable_set('@uri_template', URITemplate.new('{api_key}/series/{id}'))
46
+
47
+ model.prepare_uri.should == "#{FanartApi::Configuration.api_key}/series/"
48
+ end
49
+ end
50
+
51
+ describe '.uri' do
52
+ it 'should receive correct uri string' do
53
+ model.instance_variable_set('@uri_template', URITemplate.new('{api_key}/series/{id}'))
54
+ model.instance_variable_set('@params', id: '1234')
55
+
56
+ model.uri.should == "#{FanartApi::Configuration.api_key}/series/1234"
13
57
  end
58
+ end
59
+
60
+ describe '.restful_param_keys' do
61
+ it 'should receive correct uri string' do
62
+ uri_template = URITemplate.new('{api_key}/series/{id}')
63
+ model.instance_variable_set('@uri_template', uri_template)
14
64
 
15
- it 'should set api_key' do
16
- klass.new(FanartApi::Client.new('API_KEY')).api_key.should == 'API_KEY'
65
+ model.restful_param_keys(uri_template.expand).sort.should == ['api_key', 'id'].sort
17
66
  end
18
67
  end
19
68
 
20
69
  describe '.shared_uri' do
21
70
  it 'should return correct string' do
22
- model.stub(:api_key).and_return('API_KEY')
23
-
24
- model.shared_uri('ID', 'TYPE', 'SORT', 'LIMIT').should == "/API_KEY/ID/json/TYPE/SORT/LIMIT"
71
+ model.shared_uri.should == '{api_key}/{id}/json/{type}/{sort}/{limit}'
25
72
  end
26
73
  end
27
74
  end
@@ -3,20 +3,31 @@ require 'spec_helper'
3
3
  describe FanartApi::Movie do
4
4
  let(:klass) { FanartApi::Movie }
5
5
  let(:model) { klass.new(FanartApi::Client.new) }
6
+ let(:mock_model) { SampleModel.new }
6
7
 
7
8
  describe '.find' do
8
- it 'should call get with specific uri' do
9
- model.stub(:api_key).and_return('API_KEY')
10
- klass.should_receive(:get).with('movie/API_KEY/1234/json/all/1/2')
9
+ it 'should call get with specific params' do
10
+ model.should_receive(:get).with('movies/{api_key}/{id}/json/{type}/{sort}/{limit}').and_return(mock_model)
11
11
 
12
- model.find(1234)
12
+ model.find('1234')
13
+ end
14
+
15
+ it 'should call params with specific params' do
16
+ model.should_receive(:params).with(id: '1234', type: 'all', sort: '1', limit: '2').and_return(mock_model)
17
+
18
+ model.find('1234')
13
19
  end
14
20
  end
15
21
 
16
22
  describe '.update' do
17
- it 'should call get with specific uri' do
18
- model.stub(:api_key).and_return('API_KEY')
19
- klass.should_receive(:get).with('newmovies/API_KEY/1234')
23
+ it 'should call get with specific params' do
24
+ model.should_receive(:get).with('newmovies/{api_key}/{timestamp}').and_return(mock_model)
25
+
26
+ model.update(1234)
27
+ end
28
+
29
+ it 'should call params with specific params' do
30
+ model.should_receive(:params).with(timestamp: 1234).and_return(mock_model)
20
31
 
21
32
  model.update(1234)
22
33
  end
@@ -3,38 +3,59 @@ require 'spec_helper'
3
3
  describe FanartApi::Music do
4
4
  let(:klass) { FanartApi::Music }
5
5
  let(:model) { klass.new(FanartApi::Client.new) }
6
+ let(:mock_model) { SampleModel.new }
6
7
 
7
8
  describe '.artist' do
8
- it 'should call get with specific uri' do
9
- model.stub(:api_key).and_return('API_KEY')
10
- klass.should_receive(:get).with('artist/API_KEY/1234/json/all/1/2')
9
+ it 'should call get with specific params' do
10
+ model.should_receive(:get).with('artist/{api_key}/{id}/json/{type}/{sort}/{limit}').and_return(mock_model)
11
11
 
12
- model.artist(1234)
12
+ model.artist('1234')
13
+ end
14
+
15
+ it 'should call params with specific params' do
16
+ model.should_receive(:params).with(id: '1234', type: 'all', sort: '1', limit: '2').and_return(mock_model)
17
+
18
+ model.artist('1234')
13
19
  end
14
20
  end
15
21
 
16
22
  describe '.album' do
17
- it 'should call get with specific uri' do
18
- model.stub(:api_key).and_return('API_KEY')
19
- klass.should_receive(:get).with('album/API_KEY/1234/json/all/1/2')
23
+ it 'should call get with specific params' do
24
+ model.should_receive(:get).with('album/{api_key}/{id}/json/{type}/{sort}/{limit}').and_return(mock_model)
20
25
 
21
- model.album(1234)
26
+ model.album('1234')
27
+ end
28
+
29
+ it 'should call params with specific params' do
30
+ model.should_receive(:params).with(id: '1234', type: 'all', sort: '1', limit: '2').and_return(mock_model)
31
+
32
+ model.album('1234')
22
33
  end
23
34
  end
24
35
 
25
36
  describe '.label' do
26
- it 'should call get with specific uri' do
27
- model.stub(:api_key).and_return('API_KEY')
28
- klass.should_receive(:get).with('label/API_KEY/1234/json/all/1/2')
37
+ it 'should call get with specific params' do
38
+ model.should_receive(:get).with('label/{api_key}/{id}/json/{type}/{sort}/{limit}').and_return(mock_model)
29
39
 
30
- model.label(1234)
40
+ model.label('1234')
41
+ end
42
+
43
+ it 'should call params with specific params' do
44
+ model.should_receive(:params).with(id: '1234', type: 'all', sort: '1', limit: '2').and_return(mock_model)
45
+
46
+ model.label('1234')
31
47
  end
32
48
  end
33
49
 
34
50
  describe '.update' do
35
- it 'should call get with specific uri' do
36
- model.stub(:api_key).and_return('API_KEY')
37
- klass.should_receive(:get).with('newmusic/API_KEY/1234')
51
+ it 'should call get with specific params' do
52
+ model.should_receive(:get).with('newmusic/{api_key}/{timestamp}').and_return(mock_model)
53
+
54
+ model.update(1234)
55
+ end
56
+
57
+ it 'should call params with specific params' do
58
+ model.should_receive(:params).with(timestamp: 1234).and_return(mock_model)
38
59
 
39
60
  model.update(1234)
40
61
  end
@@ -3,20 +3,31 @@ require 'spec_helper'
3
3
  describe FanartApi::Tv do
4
4
  let(:klass) { FanartApi::Tv }
5
5
  let(:model) { klass.new(FanartApi::Client.new) }
6
+ let(:mock_model) { SampleModel.new }
6
7
 
7
8
  describe '.find' do
8
- it 'should call get with specific uri' do
9
- model.stub(:api_key).and_return('API_KEY')
10
- klass.should_receive(:get).with('series/API_KEY/1234/json/all/1/2')
9
+ it 'should call get with specific params' do
10
+ model.should_receive(:get).with('series/{api_key}/{id}/json/{type}/{sort}/{limit}').and_return(mock_model)
11
11
 
12
- model.find(1234)
12
+ model.find('1234')
13
+ end
14
+
15
+ it 'should call params with specific params' do
16
+ model.should_receive(:params).with(id: '1234', type: 'all', sort: '1', limit: '2').and_return(mock_model)
17
+
18
+ model.find('1234')
13
19
  end
14
20
  end
15
21
 
16
22
  describe '.update' do
17
- it 'should call get with specific uri' do
18
- model.stub(:api_key).and_return('API_KEY')
19
- klass.should_receive(:get).with('newtv/API_KEY/1234')
23
+ it 'should call get with specific params' do
24
+ model.should_receive(:get).with('newtv/{api_key}/{timestamp}').and_return(mock_model)
25
+
26
+ model.update(1234)
27
+ end
28
+
29
+ it 'should call params with specific params' do
30
+ model.should_receive(:params).with(timestamp: 1234).and_return(mock_model)
20
31
 
21
32
  model.update(1234)
22
33
  end
data/spec/spec_helper.rb CHANGED
@@ -9,5 +9,18 @@ FanartApi::Configuration.configure do |config|
9
9
  config.api_key = ''
10
10
  end
11
11
 
12
+ class SampleModel
13
+ def get(uri)
14
+ self
15
+ end
16
+
17
+ def params(options = {})
18
+ self
19
+ end
20
+
21
+ def response
22
+ end
23
+ end
24
+
12
25
  RSpec.configure do |config|
13
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fanart_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
  - Krzysztof Wawer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-15 00:00:00.000000000 Z
11
+ date: 2013-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: confiture
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.12.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: uri_template
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.6.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.6.0
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement