trailer_vote-api 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rubocop.yml +29 -0
  4. data/CHANGELOG.md +3 -0
  5. data/Gemfile +6 -0
  6. data/Gemfile.lock +77 -0
  7. data/README.md +184 -0
  8. data/Rakefile +12 -0
  9. data/bin/console +15 -0
  10. data/bin/setup +8 -0
  11. data/lib/trailer_vote/api/autoload.rb +15 -0
  12. data/lib/trailer_vote/api/composable/common.rb +61 -0
  13. data/lib/trailer_vote/api/composable/get.rb +54 -0
  14. data/lib/trailer_vote/api/configuration.rb +69 -0
  15. data/lib/trailer_vote/api/errors.rb +62 -0
  16. data/lib/trailer_vote/api/links.rb +51 -0
  17. data/lib/trailer_vote/api/place/create.rb +65 -0
  18. data/lib/trailer_vote/api/place/find.rb +49 -0
  19. data/lib/trailer_vote/api/place.rb +19 -0
  20. data/lib/trailer_vote/api/product/create.rb +56 -0
  21. data/lib/trailer_vote/api/product/find.rb +37 -0
  22. data/lib/trailer_vote/api/product/image/create.rb +66 -0
  23. data/lib/trailer_vote/api/product/image/find.rb +39 -0
  24. data/lib/trailer_vote/api/product/image/urls.rb +57 -0
  25. data/lib/trailer_vote/api/product/image.rb +31 -0
  26. data/lib/trailer_vote/api/product/lookup.rb +81 -0
  27. data/lib/trailer_vote/api/product/place/link.rb +64 -0
  28. data/lib/trailer_vote/api/product/place.rb +31 -0
  29. data/lib/trailer_vote/api/product/update.rb +72 -0
  30. data/lib/trailer_vote/api/product/video/create.rb +66 -0
  31. data/lib/trailer_vote/api/product/video/find.rb +39 -0
  32. data/lib/trailer_vote/api/product/video/urls.rb +57 -0
  33. data/lib/trailer_vote/api/product/video.rb +31 -0
  34. data/lib/trailer_vote/api/product.rb +25 -0
  35. data/lib/trailer_vote/api/type_registry.rb +93 -0
  36. data/lib/trailer_vote/api/version.rb +7 -0
  37. data/lib/trailer_vote/api.rb +42 -0
  38. data/trailer_vote-api.gemspec +35 -0
  39. metadata +234 -0
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'trailer_vote/api/composable/common'
4
+
5
+ require 'trailer_vote/api/product/video'
6
+ require 'trailer_vote/api/product/video/find'
7
+
8
+ module TrailerVote
9
+ module Api
10
+ class Product
11
+ class Video
12
+ def create
13
+ Create.new(configuration: configuration, product: product)
14
+ end
15
+
16
+ class Create
17
+ include Composable::Common
18
+
19
+ CONTENT = MediaTypes::ProductVideo.to_constructable.version(1).view('create')
20
+ SUCCESS = MediaTypes::ProductVideo.to_constructable.version(1)
21
+ FAILURE = MediaTypes::Errors.to_constructable.version(1)
22
+
23
+ ACCEPT = [SUCCESS.to_s, FAILURE.to_s(0.1)].join(', ').freeze
24
+
25
+ def initialize(configuration:, product:)
26
+ self.configuration = configuration
27
+ self.product = product
28
+ end
29
+
30
+ def call(data:, url: resolve_url)
31
+ body = encode(data)
32
+ branch(
33
+ resolve_client.headers(
34
+ Headers::ACCEPT => ACCEPT,
35
+ Headers::CONTENT_TYPE => "#{CONTENT}; charset=#{body.encoding.name}"
36
+ ).post(url, body: body),
37
+ data: data
38
+ )
39
+ end
40
+
41
+ private
42
+
43
+ attr_accessor :product
44
+
45
+ def resolve_url
46
+ product.links.videos
47
+ end
48
+
49
+ def encode(data)
50
+ TrailerVote::Api.encode(CONTENT, product_video: data)
51
+ end
52
+
53
+ def forward(result, data:)
54
+ return unless [307, 308].include?(result.status)
55
+ forward_klazz.new(configuration: configuration, product: product)
56
+ .call(data: data, url: redirected_url(result))
57
+ end
58
+
59
+ def redirect_klazz
60
+ Find
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'trailer_vote/api/composable/get'
4
+ require 'trailer_vote/api/product/video'
5
+
6
+ module TrailerVote
7
+ module Api
8
+ class Product
9
+ class Video
10
+ def find(result: nil)
11
+ Find.new(configuration: configuration, result: result)
12
+ end
13
+
14
+ class Find
15
+ include Composable::Get
16
+
17
+ SUCCESS = MediaTypes::ProductVideo.to_constructable.version(1)
18
+ FAILURE = MediaTypes::Errors.to_constructable.version(1)
19
+
20
+ ACCEPT = [SUCCESS.to_s, FAILURE.to_s(0.1)].join(', ').freeze
21
+
22
+ def initialize(configuration:, result: nil)
23
+ self.configuration = configuration
24
+ self.result = result
25
+ end
26
+
27
+ def call(url: nil)
28
+ return self if ok? || !url
29
+ branch(resolve_client.headers(Headers::ACCEPT => ACCEPT).get(url))
30
+ end
31
+
32
+ def data
33
+ to_h['product_video']
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'trailer_vote/api/composable/get'
4
+ require 'trailer_vote/api/product/video'
5
+
6
+ module TrailerVote
7
+ module Api
8
+ class Product
9
+ class Video
10
+ def urls
11
+ Urls.new(configuration: configuration, product: product)
12
+ end
13
+
14
+ class Urls
15
+ include Composable::Get
16
+
17
+ SUCCESS = MediaTypes::ProductVideo.to_constructable.version(1).view('index')
18
+ FAILURE = MediaTypes::Errors.to_constructable.version(1)
19
+
20
+ ACCEPT = [SUCCESS.to_s, FAILURE.to_s(0.1)].join(', ').freeze
21
+
22
+ def initialize(configuration:, product:, result: nil)
23
+ self.configuration = configuration
24
+ self.product = product
25
+ self.result = result
26
+ end
27
+
28
+ def call(url: resolve_url)
29
+ return self if ok? || !url
30
+ branch(resolve_client.headers(Headers::ACCEPT => ACCEPT).get(url))
31
+ end
32
+
33
+ def data
34
+ to_h['product_videos']
35
+ end
36
+
37
+ private
38
+
39
+ attr_accessor :product
40
+
41
+ def ok?
42
+ result&.status&.ok?
43
+ end
44
+
45
+ def resolve_url
46
+ product.links.videos
47
+ end
48
+
49
+ def redirect(result)
50
+ redirect_klazz.new(configuration: configuration, product: product, result: result)
51
+ .call(url: redirected_url(result))
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'trailer_vote/api/product'
4
+ require 'trailer_vote/api/product/find'
5
+
6
+ module TrailerVote
7
+ module Api
8
+ class Product
9
+ class Find
10
+ def video
11
+ Video.new(configuration: configuration, product: self)
12
+ end
13
+
14
+ alias product_video video
15
+ end
16
+
17
+ class Video
18
+ def initialize(configuration:, product: nil)
19
+ self.configuration = configuration
20
+ self.product = product
21
+ end
22
+
23
+ private
24
+
25
+ attr_accessor :configuration, :product
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ TrailerVote::MediaTypes::ProductVideo.register
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'trailer_vote/api/configuration'
4
+
5
+ module TrailerVote
6
+ module Api
7
+ class Configuration
8
+ def product
9
+ Product.new(configuration: self)
10
+ end
11
+ end
12
+
13
+ class Product
14
+ def initialize(configuration:)
15
+ self.configuration = configuration
16
+ end
17
+
18
+ private
19
+
20
+ attr_accessor :configuration
21
+ end
22
+ end
23
+ end
24
+
25
+ TrailerVote::MediaTypes::Product.register
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'oj'
4
+ require 'media_types'
5
+ require 'trailer_vote/media_types'
6
+
7
+ module TrailerVote
8
+ module Api
9
+
10
+ module TypeRegistry
11
+ class << self
12
+ attr_accessor :registry, :registry_shortcuts
13
+ end
14
+
15
+ self.registry = {}
16
+ self.registry_shortcuts = {}
17
+
18
+ module_function
19
+
20
+ def [](key)
21
+ registry.fetch(key) { registry.fetch(registry_shortcuts.fetch(key)) }
22
+ end
23
+
24
+ def []=(key, value)
25
+ registry[key] = value
26
+ end
27
+
28
+ def shortcut(key, alias_name)
29
+ registry_shortcuts[alias_name] = key
30
+ end
31
+
32
+ def register(registerable)
33
+ mime_type = registerable.to_s
34
+
35
+ self[mime_type] = AdapterFor(registerable.media_type)
36
+ shortcut(mime_type, registerable.to_sym)
37
+
38
+ registerable.aliases.each do |alias_mime_type|
39
+ shortcut(mime_type, alias_mime_type)
40
+ end
41
+ end
42
+
43
+ class << self
44
+ private
45
+
46
+ # noinspection RubyClassMethodNamingConvention
47
+ def AdapterFor(media_type) # rubocop:disable Naming/MethodName
48
+ adapter_name = generate_adapter_name(media_type)
49
+
50
+ if const_defined?(adapter_name)
51
+ return const_get(adapter_name)
52
+ end
53
+
54
+ const_set(adapter_name, Module.new).tap do |adapter|
55
+ define_encode(adapter, media_type)
56
+ define_decode(adapter, media_type)
57
+ end
58
+ end
59
+
60
+ def generate_adapter_name(media_type)
61
+ media_type.split(%r{[./_+-]}).map(&:capitalize).join('').tr('^A-z0-9', '_')
62
+ end
63
+
64
+ def define_encode(adapter, media_type)
65
+ adapter.define_singleton_method(:encode) do
66
+ begin
67
+ media_type.validate!(obj)
68
+ Oj.dump(obj, mode: :compat)
69
+ rescue Oj::Error, ::MediaTypes::Scheme::ValidationError => err
70
+ raise EncodeError.new(media_type: media_type, source: err)
71
+ end
72
+ end
73
+ end
74
+
75
+ def define_decode(adapter, media_type)
76
+ adapter.define_singleton_method('decode') do |str|
77
+ begin
78
+ Oj.load(str, mode: :strict).tap do |result|
79
+ media_type.valid?(result)
80
+ end
81
+ rescue Oj::Error, ::MediaTypes::Scheme::ValidationError => err
82
+ raise DecodeError.new(media_type: media_type, source: err)
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ module MediaTypes
92
+ integrate TrailerVote::Api::TypeRegistry
93
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TrailerVote
4
+ module Api
5
+ VERSION = '0.5.0'
6
+ end
7
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ # require 'httpx'
4
+ # HTTP_KLAZZ = HTTPX.plugin(:compression, :basic_authentication)
5
+ require 'http'
6
+ HTTP_KLAZZ = HTTP.use(:auto_inflate)
7
+
8
+ require 'trailer_vote/api/version'
9
+ require 'trailer_vote/api/type_registry'
10
+ require 'trailer_vote/api/errors'
11
+ require 'trailer_vote/api/configuration'
12
+
13
+ module TrailerVote
14
+ module Api
15
+ module_function
16
+
17
+ def decode(result)
18
+ TypeRegistry[result.content_type.mime_type].decode(result.body)
19
+ end
20
+
21
+ def encode(media_type, data)
22
+ TypeRegistry[media_type.to_s].encode(data)
23
+ end
24
+
25
+ def default_client(username, password)
26
+ # .basic_authentication
27
+ HTTP_KLAZZ.basic_auth(user: username, pass: password).headers(
28
+ Headers::ACCEPT_ENCODING => 'gzip, deflate; q=0.5, identity; q=0.1',
29
+ Headers::USER_AGENT => format('TrailerVote/httpx.rb (client %<version>s)', version: VERSION)
30
+ )
31
+ end
32
+
33
+ module Headers
34
+ ACCEPT = 'Accept'
35
+ ACCEPT_ENCODING = 'Accept-Encoding'
36
+ CONTENT_TYPE = 'Content-Type'
37
+ ETAG = 'ETag'
38
+ IF_MATCH = 'If-Match'
39
+ USER_AGENT = 'User-Agent'
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'trailer_vote/api/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'trailer_vote-api'
9
+ spec.version = TrailerVote::Api::VERSION
10
+ spec.authors = ['Derk-Jan Karrenbeld']
11
+ spec.email = ['dj@trailervote.com']
12
+ spec.homepage = 'https://github.com/trailervote/trailervote-api-clients'
13
+ spec.summary = 'Provides a client to interact with the TrailerVote API'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = 'bin'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_runtime_dependency 'http'
23
+ # spec.add_runtime_dependency 'httpx'
24
+ spec.add_runtime_dependency 'oj', '~> 3.6'
25
+ spec.add_runtime_dependency 'trailer_vote-media_types', '~> 0.6.1'
26
+
27
+ spec.add_development_dependency 'awesome_print', '~> 1.8'
28
+ spec.add_development_dependency 'bundler', '~> 1.16'
29
+ spec.add_development_dependency 'minitest', '~> 5.11'
30
+ spec.add_development_dependency 'minitest-ci', '~> 3.4'
31
+ spec.add_development_dependency 'minitest-reporters', '~> 1.3'
32
+ spec.add_development_dependency 'rake', '~> 10.5'
33
+ spec.add_development_dependency 'simplecov', '~> 0.16'
34
+ spec.add_development_dependency 'webmock', '~> 3.4'
35
+ end
metadata ADDED
@@ -0,0 +1,234 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trailer_vote-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Derk-Jan Karrenbeld
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: oj
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: trailer_vote-media_types
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.6.1
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.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: awesome_print
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.16'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.16'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.11'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.11'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest-ci
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.4'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.4'
111
+ - !ruby/object:Gem::Dependency
112
+ name: minitest-reporters
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.3'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.3'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rake
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '10.5'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '10.5'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.16'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.16'
153
+ - !ruby/object:Gem::Dependency
154
+ name: webmock
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '3.4'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '3.4'
167
+ description:
168
+ email:
169
+ - dj@trailervote.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - ".rubocop.yml"
176
+ - CHANGELOG.md
177
+ - Gemfile
178
+ - Gemfile.lock
179
+ - README.md
180
+ - Rakefile
181
+ - bin/console
182
+ - bin/setup
183
+ - lib/trailer_vote/api.rb
184
+ - lib/trailer_vote/api/autoload.rb
185
+ - lib/trailer_vote/api/composable/common.rb
186
+ - lib/trailer_vote/api/composable/get.rb
187
+ - lib/trailer_vote/api/configuration.rb
188
+ - lib/trailer_vote/api/errors.rb
189
+ - lib/trailer_vote/api/links.rb
190
+ - lib/trailer_vote/api/place.rb
191
+ - lib/trailer_vote/api/place/create.rb
192
+ - lib/trailer_vote/api/place/find.rb
193
+ - lib/trailer_vote/api/product.rb
194
+ - lib/trailer_vote/api/product/create.rb
195
+ - lib/trailer_vote/api/product/find.rb
196
+ - lib/trailer_vote/api/product/image.rb
197
+ - lib/trailer_vote/api/product/image/create.rb
198
+ - lib/trailer_vote/api/product/image/find.rb
199
+ - lib/trailer_vote/api/product/image/urls.rb
200
+ - lib/trailer_vote/api/product/lookup.rb
201
+ - lib/trailer_vote/api/product/place.rb
202
+ - lib/trailer_vote/api/product/place/link.rb
203
+ - lib/trailer_vote/api/product/update.rb
204
+ - lib/trailer_vote/api/product/video.rb
205
+ - lib/trailer_vote/api/product/video/create.rb
206
+ - lib/trailer_vote/api/product/video/find.rb
207
+ - lib/trailer_vote/api/product/video/urls.rb
208
+ - lib/trailer_vote/api/type_registry.rb
209
+ - lib/trailer_vote/api/version.rb
210
+ - trailer_vote-api.gemspec
211
+ homepage: https://github.com/trailervote/trailervote-api-clients
212
+ licenses: []
213
+ metadata: {}
214
+ post_install_message:
215
+ rdoc_options: []
216
+ require_paths:
217
+ - lib
218
+ required_ruby_version: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - ">="
221
+ - !ruby/object:Gem::Version
222
+ version: '0'
223
+ required_rubygems_version: !ruby/object:Gem::Requirement
224
+ requirements:
225
+ - - ">="
226
+ - !ruby/object:Gem::Version
227
+ version: '0'
228
+ requirements: []
229
+ rubyforge_project:
230
+ rubygems_version: 2.6.14.1
231
+ signing_key:
232
+ specification_version: 4
233
+ summary: Provides a client to interact with the TrailerVote API
234
+ test_files: []