omdb-api 1.4.2 → 1.4.3

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: 53a689d8935ccce557c928d0e8f38b8bd7cf75613d3e1fcb90bdbec4e0a8d1b5
4
- data.tar.gz: fc8af5261edc11877a0d4b0114313fbedf019a76fda2c01539210d70327d4546
3
+ metadata.gz: b434f90503d4999be549f1e24e40bccfb83c2e0d568838c9f6840e97ed60a80f
4
+ data.tar.gz: 6ebabe3d1e8048ca976d92a90d2733cf266bc4d6c83405ffaac0255f8807a053
5
5
  SHA512:
6
- metadata.gz: 1e7b1a6a65f3646aff09bcb100ee041a695e67c63cc10c809d7b429e7e6714126951a5c38d75248a839e0aea52b032c2ab9556272a75f05f1e421b2938817dd9
7
- data.tar.gz: 0b7bc261b523b9b3c1758f6723afc14c83eae4750e6ef3c0dc4d396ce916e2636d8a3860d18285e7b430e1e2c94342f6d25e093e1f5c81e29187974fc15e5fd4
6
+ metadata.gz: 2bc3aa06e4c30f98ab3659ed1e701134e063e8d713fb40c3c4e1e7e5e70626f6b3a6ba2343b1d74fe49044d2674772feab1c21e457ba43ba1f067d80fbf9bd6d
7
+ data.tar.gz: b3dfb55d02b3ec34adfd18fbb62878afa89fee38771a892e90eb1e89e4f09f24d088dc27316304c328cca2c6015ecc0cb6eb8488a2ab31d963d01e02d957f0de
@@ -6,3 +6,6 @@ Style/Documentation:
6
6
 
7
7
  Lint/Void:
8
8
  Enabled: false
9
+
10
+ Layout/LineLength:
11
+ Enabled: false
@@ -5,38 +5,37 @@ module Omdb
5
5
  module PublicApi
6
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(self, method, value, options)
9
-
10
- data = request.response
11
- movie = __format_data(data)
12
-
13
- if request.success?
14
- Omdb::Api::Movie.new(movie)
15
- else
16
- Omdb::Api::Error.new(movie)
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
17
15
  end
18
16
  end
19
17
  end
20
18
 
21
19
  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)
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)
28
25
 
29
- Omdb::Api::Movie.new(movie)
30
- end
31
- )
32
- else
33
- Omdb::Api::Error.new(request.response)
26
+ Omdb::Api::Movie.new(response)
27
+ end
28
+ )
29
+ rescue StandardError
30
+ Omdb::Api::Error.new(response)
31
+ end
34
32
  end
35
33
  end
36
34
 
37
- def __format_data(data)
38
- keys = data.keys.map(&:underscore)
39
- keys.zip(data.values).to_h
35
+ private
36
+
37
+ def format_response(response)
38
+ response.keys.map(&:underscore).zip(response.values).to_h
40
39
  end
41
40
  end
42
41
  end
@@ -5,10 +5,14 @@ module Omdb
5
5
  class Request
6
6
  BASE_URI = 'https://www.omdbapi.com'
7
7
 
8
- attr_reader :api_key, :field, :value, :options
8
+ attr_reader :client, :field, :value, :options
9
+
10
+ def self.call(client, method, value, options, &block)
11
+ new(client, method, value, options).make_get_to_imdb_api(&block)
12
+ end
9
13
 
10
14
  def initialize(client, method, value, options)
11
- @api_key = client.api_key
15
+ @client = client
12
16
  @value = CGI.escape(value)
13
17
  @options = options
14
18
  @field = if /id/.match?(method)
@@ -20,16 +24,15 @@ module Omdb
20
24
  end
21
25
  end
22
26
 
23
- def query_params
24
- { query: { apikey: api_key, field.to_s => value.to_s }.merge(options) }
25
- end
26
-
27
- def response
28
- @response ||= HTTParty.get(BASE_URI, query_params)
29
- end
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)
33
+ }
30
34
 
31
- def success?
32
- (response['Response'] || response['root']['response']) == 'True'
35
+ yield HTTParty.get(BASE_URI, params)
33
36
  end
34
37
  end
35
38
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Omdb
4
4
  module Api
5
- VERSION = '1.4.2'
5
+ VERSION = '1.4.3'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omdb-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Palaniuk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-11 00:00:00.000000000 Z
11
+ date: 2020-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport