omdb-api 1.4.3 → 2.0.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
2
  SHA256:
3
- metadata.gz: b434f90503d4999be549f1e24e40bccfb83c2e0d568838c9f6840e97ed60a80f
4
- data.tar.gz: 6ebabe3d1e8048ca976d92a90d2733cf266bc4d6c83405ffaac0255f8807a053
3
+ metadata.gz: dc18a31e3a6e5ac9b7825783220ad5b8c05195150845a8042a54294528f4867a
4
+ data.tar.gz: c729c986aa822cd3fdfbea991d55154cc8709d73e250a2fb524086cafe1f739e
5
5
  SHA512:
6
- metadata.gz: 2bc3aa06e4c30f98ab3659ed1e701134e063e8d713fb40c3c4e1e7e5e70626f6b3a6ba2343b1d74fe49044d2674772feab1c21e457ba43ba1f067d80fbf9bd6d
7
- data.tar.gz: b3dfb55d02b3ec34adfd18fbb62878afa89fee38771a892e90eb1e89e4f09f24d088dc27316304c328cca2c6015ecc0cb6eb8488a2ab31d963d01e02d957f0de
6
+ metadata.gz: 846f21c9236f28a6d6415f7ba95825a4ee253f4d26f464c994cc7e0cee3fa86031af404167515f04a7c2def257d577a948139ded15c584da584ade2984f8bf3f
7
+ data.tar.gz: 001ae8d21322fa92ff346d9c08aa8025a2e4ce1bf19cbd4d268bcc7177764e56bcd79d17006bebe6dff014550d60e15021c677db67fb1188d8170fd3f295a130
@@ -0,0 +1,19 @@
1
+ ## 2.0 (2020-07-26)
2
+
3
+ ### Breaking Changes
4
+
5
+ * query params given to the client will be full words
6
+ and mapped to the Omdb API params - implemented so far:
7
+
8
+ ```ruby
9
+ {
10
+ id: 'i',
11
+ title: 't',
12
+ search: 's',
13
+ plot: 'plot',
14
+ year: 'y',
15
+ version: 'v'
16
+ }
17
+ ```
18
+
19
+ * a collection is now called `Movies` and will return `MovieResult` objects
data/Gemfile CHANGED
@@ -9,6 +9,7 @@ group :development do
9
9
  end
10
10
 
11
11
  group :test do
12
+ gem 'faker'
12
13
  gem 'rspec'
13
14
  gem 'rubocop'
14
15
  gem 'webmock'
data/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  ## Required
8
8
 
