omdb-api 1.0.0 → 1.2.0

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
- SHA256:
3
- metadata.gz: 6a2c321db04cfe60d53a29f52e1224e9f12ced5652b36d625a4d2b89f8e1ffea
4
- data.tar.gz: 586faa3491d686935bccd36a958656be6ecdd7f92a1f660ea384cb448baa665a
2
+ SHA1:
3
+ metadata.gz: 62bb6e258bb5205766ccb941ac9d91137698295b
4
+ data.tar.gz: 03b575568253ace39829ebc50f18098ff81cd876
5
5
  SHA512:
6
- metadata.gz: 9caec6e78e7b108c2843825ab48013f4a105b103d621238b72439cd597ecb4be701ac9513768ec9c7871cc08fd0f81f62e1866e81e7984615e6ab55bdf79d106
7
- data.tar.gz: 28ed895fd0cee519bd33977513de95e6d5c400530b8a935eee53523f6c5fe15dabf8a6023e82460baed53ae7786b25942698fd8ca1e98a34307928327fdd6526
6
+ metadata.gz: e0ab3602c4603c6139d2a588775cd6edc7004ef2208064480933f509e4dde26f6962aac643bbb9f584e40402faa40111699d568689b67ca1882da7d8d6a5858e
7
+ data.tar.gz: fe2ced8a302a4574e0ff4397915d2fae8fcaea7852d2abf096af825015b8d79ffeb0902b9048b6c3e9ef3d5b17e57d2f92a75a6027a940f3c1f7cf19f7272cb8
@@ -1,4 +1,3 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.2
4
- before_install: gem install bundler -v 1.11.2
3
+ - 2.5.1
data/README.md CHANGED
@@ -26,11 +26,21 @@ Or install it yourself as:
26
26
 
27
27
  ## Usage
28
28
 
29
+ ### Configuration
30
+
29
31
  First you have to create a client object with your API key
30
32
 
31
33
  `client = Omdb::Api::Client.new(api_key: [your API key])`
32
34
 
33
- Then you can query with this client object
35
+ You can also set the API key with a block
36
+
37
+ ```ruby
38
+ client = Omdb::Api::Client.new do |config|
39
+ config.api_key = api_key
40
+ end
41
+ ```
42
+
43
+ ### API
34
44
 
