trailer_vote-api 0.8.4 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +13 -13
  3. data/.rubocop.yml +29 -29
  4. data/CHANGELOG.md +56 -56
  5. data/Gemfile +6 -6
  6. data/Gemfile.lock +10 -11
  7. data/README.md +188 -188
  8. data/Rakefile +12 -12
  9. data/bin/console +15 -15
  10. data/bin/setup +8 -8
  11. data/lib/trailer_vote/api.rb +43 -43
  12. data/lib/trailer_vote/api/autoload.rb +24 -22
  13. data/lib/trailer_vote/api/composable/common.rb +91 -91
  14. data/lib/trailer_vote/api/composable/get.rb +66 -66
  15. data/lib/trailer_vote/api/configuration.rb +71 -71
  16. data/lib/trailer_vote/api/errors.rb +115 -115
  17. data/lib/trailer_vote/api/fallback_content_types.rb +50 -50
  18. data/lib/trailer_vote/api/issue.rb +31 -31
  19. data/lib/trailer_vote/api/issue/create.rb +72 -72
  20. data/lib/trailer_vote/api/issue/find.rb +42 -42
  21. data/lib/trailer_vote/api/links.rb +54 -54
  22. data/lib/trailer_vote/api/place.rb +24 -24
  23. data/lib/trailer_vote/api/place/children.rb +33 -0
  24. data/lib/trailer_vote/api/place/children/urls.rb +67 -0
  25. data/lib/trailer_vote/api/place/create.rb +83 -83
  26. data/lib/trailer_vote/api/place/find.rb +60 -59
  27. data/lib/trailer_vote/api/product.rb +33 -33
  28. data/lib/trailer_vote/api/product/create.rb +63 -63
  29. data/lib/trailer_vote/api/product/find.rb +55 -55
  30. data/lib/trailer_vote/api/product/image.rb +38 -38
  31. data/lib/trailer_vote/api/product/image/create.rb +83 -83
  32. data/lib/trailer_vote/api/product/image/find.rb +54 -54
  33. data/lib/trailer_vote/api/product/image/urls.rb +66 -66
  34. data/lib/trailer_vote/api/product/lookup.rb +97 -97
  35. data/lib/trailer_vote/api/product/place.rb +40 -40
  36. data/lib/trailer_vote/api/product/place/link.rb +75 -75
  37. data/lib/trailer_vote/api/product/update.rb +80 -80
  38. data/lib/trailer_vote/api/product/video.rb +38 -38
  39. data/lib/trailer_vote/api/product/video/create.rb +80 -80
  40. data/lib/trailer_vote/api/product/video/find.rb +56 -56
  41. data/lib/trailer_vote/api/product/video/urls.rb +66 -66
  42. data/lib/trailer_vote/api/type_registry.rb +99 -99
  43. data/lib/trailer_vote/api/version.rb +7 -7
  44. data/trailer_vote-api.gemspec +45 -45
  45. metadata +7 -6
