trailer_vote-api 0.8.4 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
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
data/Rakefile CHANGED
@@ -1,12 +1,12 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/gem_tasks'
4
- require 'rake/testtask'
5
-
6
- Rake::TestTask.new(:test) do |t|
7
- t.libs << 'test'
8
- t.libs << 'lib'
9
- t.test_files = FileList['test/**/*_test.rb']
10
- end
11
-
12
- task default: :test
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
data/bin/console CHANGED
@@ -1,15 +1,15 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'bundler/setup'
5
- require 'trailer_vote/api'
6
-
7
- # You can add fixtures and/or initialization code here to make experimenting
8
- # with your gem easier. You can also use a different console, if you like.
9
-
10
- # (If you use this, don't forget to add pry to your Gemfile!)
11
- # require "pry"
12
- # Pry.start
13
-
14
- require 'irb'
15
- IRB.start(__FILE__)
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'trailer_vote/api'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -1,43 +1,43 @@
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/fallback_content_types'
11
- require 'trailer_vote/api/errors'
12
- require 'trailer_vote/api/configuration'
13
-
14
- module TrailerVote
15
- module Api
16
- module_function
17
-
18
- def decode(result)
19
- TypeRegistry[result.content_type.mime_type].decode(result.body.to_s)
20
- end
21
-
22
- def encode(media_type, data)
23
- TypeRegistry[media_type.to_s].encode(data)
24
- end
25
-
26
- def default_client(username, password)
27
- # .basic_authentication
28
- HTTP_KLAZZ.basic_auth(user: username, pass: password).headers(
29
- Headers::ACCEPT_ENCODING => 'gzip, deflate; q=0.5, identity; q=0.1',
30
- Headers::USER_AGENT => format('TrailerVote/httpx.rb (client %<version>s)', version: VERSION)
31
- )
32
- end
33
-
34
- module Headers
35
- ACCEPT = 'Accept'
36
- ACCEPT_ENCODING = 'Accept-Encoding'
37
- CONTENT_TYPE = 'Content-Type'
38
- ETAG = 'ETag'
39
- IF_MATCH = 'If-Match'
40
- USER_AGENT = 'User-Agent'
41
- end
42
- end
43
- end
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/fallback_content_types'
11
+ require 'trailer_vote/api/errors'
12
+ require 'trailer_vote/api/configuration'
13
+
14
+ module TrailerVote
15
+ module Api
16
+ module_function
17
+
18
+ def decode(result)
19
+ TypeRegistry[result.content_type.mime_type].decode(result.body.to_s)
20
+ end
21
+
22
+ def encode(media_type, data)
23
+ TypeRegistry[media_type.to_s].encode(data)
24
+ end
25
+
26
+ def default_client(username, password)
27
+ # .basic_authentication
28
+ HTTP_KLAZZ.basic_auth(user: username, pass: password).headers(
29
+ Headers::ACCEPT_ENCODING => 'gzip, deflate; q=0.5, identity; q=0.1',
30
+ Headers::USER_AGENT => format('TrailerVote/httpx.rb (client %<version>s)', version: VERSION)
31
+ )
32
+ end
33
+
34
+ module Headers
35
+ ACCEPT = 'Accept'
36
+ ACCEPT_ENCODING = 'Accept-Encoding'
37
+ CONTENT_TYPE = 'Content-Type'
38
+ ETAG = 'ETag'
39
+ IF_MATCH = 'If-Match'
40
+ USER_AGENT = 'User-Agent'
41
+ end
42
+ end
43
+ end
@@ -1,22 +1,24 @@
1
- # frozen_string_literal: true
2
-
3
- require 'trailer_vote/api'
4
-
5
- require 'trailer_vote/api/issue'
6
- require 'trailer_vote/api/issue/create'
7
- require 'trailer_vote/api/issue/find'
8
- require 'trailer_vote/api/place'
9
- require 'trailer_vote/api/place/create'
10
- require 'trailer_vote/api/place/find'
11
- require 'trailer_vote/api/product'
12
- require 'trailer_vote/api/product/create'
13
- require 'trailer_vote/api/product/find'
14
- require 'trailer_vote/api/product/lookup'
15
- require 'trailer_vote/api/product/image'
16
- require 'trailer_vote/api/product/image/create'
17
- require 'trailer_vote/api/product/image/find'
18
- require 'trailer_vote/api/product/place'
19
- require 'trailer_vote/api/product/place/link'
20
- require 'trailer_vote/api/product/video'
21
- require 'trailer_vote/api/product/video/create'
22
- require 'trailer_vote/api/product/video/find'
1
+ # frozen_string_literal: true
2
+
3
+ require 'trailer_vote/api'
4
+
5
+ require 'trailer_vote/api/issue'
6
+ require 'trailer_vote/api/issue/create'
7
+ require 'trailer_vote/api/issue/find'
8
+ require 'trailer_vote/api/place'
9
+ require 'trailer_vote/api/place/children'
10
+ require 'trailer_vote/api/place/children/urls'
11
+ require 'trailer_vote/api/place/create'
12
+ require 'trailer_vote/api/place/find'
13
+ require 'trailer_vote/api/product'
14
+ require 'trailer_vote/api/product/create'
15
+ require 'trailer_vote/api/product/find'
16
+ require 'trailer_vote/api/product/lookup'
17
+ require 'trailer_vote/api/product/image'
18
+ require 'trailer_vote/api/product/image/create'
19
+ require 'trailer_vote/api/product/image/find'
20
+ require 'trailer_vote/api/product/place'
21
+ require 'trailer_vote/api/product/place/link'
22
+ require 'trailer_vote/api/product/video'
23
+ require 'trailer_vote/api/product/video/create'
24
+ require 'trailer_vote/api/product/video/find'
@@ -1,91 +1,91 @@
1
- # frozen_string_literal: true
2
-
3
- module TrailerVote
4
- module Api
5
- module Composable
6
- module Common
7
-
8
- def self.included(base)
9
- base.class_eval do
10
- private
11
-
12
- attr_accessor :configuration
13
- end
14
- end
15
-
16
- # Execute the call (and all dependent calls)
17
- #
18
- # @see #forward_klazz
19
- # @see #redirect_klazz
20
- def call(**_opts)
21
- raise format('Missing implementation of #args in %<name>s', self.class.name)
22
- end
23
-
24
- private
25
-
26
- def resolve_client
27
- configuration.client
28
- end
29
-
30
- # Class used to branch forward to in case of a HTTP 307 or 308.
31
- #
32
- # @see #branch
33
- # @see #forward
34
- #
35
- # @private
36
- # @return [Class]
37
- def forward_klazz
38
- self.class
39
- end
40
-
41
- # Class used to branch redirect to in case of a HTTP 200, 201, 204, 301, 302, 303 or 304.
42
- #
43
- # When the status does not allow for redirecting, instead sends the retrieved result to the redirected class.
44
- #
45
- # @see #branch
46
- # @see #redirect
47
- #
48
- # @private
49
- # @return [Class]
50
- def redirect_klazz
51
- self.class
52
- end
53
-
54
- def guard_network_errors
55
- yield
56
- rescue HTTP::ConnectionError => err
57
- raise ConnectionError, err
58
- rescue HTTP::TimeoutError => err
59
- raise TimeoutError, err
60
- rescue HTTP::Error => err
61
- raise NetworkError, err
62
- end
63
-
64
- def branch(result, data: nil)
65
- raise_on_error(result)
66
- forward(result, data: data) || redirect(result)
67
- end
68
-
69
- def raise_on_error(result)
70
- return unless result.status.client_error? || result.status.server_error?
71
- TrailerVote::Api.raise_error result
72
- end
73
-
74
- def forward(result, data:)
75
- return unless [307, 308].include?(result.status)
76
- forward_klazz.new(configuration: configuration)
77
- .call(data: data, url: redirected_url(result))
78
- end
79
-
80
- def redirect(result)
81
- redirect_klazz.new(configuration: configuration, result: result)
82
- .call(url: redirected_url(result))
83
- end
84
-
85
- def redirected_url(result)
86
- result['Location'] || result['Content-Location']
87
- end
88
- end
89
- end
90
- end
91
- end
1
+ # frozen_string_literal: true
2
+
3
+ module TrailerVote
4
+ module Api
5
+ module Composable
6
+ module Common
7
+
8
+ def self.included(base)
9
+ base.class_eval do
10
+ private
11
+
12
+ attr_accessor :configuration
13
+ end
14
+ end
15
+
16
+ # Execute the call (and all dependent calls)
17
+ #
18
+ # @see #forward_klazz
19
+ # @see #redirect_klazz
20
+ def call(**_opts)
21
+ raise format('Missing implementation of #args in %<name>s', self.class.name)
22
+ end
23
+
24
+ private
25
+
26
+ def resolve_client
27
+ configuration.client
28
+ end
29
+
30
+ # Class used to branch forward to in case of a HTTP 307 or 308.
31
+ #
32
+ # @see #branch
33
+ # @see #forward
34
+ #
35
+ # @private
36
+ # @return [Class]
37
+ def forward_klazz
38
+ self.class
39
+ end
40
+
41
+ # Class used to branch redirect to in case of a HTTP 200, 201, 204, 301, 302, 303 or 304.
42
+ #
43
+ # When the status does not allow for redirecting, instead sends the retrieved result to the redirected class.
44
+ #
45
+ # @see #branch
46
+ # @see #redirect
47
+ #
48
+ # @private
49
+ # @return [Class]
50
+ def redirect_klazz
51
+ self.class
52
+ end
53
+
54
+ def guard_network_errors
55
+ yield
56
+ rescue HTTP::ConnectionError => err
57
+ raise ConnectionError, err
58
+ rescue HTTP::TimeoutError => err
59
+ raise TimeoutError, err
60
+ rescue HTTP::Error => err
61
+ raise NetworkError, err
62
+ end
63
+
64
+ def branch(result, data: nil)
65
+ raise_on_error(result)
66
+ forward(result, data: data) || redirect(result)
67
+ end
68
+
69
+ def raise_on_error(result)
70
+ return unless result.status.client_error? || result.status.server_error?
71
+ TrailerVote::Api.raise_error result
72
+ end
73
+
74
+ def forward(result, data:)
75
+ return unless [307, 308].include?(result.status)
76
+ forward_klazz.new(configuration: configuration)
77
+ .call(data: data, url: redirected_url(result))
78
+ end
79
+
80
+ def redirect(result)
81
+ redirect_klazz.new(configuration: configuration, result: result)
82
+ .call(url: redirected_url(result))
83
+ end
84
+
85
+ def redirected_url(result)
86
+ result['Location'] || result['Content-Location']
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -1,66 +1,66 @@
1
- # frozen_string_literal: true
2
-
3
- require 'trailer_vote/api/composable/common'
4
- require 'trailer_vote/api/links'
5
-
6
- module TrailerVote
7
- module Api
8
- module Composable
9
- module Get
10
- def self.included(base)
11
- base.include Common
12
- base.class_eval do
13
- attr_accessor :result
14
- end
15
- end
16
-
17
- # Return the {Links} for the result
18
- # @return [Links] the links
19
- def links
20
- # TODO: or headers
21
- @links ||= Links.new(data[:_links])
22
- end
23
-
24
- # Decode the result via {TypeRegistry}. This also takes care of MediaTypes validation.
25
- #
26
- # @see #decode
27
- # @return [Hash, Array] the decoded response
28
- def to_h
29
- @to_h ||= TrailerVote::Api.decode(call.result)
30
- end
31
-
32
- # Return the HTTP status
33
- # @return [Numeric] the HTTP status
34
- def to_i
35
- call.result.status
36
- end
37
-
38
- # Return the ETag value
39
- # @return [String, NilClass] the etag or nil
40
- def etag
41
- call.result[Headers::ETAG]
42
- end
43
-
44
- # Return the decoded result inner object
45
- # @return [Object] the inner object
46
- def data
47
- raise format('Missing implementation of #data in %<name>s', self.class.name)
48
- end
49
-
50
- private
51
-
52
- def ok?
53
- result&.status == 200
54
- end
55
-
56
- def redirecting?
57
- [301, 302, 303, 307, 308].include?(result&.status)
58
- end
59
-
60
- def redirect_to
61
- self.class
62
- end
63
- end
64
- end
65
- end
66
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'trailer_vote/api/composable/common'
4
+ require 'trailer_vote/api/links'
5
+
6
+ module TrailerVote
7
+ module Api
8
+ module Composable
9
+ module Get
10
+ def self.included(base)
11
+ base.include Common
12
+ base.class_eval do
13
+ attr_accessor :result
14
+ end
15
+ end
16
+
17
+ # Return the {Links} for the result
18
+ # @return [Links] the links
19
+ def links
20
+ # TODO: or headers
21
+ @links ||= Links.new(data[:_links])
22
+ end
23
+
24
+ # Decode the result via {TypeRegistry}. This also takes care of MediaTypes validation.
25
+ #
26
+ # @see #decode
27
+ # @return [Hash, Array] the decoded response
28
+ def to_h
29
+ @to_h ||= TrailerVote::Api.decode(call.result)
30
+ end
31
+
32
+ # Return the HTTP status
33
+ # @return [Numeric] the HTTP status
34
+ def to_i
35
+ call.result.status
36
+ end
37
+
38
+ # Return the ETag value
39
+ # @return [String, NilClass] the etag or nil
40
+ def etag
41
+ call.result[Headers::ETAG]
42
+ end
43
+
44
+ # Return the decoded result inner object
45
+ # @return [Object] the inner object
46
+ def data
47
+ raise format('Missing implementation of #data in %<name>s', self.class.name)
48
+ end
49
+
50
+ private
51
+
52
+ def ok?
53
+ result&.status == 200
54
+ end
55
+
56
+ def redirecting?
57
+ [301, 302, 303, 307, 308].include?(result&.status)
58
+ end
59
+
60
+ def redirect_to
61
+ self.class
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end