9
- You must request an API key first from [here](http://omdbapi.com/)
9
+ You must request an API key first from [here](http://omdbapi.com/apikey.aspx)
10
10
 
11
11
  ## Installation
12
12
 
@@ -30,7 +30,9 @@ Or install it yourself as:
30
30
 
31
31
  First you have to create a client object with your API key
32
32
 
33
- `client = Omdb::Api::Client.new(api_key: [your API key])`
33
+ ```ruby
34
+ client = Omdb::Api::Client.new(api_key: [your API key])`
35
+ ```
34
36
 
35
37
  You can also set the API key with a block
36
38
 
@@ -60,19 +62,16 @@ the OMDB API spec.
60
62
 
61
63
  For example:
62
64
 
63
- `client.find_by_title('star wars', plot: 'short')`
64
-
65
- Returns an `Omdb::Api::Movie`
66
-
67
- `#find_by_id('id')`
68
-
69
- `#find_by_title('title')`
65
+ ```ruby
66
+ client.find_by_title('star wars', plot: 'short')`
67
+ ```
70
68
 
71
- Returns an `Omdb::Api::Collection`
69
+ Returns an `Omdb::Api::Types::Movie`
72
70
 
73
- `#search('movie')`
71
+ An unsuccessful query will return an `Omdb::Api::Types::Error` object
74
72
 
75
- An unsuccessful query will return an `Omdb::Api::Error` object
73
+ ## TODO
74
+ Implement all all find options
76
75
 
77
76
 
78
77
  ## API
@@ -1,12 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'omdb/api/version'
4
- require 'omdb/api/collection'
5
- require 'omdb/api/movie'
6
- require 'omdb/api/public_api'
7
4
  require 'omdb/api/client'
8
- require 'omdb/api/request'
9
- require 'omdb/api/error'
10
- require 'active_support'
11
- require 'active_support/core_ext/string'
12
- require 'httparty'
@@ -1,18 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'omdb/api/public_api'
4
+
3
5
  module Omdb
4
6
  module Api
5
7
  class Client
6
8
  include PublicApi
7
9
 
8
- attr_accessor :api_key
10
+ attr_reader :configuration
9
11
 
10
12
  def initialize(options = {})
11
- options.each_pair do |k, v|
12
- instance_variable_set("@#{k}", v)
13
- end
14
- yield(self) if block_given?
13
+ @configuration = Configuration.new
14
+
15
+ options.each { |k, v| @configuration.__send__("#{k}=", v) }
16
+
17
+ yield(@configuration) if block_given?
15
18
  end
19
+
20
+ class Configuration
21
+ attr_accessor :api_key
22
+ end
23
+
24
+ private_constant :Configuration
16
25
  end
17
26
  end
18
27
  end
@@ -1,42 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'omdb/api/request'
4
+ require 'omdb/api/utils'
5
+ require 'omdb/api/types'
6
+ require 'omdb/api/public_api/find_by'
7
+ require 'omdb/api/public_api/search'
8
+
3
9
  module Omdb
4
10
  module Api
5
11
  module PublicApi
6
- %i[find_by_id find_by_title].each do |method|
7
- define_method(method) do |value, **options|
8
- Omdb::Api::Request.call(self, method, value, options) do |response|
9
- response = format_response(response)
10
- if response.dig('response') == 'False'
11
- Omdb::Api::Error.new(response)
12
- else
13
- Omdb::Api::Movie.new(response)
14
- end
15
- end
16
- end
17
- end
18
-
19
- def search(value, options = {})
20
- Omdb::Api::Request.call(self, 'search', value, options) do |response|
21
- begin
22
- Omdb::Api::Collection.new(
23
- response.fetch('Search').map do |movie_data|
24
- response = format_response(movie_data)
25
-
26
- Omdb::Api::Movie.new(response)
27
- end
28
- )
29
- rescue StandardError
30
- Omdb::Api::Error.new(response)
31
- end
32
- end
33
- end
34
-
35
- private
36
-
37
- def format_response(response)
38
- response.keys.map(&:underscore).zip(response.values).to_h
39
- end
12
+ include Utils
13
+ include FindBy
14
+ include Search
40
15
  end
41
16
  end
42
17
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Omdb
4
+ module Api
5
+ module PublicApi
6
+ module FindBy
7
+ def find_by_id(id, **options)
8
+ perform_get(
9
+ klass: Omdb::Api::Types::Movie,
10
+ query_params: { id: id }.merge(options),
11
+ headers: options.fetch(:headers, {})
12
+ )
13
+ end
14
+
15
+ def find_by_title(title, **options)
16
+ perform_get(
17
+ klass: Omdb::Api::Types::Movie,
18
+ query_params: { title: title }.merge(options),
19
+ headers: options.fetch(:headers, {})
20
+ )
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Omdb
4
+ module Api
5
+ module PublicApi
6
+ module Search
7
+ def search(value, **options)
8
+ perform_get(
9
+ klass: Omdb::Api::Types::Movies,
10
+ query_params: { search: value }.merge(options),
11
+ headers: options.fetch(:headers, {})
12
+ )
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,38 +1,60 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'httparty'
4
+
3
5
  module Omdb
4
6
  module Api
5
7
  class Request
6
8
  BASE_URI = 'https://www.omdbapi.com'
7
9
 
8
- attr_reader :client, :field, :value, :options
10
+ def initialize(client, request_method, params)
11
+ @configuration = client.configuration
12
+ @request_method = request_method
13
+ @headers = _set_headers(params.delete(:headers))
14
+ @params = _set_params(params.delete(:query_params))
15
+ end
9
16
 
10
- def self.call(client, method, value, options, &block)
11
- new(client, method, value, options).make_get_to_imdb_api(&block)
17
+ def perform
18
+ _http_client.public_send(
19
+ @request_method,
20
+ BASE_URI,
21
+ headers: @headers,
22
+ query: @params
23
+ )
12
24
  end
13
25
 
14
- def initialize(client, method, value, options)
15
- @client = client
16
- @value = CGI.escape(value)
17
- @options = options
18
- @field = if /id/.match?(method)
19
- 'i'
20
- elsif /title/.match?(method)
21
- 't'
22
- else
23
- 's'
24
- end
26
+ private
27
+
28
+ PARAMS_MAP = {
29
+ id: 'i',
30
+ title: 't',
31
+ search: 's',
32
+ plot: 'plot',
33
+ year: 'y',
34
+ return: 'r',
35
+ version: 'v'
36
+ }.freeze
37
+
38
+ def _set_params(params)
39
+ {}.tap do |p|
40
+ params.each { |k, v| p[PARAMS_MAP[k]] = v }
41
+ end.merge({ apikey: @configuration.api_key })
25
42
  end
26
43
 
27
- def make_get_to_imdb_api
28
- params = {
29
- query: {
30
- apikey: client.api_key,
31
- field.to_s => value.to_s
32
- }.merge(options)
44
+ def _set_headers(headers)
45
+ key_translate = {
46
+ content_type: 'Content-Type'
33
47
  }
34
48
 
35
- yield HTTParty.get(BASE_URI, params)
49
+ translated_headers = headers.each_with_object({}) do |(k, v), o|
50
+ o[key_translate[k]] = v
51
+ end
52
+
53
+ { 'Content-Type' => 'application/json' }.merge(translated_headers)
54
+ end
55
+
56
+ def _http_client
57
+ HTTParty
36
58
  end
37
59
  end
38
60
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-struct'
4
+ require 'omdb/api/types/base'
5
+ require 'omdb/api/types/movie'
6
+ require 'omdb/api/types/movie_result'
7
+ require 'omdb/api/types/movies'
8
+ require 'omdb/api/types/error'
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Omdb
4
+ module Api
5
+ module Types
6
+ class Base < Dry::Struct
7
+ transform_keys(&:to_sym)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Omdb
4
+ module Api
5
+ module Types
6
+ include Dry.Types()
7
+
8
+ class Error < Base
9
+ attribute :response, Omdb::Api::Types::String.optional.default(nil)
10
+ attribute :error, Omdb::Api::Types::String.optional.default(nil)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Omdb
4
+ module Api
5
+ module Types
6
+ include Dry.Types()
7
+
8
+ class Movie < Base
9
+ attribute :actors, Omdb::Api::Types::String.optional.default(nil)
10
+ attribute :awards, Omdb::Api::Types::String.optional.default(nil)
11
+ attribute :box_office, Omdb::Api::Types::String.optional.default(nil)
12
+ attribute :country, Omdb::Api::Types::String.optional.default(nil)
13
+ attribute :director, Omdb::Api::Types::String.optional.default(nil)
14
+ attribute :dvd, Omdb::Api::Types::String.optional.default(nil)
15
+ attribute :error, Omdb::Api::Types::String.optional.default(nil)
16
+ attribute :genre, Omdb::Api::Types::String.optional.default(nil)
17
+ attribute :imdb_id, Omdb::Api::Types::String.optional.default(nil)
18
+ attribute :imdb_rating, Omdb::Api::Types::String.optional.default(nil)
19
+ attribute :imdb_votes, Omdb::Api::Types::String.optional.default(nil)
20
+ attribute :language, Omdb::Api::Types::String.optional.default(nil)
21
+ attribute :metascore, Omdb::Api::Types::String.optional.default(nil)
22
+ attribute :plot, Omdb::Api::Types::String.optional.default(nil)
23
+ attribute :poster, Omdb::Api::Types::String.optional.default(nil)
24
+ attribute :rated, Omdb::Api::Types::String.optional.default(nil)
25
+ attribute :released, Omdb::Api::Types::String.optional.default(nil)
26
+ attribute :runtime, Omdb::Api::Types::String.optional.default(nil)
27
+ attribute :title, Omdb::Api::Types::String.optional.default(nil)
28
+ attribute :type, Omdb::Api::Types::String.optional.default(nil)
29
+ attribute :writer, Omdb::Api::Types::String.optional.default(nil)
30
+ attribute :year, Omdb::Api::Types::String.optional.default(nil)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Omdb
4
+ module Api
5
+ module Types
6
+ include Dry.Types()
7
+
8
+ class MovieResult < Base
9
+ attribute :imdb_id, Omdb::Api::Types::String.optional.default(nil)
10
+ attribute :poster, Omdb::Api::Types::String.optional.default(nil)
11
+ attribute :title, Omdb::Api::Types::String.optional.default(nil)
12
+ attribute :type, Omdb::Api::Types::String.optional.default(nil)
13
+ attribute :year, Omdb::Api::Types::String.optional.default(nil)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Omdb
4
+ module Api
5
+ module Types
6
+ include Dry.Types()
7
+
8
+ class Movies < Base
9
+ attribute :search, Omdb::Api::Types::Strict::Array.of(Omdb::Api::Types::MovieResult).optional.default([])
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/all'
4
+
5
+ module Omdb
6
+ module Api
7
+ module Utils
8
+ def perform_get(options)
9
+ perform_request(:get, options)
10
+ end
11
+
12
+ def perform_request(request_method, options)
13
+ klass = options.delete(:klass)
14
+
15
+ _response_handler(klass) do
16
+ Omdb::Api::Request.new(self, request_method, options).perform
17
+ end
18
+ end
19
+
20
+ def _response_handler(klass)
21
+ _handle_response(yield, klass)
22
+ # rescue Errno::ECONNREFUSED => e
23
+ end
24
+
25
+ def _handle_response(resp, klass)
26
+ resp.deep_transform_keys! { |k| k.underscore.to_sym }
27
+
28
+ resp.fetch(:response) == 'True' ? klass.new(resp) : Omdb::Api::Types::Error.new(resp)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Omdb
4
4
  module Api
5
- VERSION = '1.4.3'
5
+ VERSION = '2.0.0'
6
6
  end
7
7
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.authors = ['Nick Palaniuk']
11
11
  spec.email = ['npalaniuk@gmail.com']
12
12
  spec.summary = 'Ruby interface for the OMDB API'
13
- spec.description = 'A ruby interface for the OMDB API'
13
+ spec.description = 'Ruby interface for the OMDB API'
14
14
  spec.homepage = 'https://github.com/nikkypx/omdb-api'
15
15
  spec.license = 'MIT'
16
16
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
@@ -18,7 +18,8 @@ Gem::Specification.new do |spec|
18
18
  end
19
19
 
20
20
  spec.require_paths = ['lib']
21
- spec.add_dependency 'activesupport'
22
- spec.add_dependency 'httparty'
21
+ spec.add_dependency 'activesupport', '~> 4.2.10'
22
+ spec.add_dependency 'dry-struct', '~> 1.3'
23
+ spec.add_dependency 'httparty', '~> 0.15'
23
24
  spec.add_development_dependency 'bundler'
24
25
  end
metadata CHANGED
@@ -1,43 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omdb-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.3
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Palaniuk
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-25 00:00:00.000000000 Z
11
+ date: 2020-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 4.2.10
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 4.2.10
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-struct
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: httparty
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - ">="
45
+ - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: '0'
47
+ version: '0.15'
34
48
  type: :runtime
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - ">="
52
+ - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: '0'
54
+ version: '0.15'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -52,7 +66,7 @@ dependencies:
52
66
  - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
- description: A ruby interface for the OMDB API
69
+ description: Ruby interface for the OMDB API
56
70
  email:
57
71
  - npalaniuk@gmail.com
58
72
  executables: []
@@ -63,24 +77,31 @@ files:
63
77
  - ".rspec"
64
78
  - ".rubocop.yml"
65
79
  - ".travis.yml"
80
+ - CHANGELOG.md
66
81
  - Gemfile
67
82
  - LICENSE.txt
68
83
  - README.md
69
84
  - Rakefile
70
85
  - lib/omdb/api.rb
71
86
  - lib/omdb/api/client.rb
72
- - lib/omdb/api/collection.rb
73
- - lib/omdb/api/error.rb
74
- - lib/omdb/api/movie.rb
75
87
  - lib/omdb/api/public_api.rb
88
+ - lib/omdb/api/public_api/find_by.rb
89
+ - lib/omdb/api/public_api/search.rb
76
90
  - lib/omdb/api/request.rb
91
+ - lib/omdb/api/types.rb
92
+ - lib/omdb/api/types/base.rb
93
+ - lib/omdb/api/types/error.rb
94
+ - lib/omdb/api/types/movie.rb
95
+ - lib/omdb/api/types/movie_result.rb
96
+ - lib/omdb/api/types/movies.rb
97
+ - lib/omdb/api/utils.rb
77
98
  - lib/omdb/api/version.rb
78
99
  - omdb-api.gemspec
79
100
  homepage: https://github.com/nikkypx/omdb-api
80
101
  licenses:
81
102
  - MIT
82
103
  metadata: {}
83
- post_install_message:
104
+ post_install_message:
84
105
  rdoc_options: []
85
106
  require_paths:
86
107
  - lib
@@ -96,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
117
  version: '0'
97
118
  requirements: []
98
119
  rubygems_version: 3.0.3
99
- signing_key:
120
+ signing_key:
100
121
  specification_version: 4
101
122
  summary: Ruby interface for the OMDB API
102
123
  test_files: []
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Omdb
4
- module Api
5
- class Collection
6
- include Enumerable
7
- attr_accessor :movies
8
-
9
- def initialize(movies)
10
- self.movies = Array.new(movies)
11
- end
12
-
13
- def each
14
- movies.each { |item| yield item }
15
- end
16
- end
17
- end
18
- end
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Omdb
4
- module Api
5
- class Error
6
- attr_reader :response,
7
- :error
8
-
9
- def initialize(params)
10
- params.each_pair do |k, v|
11
- instance_variable_set("@#{k}", v)
12
- end
13
- end
14
- end
15
- end
16
- end
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Omdb
4
- module Api
5
- class Movie
6
- attr_reader :actors,
7
- :awards,
8
- :box_office,
9
- :country,
10
- :director,
11
- :dvd,
12
- :error,
13
- :genre,
14
- :imdb_id,
15
- :imdb_rating,
16
- :imdb_votes,
17
- :language,
18
- :metascore,
19
- :plot,
20
- :poster,
21
- :rated,
22
- :released,
23
- :runtime,
24
- :title,
25
- :type,
26
- :writer,
27
- :year
28
-
29
- def initialize(params)
30
- params.each_pair do |k, v|
31
- instance_variable_set("@#{k}", v)
32
- end
33
- end
34
- end
35
- end
36
- end