@@ -1,71 +1,71 @@
1
- # frozen_string_literal: true
2
-
3
- require 'trailer_vote/media_types'
4
- require 'trailer_vote/api/composable/get'
5
-
6
- module TrailerVote
7
- module Api
8
-
9
- module_function
10
-
11
- def configure(url:, key:, secret:)
12
- client = Api.default_client(key, secret)
13
- Configuration.new(client: client, url: url)
14
- end
15
-
16
- class Configuration
17
- include Composable::Get
18
-
19
- SUCCESS = MediaTypes::Configuration.to_constructable.version(2)
20
- FAILURE = MediaTypes::Errors.to_constructable.version(1)
21
-
22
- ACCEPT = [SUCCESS.to_s, FAILURE.to_s(0.1)].join(', ').freeze
23
-
24
- attr_accessor :client
25
-
26
- def initialize(client:, url: nil, result: nil)
27
- self.client = client
28
- self.result = result
29
- self.url = url
30
- end
31
-
32
- def data
33
- to_h[:configuration]
34
- end
35
-
36
- def call(url: resolve_url)
37
- return self if ok? || !url
38
- guard_network_errors do
39
- merge(resolve_client.headers(Headers::ACCEPT => ACCEPT).get(url))
40
- end
41
- end
42
-
43
- private
44
-
45
- attr_accessor :url
46
-
47
- def ok?
48
- result&.status == 200
49
- end
50
-
51
- def redirecting?(result)
52
- [301, 302, 303, 307, 308].include?(result.status)
53
- end
54
-
55
- alias resolve_url url
56
- alias resolve_client client
57
-
58
- def merge(result)
59
- raise_on_error(result)
60
- self.result = redirecting?(result) ? redirect(result).result : result
61
- self
62
- end
63
-
64
- def redirect(result)
65
- call(url: result['Location'] || result['Content-Location'])
66
- end
67
- end
68
- end
69
- end
70
-
71
- TrailerVote::MediaTypes::Configuration.register
1
+ # frozen_string_literal: true
2
+
3
+ require 'trailer_vote/media_types'
4
+ require 'trailer_vote/api/composable/get'
5
+
6
+ module TrailerVote
7
+ module Api
8
+
9
+ module_function
10
+
11
+ def configure(url:, key:, secret:)
12
+ client = Api.default_client(key, secret)
13
+ Configuration.new(client: client, url: url)
14
+ end
15
+
16
+ class Configuration
17
+ include Composable::Get
18
+
19
+ SUCCESS = MediaTypes::Configuration.to_constructable.version(2)
20
+ FAILURE = MediaTypes::Errors.to_constructable.version(1)
21
+
22
+ ACCEPT = [SUCCESS.to_s, FAILURE.to_s(0.1)].join(', ').freeze
23
+
24
+ attr_accessor :client
25
+
26
+ def initialize(client:, url: nil, result: nil)
27
+ self.client = client
28
+ self.result = result
29
+ self.url = url
30
+ end
31
+
32
+ def data
33
+ to_h[:configuration]
34
+ end
35
+
36
+ def call(url: resolve_url)
37
+ return self if ok? || !url
38
+ guard_network_errors do
39
+ merge(resolve_client.headers(Headers::ACCEPT => ACCEPT).get(url))
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ attr_accessor :url
46
+
47
+ def ok?
48
+ result&.status == 200
49
+ end
50
+
51
+ def redirecting?(result)
52
+ [301, 302, 303, 307, 308].include?(result.status)
53
+ end
54
+
55
+ alias resolve_url url
56
+ alias resolve_client client
57
+
58
+ def merge(result)
59
+ raise_on_error(result)
60
+ self.result = redirecting?(result) ? redirect(result).result : result
61
+ self
62
+ end
63
+
64
+ def redirect(result)
65
+ call(url: result['Location'] || result['Content-Location'])
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ TrailerVote::MediaTypes::Configuration.register
@@ -1,115 +1,115 @@
1
- # frozen_string_literal: true
2
-
3
- require 'trailer_vote/media_types'
4
-
5
- module TrailerVote
6
- module Api
7
-
8
- class Error < RuntimeError; end
9
- class UnknownMediaType < Error; end
10
- class NetworkError < Error; end
11
- class ConnectionError < NetworkError; end
12
- class TimeoutError < NetworkError; end
13
-
14
- class EncodeError < Error
15
- def initialize(media_type:, source:)
16
- super format(
17
- 'Failed to encode data for %<media_type>s. Reason: %<reason>s',
18
- media_type: media_type,
19
- reason: source.message
20
- )
21
-
22
- self.source = source
23
- end
24
-
25
- attr_accessor :source
26
- end
27
-
28
- class DecodeError < Error
29
- def initialize(media_type:, source:)
30
- super format(
31
- 'Failed to decode data for %<media_type>s. Reason: %<reason>s',
32
- media_type: media_type,
33
- reason: source.message
34
- )
35
-
36
- self.source = source
37
- end
38
-
39
- attr_accessor :source
40
- end
41
-
42
- class ErrorsResponse < Error
43
- attr_accessor :result
44
-
45
- def initialize(result)
46
- self.result = result
47
- super messages
48
- end
49
-
50
- def messages
51
- Array(formatted_data[:errors]).map { |error| error[:message] }.join(', ')
52
- end
53
-
54
- def data
55
- @data ||= TrailerVote::Api.decode(result)
56
- rescue DecodeError, UnknownMediaType
57
- # noinspection RubyStringKeysInHashInspection
58
- @data = { errors: [{ message: result.status.reason }] }
59
- end
60
-
61
- def status
62
- result.status.to_i
63
- end
64
-
65
- def inspect
66
- format('[%<status>s] %<klazz>s' + "\n" + '%<messages>s', status: status, klazz: self.class.name, messages: messages)
67
- end
68
-
69
- alias to_i status
70
- alias to_s inspect
71
-
72
- private
73
-
74
- def formatted_data
75
- return data if data.is_a?(::Hash)
76
-
77
- { errors: [{ message: String(data) }] }
78
- end
79
- end
80
-
81
- class ClientError < ErrorsResponse; end
82
- class BadRequest < ClientError; end
83
- class Unauthorized < ClientError; end
84
- class Forbidden < ClientError; end
85
- class NotFound < ClientError; end
86
- class Conflict < ClientError; end
87
- class Gone < ClientError; end
88
- class PreconditionFailed < ClientError; end
89
- class UnprocessableEntity < ClientError; end
90
- class TooManyRequests < ClientError; end
91
-
92
- class ServerError < ErrorsResponse; end
93
-
94
- ERROR_MAPPING = Hash.new { |_, key| key < 500 ? ClientError : ServerError }.merge(
95
- 400 => BadRequest,
96
- 401 => Unauthorized,
97
- 403 => Forbidden,
98
- 404 => NotFound,
99
- 409 => Conflict,
100
- 410 => Gone,
101
- 412 => PreconditionFailed,
102
- 422 => UnprocessableEntity,
103
- 429 => TooManyRequests
104
- ).freeze
105
-
106
- module_function
107
-
108
- def raise_error(result)
109
- raise ERROR_MAPPING[result.status], result
110
- end
111
-
112
- end
113
- end
114
-
115
- TrailerVote::MediaTypes::Errors.register
1
+ # frozen_string_literal: true
2
+
3
+ require 'trailer_vote/media_types'
4
+
5
+ module TrailerVote
6
+ module Api
7
+
8
+ class Error < RuntimeError; end
9
+ class UnknownMediaType < Error; end
10
+ class NetworkError < Error; end
11
+ class ConnectionError < NetworkError; end
12
+ class TimeoutError < NetworkError; end
13
+
14
+ class EncodeError < Error
15
+ def initialize(media_type:, source:)
16
+ super format(
17
+ 'Failed to encode data for %<media_type>s. Reason: %<reason>s',
18
+ media_type: media_type,
19
+ reason: source.message
20
+ )
21
+
22
+ self.source = source
23
+ end
24
+
25
+ attr_accessor :source
26
+ end
27
+
28
+ class DecodeError < Error
29
+ def initialize(media_type:, source:)
30
+ super format(
31
+ 'Failed to decode data for %<media_type>s. Reason: %<reason>s',
32
+ media_type: media_type,
33
+ reason: source.message
34
+ )
35
+
36
+ self.source = source
37
+ end
38
+
39
+ attr_accessor :source
40
+ end
41
+
42
+ class ErrorsResponse < Error
43
+ attr_accessor :result
44
+
45
+ def initialize(result)
46
+ self.result = result
47
+ super messages
48
+ end
49
+
50
+ def messages
51
+ Array(formatted_data[:errors]).map { |error| error[:message] }.join(', ')
52
+ end
53
+
54
+ def data
55
+ @data ||= TrailerVote::Api.decode(result)
56
+ rescue DecodeError, UnknownMediaType
57
+ # noinspection RubyStringKeysInHashInspection
58
+ @data = { errors: [{ message: result.status.reason }] }
59
+ end
60
+
61
+ def status
62
+ result.status.to_i
63
+ end
64
+
65
+ def inspect
66
+ format('[%<status>s] %<klazz>s' + "\n" + '%<messages>s', status: status, klazz: self.class.name, messages: messages)
67
+ end
68
+
69
+ alias to_i status
70
+ alias to_s inspect
71
+
72
+ private
73
+
74
+ def formatted_data
75
+ return data if data.is_a?(::Hash)
76
+
77
+ { errors: [{ message: String(data) }] }
78
+ end
79
+ end
80
+
81
+ class ClientError < ErrorsResponse; end
82
+ class BadRequest < ClientError; end
83
+ class Unauthorized < ClientError; end
84
+ class Forbidden < ClientError; end
85
+ class NotFound < ClientError; end
86
+ class Conflict < ClientError; end
87
+ class Gone < ClientError; end
88
+ class PreconditionFailed < ClientError; end
89
+ class UnprocessableEntity < ClientError; end
90
+ class TooManyRequests < ClientError; end
91
+
92
+ class ServerError < ErrorsResponse; end
93
+
94
+ ERROR_MAPPING = Hash.new { |_, key| key < 500 ? ClientError : ServerError }.merge(
95
+ 400 => BadRequest,
96
+ 401 => Unauthorized,
97
+ 403 => Forbidden,
98
+ 404 => NotFound,
99
+ 409 => Conflict,
100
+ 410 => Gone,
101
+ 412 => PreconditionFailed,
102
+ 422 => UnprocessableEntity,
103
+ 429 => TooManyRequests
104
+ ).freeze
105
+
106
+ module_function
107
+
108
+ def raise_error(result)
109
+ raise ERROR_MAPPING[result.status], result
110
+ end
111
+
112
+ end
113
+ end
114
+
115
+ TrailerVote::MediaTypes::Errors.register
@@ -1,50 +1,50 @@
1
- # frozen_string_literal: true
2
-
3
- require 'oj'
4
- require 'trailer_vote/api/type_registry'
5
-
6
- module TrailerVote
7
- module Api
8
- module JsonTypeAdapter
9
- module_function
10
-
11
- def encode(obj)
12
- Oj.dump(obj, mode: :compat)
13
- rescue Oj::Error => err
14
- raise EncodeError.new(media_type: 'application/json', source: err)
15
- end
16
-
17
- def decode(obj)
18
- Oj.load(obj, mode: :strict, symbol_keys: true)
19
- rescue Oj::Error => err
20
- raise DecodeError.new(media_type: 'application/json', source: err)
21
- end
22
- end
23
- end
24
- end
25
-
26
- TrailerVote::Api::TypeRegistry['application/json'] = TrailerVote::Api::JsonTypeAdapter
27
- TrailerVote::Api::TypeRegistry.shortcut('application/json', :json)
28
- TrailerVote::Api::TypeRegistry.shortcut('application/json', 'text/json')
29
-
30
- module TrailerVote
31
- module Api
32
- module HtmlTypeAdapter
33
- module_function
34
-
35
- def encode(obj)
36
- return obj if obj.is_a?(String)
37
- raise EncodeError.new(
38
- media_type: 'text/html',
39
- source: ArgumentError.new('HTML must be passed in as a HTML string')
40
- )
41
- end
42
-
43
- def decode(str)
44
- str
45
- end
46
- end
47
- end
48
- end
49
-
50
- TrailerVote::Api::TypeRegistry['text/html'] = TrailerVote::Api::HtmlTypeAdapter
1
+ # frozen_string_literal: true
2
+
3
+ require 'oj'
4
+ require 'trailer_vote/api/type_registry'
5
+
6
+ module TrailerVote
7
+ module Api
8
+ module JsonTypeAdapter
9
+ module_function
10
+
11
+ def encode(obj)
12
+ Oj.dump(obj, mode: :compat)
13
+ rescue Oj::Error => err
14
+ raise EncodeError.new(media_type: 'application/json', source: err)
15
+ end
16
+
17
+ def decode(obj)
18
+ Oj.load(obj, mode: :strict, symbol_keys: true)
19
+ rescue Oj::Error => err
20
+ raise DecodeError.new(media_type: 'application/json', source: err)
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ TrailerVote::Api::TypeRegistry['application/json'] = TrailerVote::Api::JsonTypeAdapter
27
+ TrailerVote::Api::TypeRegistry.shortcut('application/json', :json)
28
+ TrailerVote::Api::TypeRegistry.shortcut('application/json', 'text/json')
29
+
30
+ module TrailerVote
31
+ module Api
32
+ module HtmlTypeAdapter
33
+ module_function
34
+
35
+ def encode(obj)
36
+ return obj if obj.is_a?(String)
37
+ raise EncodeError.new(
38
+ media_type: 'text/html',
39
+ source: ArgumentError.new('HTML must be passed in as a HTML string')
40
+ )
41
+ end
42
+
43
+ def decode(str)
44
+ str
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ TrailerVote::Api::TypeRegistry['text/html'] = TrailerVote::Api::HtmlTypeAdapter