35
45
  ```ruby
36
46
  require 'omdb/api'
data/Rakefile CHANGED
@@ -1,10 +1,8 @@
1
- # frozen_string_literal: true
2
-
3
1
  require 'bundler/gem_tasks'
4
2
  require 'rspec/core/rake_task'
5
3
  require 'rubocop/rake_task'
6
4
 
7
- RuboCop::RakeTask.new
8
5
  RSpec::Core::RakeTask.new(:spec)
6
+ RuboCop::RakeTask.new
9
7
 
10
- task default: %i[rubocop spec]
8
+ task default: %i[spec rubocop]
@@ -7,4 +7,6 @@ require 'omdb/api/public_api'
7
7
  require 'omdb/api/client'
8
8
  require 'omdb/api/request'
9
9
  require 'omdb/api/error'
10
+ require 'active_support'
11
+ require 'active_support/core_ext/string'
10
12
  require 'httparty'
@@ -1,24 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'klass_param'
4
+
3
5
  module Omdb
4
6
  module Api
5
7
  class Error
6
- ERROR_ATTRIBUTES = %i[
7
- response
8
- error
9
- ].freeze
10
-
11
- attr_reader(*ERROR_ATTRIBUTES)
12
-
13
- def initialize(response)
14
- attrs(response)
15
- end
8
+ include KlassParam
16
9
 
17
- def attrs(response)
18
- response.each_key do |key|
19
- instance_variable_set("@#{key.downcase}".to_sym, response[key])
20
- end
21
- end
10
+ param :response
11
+ param :error
22
12
  end
23
13
  end
24
14
  end
@@ -1,42 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'klass_param'
4
+
3
5
  module Omdb
4
6
  module Api
5
7
  class Movie
6
- MOVIE_ATTRIBUTES = %i[
7
- actors
8
- awards
9
- country
10
- director
11
- genre
12
- imdbid
13
- imdbrating
14
- imdbvotes
15
- language
16
- metascore
17
- plot
18
- poster
19
- rated
20
- released
21
- runtime
22
- title
23
- type
24
- writer
25
- year
26
- error
27
- ].freeze
28
-
29
- attr_reader(*MOVIE_ATTRIBUTES)
30
-
31
- def initialize(response)
32
- attrs(response)
33
- end
8
+ include KlassParam
34
9
 
35
- def attrs(response)
36
- response.each_key do |key|
37
- instance_variable_set("@#{key.downcase}".to_sym, response[key])
38
- end
39
- end
10
+ param :actors
11
+ param :awards
12
+ param :country
13
+ param :director
14
+ param :genre
15
+ param :imdb_id
16
+ param :imdb_rating
17
+ param :imdb_votes
18
+ param :language
19
+ param :metascore
20
+ param :plot
21
+ param :poster
22
+ param :rated
23
+ param :released
24
+ param :runtime
25
+ param :title
26
+ param :type
27
+ param :writer
28
+ param :year
29
+ param :error
40
30
  end
41
31
  end
42
32
  end
@@ -3,24 +3,41 @@
3
3
  module Omdb
4
4
  module Api
5
5
  module PublicApi
6
- %i[find_by_id find_by_title search].each do |method|
6
+ %i[find_by_id find_by_title].each do |method|
7
7
  define_method(method) do |value, **options|
8
- request = Omdb::Api::Request.new(api_key, method, value, options)
8
+ request = Omdb::Api::Request.new(self, method, value, options)
9
9
 
10
- if method == :search && request.success?
11
- movies = [].tap do |m|
12
- request.response.fetch('Search').each do |movie|
13
- m << Omdb::Api::Movie.new(movie)
14
- end
15
- end
16
- Omdb::Api::Collection.new(movies)
17
- elsif request.success?
18
- Omdb::Api::Movie.new(request.response)
10
+ data = request.response
11
+ movie = __format_data(data)
12
+
13
+ if request.success?
14
+ Omdb::Api::Movie.new(params: movie)
19
15
  else
20
- Omdb::Api::Error.new(request.response)
16
+ Omdb::Api::Error.new(params: movie)
21
17
  end
22
18
  end
23
19
  end
20
+
21
+ def search(value, options = {})
22
+ request = Omdb::Api::Request.new(self, 'search', value, options)
23
+
24
+ if request.success?
25
+ Omdb::Api::Collection.new(
26
+ request.response.fetch('Search').map do |movie|
27
+ movie = __format_data(movie)
28
+
29
+ Omdb::Api::Movie.new(params: movie)
30
+ end
31
+ )
32
+ else
33
+ Omdb::Api::Error.new(params: request.response)
34
+ end
35
+ end
36
+
37
+ def __format_data(data)
38
+ keys = data.keys.map(&:underscore)
39
+ keys.zip(data.values).to_h
40
+ end
24
41
  end
25
42
  end
26
43
  end
@@ -3,12 +3,14 @@
3
3
  module Omdb
4
4
  module Api
5
5
  class Request
6
- BASE_URI = 'https://www.omdbapi.com'
6
+ BASE_URI = 'https://www.omdbapi.com'.freeze
7
7
 
8
8
  attr_reader :api_key, :field, :value, :options
9
9
 
10
- def initialize(api_key, method, value, options)
11
- @api_key = api_key
10
+ def initialize(client, method, value, options)
11
+ @api_key = client.api_key
12
+ @value = CGI.escape(value)
13
+ @options = options
12
14
  @field = if /id/.match?(method)
13
15
  'i'
14
16
  elsif /title/.match?(method)
@@ -16,8 +18,6 @@ module Omdb
16
18
  else
17
19
  's'
18
20
  end
19
- @value = CGI.escape(value)
20
- @options = options
21
21
  end
22
22
 
23
23
  def query_params
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Omdb
4
4
  module Api
5
- VERSION = '1.0.0'
5
+ VERSION = '1.2.0'.freeze
6
6
  end
7
7
  end
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
18
18
  end
19
19
 
20
20
  spec.require_paths = ['lib']
21
+ spec.add_dependency 'activesupport'
21
22
  spec.add_dependency 'httparty'
23
+ spec.add_dependency 'klass_param'
22
24
  spec.add_development_dependency 'bundler'
23
25
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omdb-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Palaniuk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-25 00:00:00.000000000 Z
11
+ date: 2019-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: httparty
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -24,6 +38,20 @@ dependencies:
24
38
  - - ">="
25
39
  - !ruby/object:Gem::Version
26
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: klass_param
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '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'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: bundler
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -82,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
110
  version: '0'
83
111
  requirements: []
84
112
  rubyforge_project:
85
- rubygems_version: 2.7.6
113
+ rubygems_version: 2.6.13
86
114
  signing_key:
87
115
  specification_version: 4
88
116
  summary: Ruby interface for the OMDB API