service_client 0.1.3 → 0.1.5

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 (71) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/doc/ServiceClient/Base.html +781 -0
  4. data/doc/ServiceClient/Errors/BadGatewayError.html +159 -0
  5. data/doc/ServiceClient/Errors/BadRequestError.html +159 -0
  6. data/doc/ServiceClient/Errors/BaseError.html +359 -0
  7. data/doc/ServiceClient/Errors/BlockedByWindowsParentalControlsError.html +159 -0
  8. data/doc/ServiceClient/Errors/ClientClosedRequestError.html +159 -0
  9. data/doc/ServiceClient/Errors/ConflictError.html +159 -0
  10. data/doc/ServiceClient/Errors/EnhanceYourCalmError.html +159 -0
  11. data/doc/ServiceClient/Errors/ExpectationFailedError.html +159 -0
  12. data/doc/ServiceClient/Errors/FailedDependencyError.html +159 -0
  13. data/doc/ServiceClient/Errors/ForbiddenError.html +159 -0
  14. data/doc/ServiceClient/Errors/GatewayTimeoutError.html +159 -0
  15. data/doc/ServiceClient/Errors/GoneError.html +159 -0
  16. data/doc/ServiceClient/Errors/HTTPVersionNotSupportedError.html +159 -0
  17. data/doc/ServiceClient/Errors/InsufficientStorageError.html +159 -0
  18. data/doc/ServiceClient/Errors/InternalServerError.html +159 -0
  19. data/doc/ServiceClient/Errors/LengthRequiredError.html +159 -0
  20. data/doc/ServiceClient/Errors/LockedError.html +159 -0
  21. data/doc/ServiceClient/Errors/LoopDetectedError.html +159 -0
  22. data/doc/ServiceClient/Errors/MethodNotAllowedError.html +159 -0
  23. data/doc/ServiceClient/Errors/NetworkAuthenticationRequiredError.html +159 -0
  24. data/doc/ServiceClient/Errors/NetworkConnectTimeoutError.html +159 -0
  25. data/doc/ServiceClient/Errors/NetworkReadTimeoutError.html +159 -0
  26. data/doc/ServiceClient/Errors/NoResponseError.html +159 -0
  27. data/doc/ServiceClient/Errors/NotAcceptableError.html +159 -0
  28. data/doc/ServiceClient/Errors/NotExtendedError.html +159 -0
  29. data/doc/ServiceClient/Errors/NotFoundError.html +159 -0
  30. data/doc/ServiceClient/Errors/NotImplementedError.html +159 -0
  31. data/doc/ServiceClient/Errors/PaymentRequiredError.html +159 -0
  32. data/doc/ServiceClient/Errors/PreconditionFailedError.html +159 -0
  33. data/doc/ServiceClient/Errors/PreconditionRequiredError.html +159 -0
  34. data/doc/ServiceClient/Errors/ProxyAuthenticationRequiredError.html +159 -0
  35. data/doc/ServiceClient/Errors/RequestEntityTooLargeError.html +159 -0
  36. data/doc/ServiceClient/Errors/RequestHeaderFieldsTooLargeError.html +159 -0
  37. data/doc/ServiceClient/Errors/RequestTimeoutError.html +159 -0
  38. data/doc/ServiceClient/Errors/RequestURITooLongError.html +159 -0
  39. data/doc/ServiceClient/Errors/RequestedRangeNotSatisfiableError.html +159 -0
  40. data/doc/ServiceClient/Errors/RetryWithError.html +159 -0
  41. data/doc/ServiceClient/Errors/ServiceUnavailableError.html +159 -0
  42. data/doc/ServiceClient/Errors/TooManyRequestsError.html +159 -0
  43. data/doc/ServiceClient/Errors/UnauthorizedError.html +159 -0
  44. data/doc/ServiceClient/Errors/UnavailableForLegalReasonsError.html +159 -0
  45. data/doc/ServiceClient/Errors/UnprocessableEntityError.html +159 -0
  46. data/doc/ServiceClient/Errors/UnsupportedMediaTypeError.html +159 -0
  47. data/doc/ServiceClient/Errors/UpgradeRequiredError.html +159 -0
  48. data/doc/ServiceClient/Errors/VariantAlsoNegotiatesError.html +159 -0
  49. data/doc/ServiceClient/Errors.html +325 -0
  50. data/doc/ServiceClient/ParamsRequired.html +135 -0
  51. data/doc/ServiceClient/Response.html +793 -0
  52. data/doc/ServiceClient.html +146 -0
  53. data/doc/_index.html +569 -0
  54. data/doc/class_list.html +51 -0
  55. data/doc/css/common.css +1 -0
  56. data/doc/css/full_list.css +58 -0
  57. data/doc/css/style.css +497 -0
  58. data/doc/file.README.html +243 -0
  59. data/doc/file_list.html +56 -0
  60. data/doc/frames.html +17 -0
  61. data/doc/index.html +249 -0
  62. data/doc/js/app.js +314 -0
  63. data/doc/js/full_list.js +216 -0
  64. data/doc/js/jquery.js +4 -0
  65. data/doc/method_list.html +171 -0
  66. data/doc/top-level-namespace.html +110 -0
  67. data/lib/service_client/base.rb +79 -24
  68. data/lib/service_client/errors.rb +18 -6
  69. data/lib/service_client/response.rb +24 -7
  70. data/lib/service_client/version.rb +1 -1
  71. metadata +69 -4
@@ -4,70 +4,121 @@ require 'httparty'
4
4
  require_relative 'response'
5
5
 
6
6
  module ServiceClient
7
- # ParamsRequired error
8
- class ParamsRequired < StandardError; end
9
-
10
- # Base class
7
+ # This class provides a lightweight and flexible way to make HTTP requests
8
+ # in Ruby projects.
9
+ #
10
+ # This class can be used to make GET, POST, PUT, and DELETE requests to RESTful APIs.
11
+ # It simplifies the HTTP request process with a simple and intuitive API, configurable
12
+ # base URL, and default headers. It also provides a consistent and structured way to
13
+ # receive responses.
11
14
  class Base
12
15
  class << self
16
+ # Sets the base URL to be used for all requests.
17
+ #
18
+ # @param url [String] the base URL to be used
13
19
  def base_url(url = nil)
14
20
  @base_url = url
15
21
  end
16
22
 
23
+ # Sets the default headers to be sent with all requests.
24
+ #
25
+ # @param headers [Hash] the default headers to be sent
17
26
  def default_headers(headers = nil)
18
27
  @default_headers = headers
19
28
  end
20
29
 
30
+ # Makes a POST request to the specified URL.
31
+ #
32
+ # @param url [String] the URL to make the request to
33
+ # @param headers [Hash] additional headers to send with the request
34
+ # @param body [Hash] the request body to send with the request
21
35
  def post(url = nil, headers: nil, body: nil)
22
- raise_params_required(url: url, headers: headers, body: body)
23
-
24
- request = HTTParty.post(build_url(url), headers: build_headers(headers), body: body)
25
-
26
- make_response(request)
36
+ request(:post, url, headers: headers, body: body)
27
37
  end
28
38
 
39
+ # Makes a GET request to the specified URL.
40
+ #
41
+ # @param url [String] the URL to make the request to
42
+ # @param headers [Hash] additional headers to send with the request
29
43
  def get(url = nil, headers: nil)
30
- raise_params_required(url: url)
31
-
32
- request = HTTParty.get(build_url(url), headers: build_headers(headers))
33
-
34
- make_response(request)
44
+ request(:get, url, headers: headers)
35
45
  end
36
46
 
47
+ # Makes a PUT request to the specified URL.
48
+ #
49
+ # @param url [String] the URL to make the request to
50
+ # @param headers [Hash] additional headers to send with the request
51
+ # @param body [Hash] the request body to send with the request
37
52
  def put(url = nil, headers: nil, body: nil)
38
- raise_params_required(url: url, body: body)
39
-
40
- request = HTTParty.put(build_url(url), headers: build_headers(headers), body: body)
41
-
42
- make_response(request)
53
+ request(:put, url, headers: headers, body: body)
43
54
  end
44
55
 
56
+ # Makes a DELETE request to the specified URL.
57
+ #
58
+ # @param url [String] the URL to make the request to
59
+ # @param headers [Hash] additional headers to send with the request
45
60
  def delete(url = nil, headers: nil)
61
+ request(:delete, url, headers: headers)
62
+ end
63
+
64
+ private
65
+
66
+ # Makes an HTTP request to the specified URL.
67
+ #
68
+ # @param method [Symbol] the HTTP method to use (e.g. :get, :post)
69
+ # @param url [String] the URL to make the request to
70
+ # @param headers [Hash] additional headers to send with the request
71
+ # @param body [Hash] the request body to send with the request
72
+ def request(method, url, headers: nil, body: nil)
46
73
  raise_params_required(url: url)
47
74
 
48
- request = HTTParty.delete(build_url(url), headers: build_headers(headers))
75
+ options = {
76
+ headers: build_headers(headers),
77
+ body: body
78
+ }.compact
79
+
80
+ request = HTTParty.send(method, build_url(url), options)
49
81
 
50
82
  make_response(request)
51
83
  end
52
84
 
53
- private
54
-
85
+ # Converts the HTTP response into a structured response object.
86
+ #
87
+ # @param response [HTTParty::Response] the HTTP response object to convert
88
+ # @return [Response] the structured response object
55
89
  def make_response(response)
56
90
  Response.call(response)
57
91
  end
58
92
 
93
+ # Raises an error if any of the required params are nil.
94
+ #
95
+ # @param params [Hash] the params to check for nil values
96
+ # @raise [ParamsRequired] if any of the params are nil
97
+ # @return [nil] if all params are not nil
59
98
  def raise_params_required(params)
60
- raise ParamsRequired, "Params: #{nil_params(params)} are required" if params_nil?(params)
99
+ raise ParamsRequired, "Params: #{params_required(params)} are required" if params_nil?(params)
61
100
  end
62
101
 
63
- def nil_params(params)
102
+ # Returns an array of the keys of the params that are nil.
103
+ #
104
+ # @param params [Hash] the params to check for nil values
105
+ # @return [Array] the keys of the params that are nil
106
+ def params_required(params)
64
107
  params.select { |_, value| value.nil? }.keys
65
108
  end
66
109
 
110
+ # Returns true if any of the params are nil.
111
+ #
112
+ # @param params [Hash] the params to check for nil values
113
+ # @return [Boolean] true if any of the params are nil
67
114
  def params_nil?(params)
68
115
  params.values.any?(&:nil?)
69
116
  end
70
117
 
118
+ # Builds the URL to make the request to.
119
+ #
120
+ # @param path [String] the path to append to the base URL
121
+ # @return [String] the URL to make the request
71
122
  def build_url(path)
72
123
  base_url = instance_variable_get('@base_url')
73
124
 
@@ -76,6 +127,10 @@ module ServiceClient
76
127
  [base_url, path].compact.join('/')
77
128
  end
78
129
 
130
+ # Builds the headers to send with the request.
131
+ #
132
+ # @param headers [Hash] the headers to send with the request
133
+ # @return [Hash] the headers to send with the request
79
134
  def build_headers(headers)
80
135
  default_headers = instance_variable_get('@default_headers')
81
136
  return default_headers if headers.nil?
@@ -1,19 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Purpose: ServiceClient::Errors module
3
+ # This module defines a set of error classes for common HTTP status codes. Each error class inherits from BaseError,
4
+ # which itself inherits from Ruby's StandardError class. These error classes are then used by the ServiceClient
5
+ # module to raise specific errors for specific HTTP status codes.
4
6
 
5
- # Compare this snippet from lib/service_client/errors.rb:
6
7
  module ServiceClient
7
8
  # Errors module
8
9
  module Errors
9
- # StandardError class
10
+ # BaseError class
10
11
  class BaseError < StandardError
11
- attr_reader :response
12
-
12
+ # Initializes a new instance of the BaseError class with the given message and HTTP response.
13
+ # @param msg [String] The error message.
14
+ # @param response [HTTParty::Response] The HTTP response that caused the error.
13
15
  def initialize(msg, response)
14
16
  @response = response
15
17
  super(msg)
16
18
  end
19
+
20
+ # Returns the HTTP response that caused the error.
21
+ # @return [HTTParty::Response] The HTTP response.
22
+ attr_reader :response
17
23
  end
18
24
 
19
25
  # 400 – Bad Request
@@ -72,7 +78,7 @@ module ServiceClient
72
78
  class NoResponseError < BaseError; end
73
79
  # 449 – Retry With
74
80
  class RetryWithError < BaseError; end
75
- # 450 – Bllocked by Windows Parental Controls
81
+ # 450 – Blocked by Windows Parental Controls
76
82
  class BlockedByWindowsParentalControlsError < BaseError; end
77
83
  # 451 – Unavailable For Legal Reasons
78
84
  class UnavailableForLegalReasonsError < BaseError; end
@@ -152,6 +158,12 @@ module ServiceClient
152
158
  '599': NetworkConnectTimeoutError,
153
159
  }.freeze
154
160
 
161
+ # Raises an error based on the given HTTP response.
162
+ #
163
+ # @param code [String] The HTTP response code.
164
+ # @param response [HTTParty::Response] The HTTP response.
165
+ # raise [BaseError] The error that corresponds to the given HTTP response code.
166
+ # @return [void]
155
167
  def raise_error(code, response)
156
168
  raise ERRORS[code.to_sym].new(ERRORS[code.to_sym].class.name, response)
157
169
  end
@@ -1,21 +1,31 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative 'errors'
3
- # Purpose: Response class for ServiceClient
4
- #
5
- # Compare this snippet from lib/service_client/response.rb:
4
+
6
5
  module ServiceClient
7
- # Response class
6
+ # The Response class encapsulates the HTTP response received by the ServiceClient gem.
7
+ #
8
+ # @attr_reader [Integer] code The HTTP status code of the response.
9
+ # @attr_reader [Hash, Array, String] data The parsed body of the response.
10
+ # @attr_reader [Hash] headers The HTTP headers of the response.
8
11
  class Response
9
12
  include ServiceClient::Errors
10
13
 
11
- attr_accessor :code, :data, :headers
14
+ attr_reader :code, :data, :headers
12
15
 
16
+ # Initializes a new instance of Response with the given HTTP response object.
17
+ #
18
+ # @param response [HTTParty::Response] The HTTP response object.
13
19
  def initialize(response)
14
20
  @code = response.code
15
21
  @data = response.parsed_response
16
22
  @headers = response.headers
17
23
  end
18
24
 
25
+ # Determines whether the response was successful or not.
26
+ #
27
+ # @return [ServiceClient::Response] Returns itself if the response was successful.
28
+ # @raise [ServiceClient::Error] Raises an error with the response code and object if the response was unsuccessful.
19
29
  def call
20
30
  case code
21
31
  when 200..299
@@ -25,8 +35,15 @@ module ServiceClient
25
35
  end
26
36
  end
27
37
 
28
- def self.call(response)
29
- new(response).call
38
+ class << self
39
+ # Initializes a new instance of Response and calls it to determine success or failure.
40
+ #
41
+ # @param response [HTTParty::Response] The HTTP response object.
42
+ # @return [ServiceClient::Response] Returns itself if the response was successful.
43
+ # @raise [ServiceClient::Error] Raises an error with the response code and object if the response was unsuccessful.
44
+ def call(response)
45
+ new(response).call
46
+ end
30
47
  end
31
48
  end
32
49
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ServiceClient
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: service_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alef Ojeda de Oliveira
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-22 00:00:00.000000000 Z
11
+ date: 2023-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -43,7 +43,8 @@ email:
43
43
  - alef.oliveira@dimensa.com.br
44
44
  executables: []
45
45
  extensions: []
46
- extra_rdoc_files: []
46
+ extra_rdoc_files:
47
+ - doc/index.html
47
48
  files:
48
49
  - Gemfile
49
50
  - Gemfile.lock
@@ -53,6 +54,70 @@ files:
53
54
  - Rakefile
54
55
  - bin/console
55
56
  - bin/setup
57
+ - doc/ServiceClient.html
58
+ - doc/ServiceClient/Base.html
59
+ - doc/ServiceClient/Errors.html
60
+ - doc/ServiceClient/Errors/BadGatewayError.html
61
+ - doc/ServiceClient/Errors/BadRequestError.html
62
+ - doc/ServiceClient/Errors/BaseError.html
63
+ - doc/ServiceClient/Errors/BlockedByWindowsParentalControlsError.html
64
+ - doc/ServiceClient/Errors/ClientClosedRequestError.html
65
+ - doc/ServiceClient/Errors/ConflictError.html
66
+ - doc/ServiceClient/Errors/EnhanceYourCalmError.html
67
+ - doc/ServiceClient/Errors/ExpectationFailedError.html
68
+ - doc/ServiceClient/Errors/FailedDependencyError.html
69
+ - doc/ServiceClient/Errors/ForbiddenError.html
70
+ - doc/ServiceClient/Errors/GatewayTimeoutError.html
71
+ - doc/ServiceClient/Errors/GoneError.html
72
+ - doc/ServiceClient/Errors/HTTPVersionNotSupportedError.html
73
+ - doc/ServiceClient/Errors/InsufficientStorageError.html
74
+ - doc/ServiceClient/Errors/InternalServerError.html
75
+ - doc/ServiceClient/Errors/LengthRequiredError.html
76
+ - doc/ServiceClient/Errors/LockedError.html
77
+ - doc/ServiceClient/Errors/LoopDetectedError.html
78
+ - doc/ServiceClient/Errors/MethodNotAllowedError.html
79
+ - doc/ServiceClient/Errors/NetworkAuthenticationRequiredError.html
80
+ - doc/ServiceClient/Errors/NetworkConnectTimeoutError.html
81
+ - doc/ServiceClient/Errors/NetworkReadTimeoutError.html
82
+ - doc/ServiceClient/Errors/NoResponseError.html
83
+ - doc/ServiceClient/Errors/NotAcceptableError.html
84
+ - doc/ServiceClient/Errors/NotExtendedError.html
85
+ - doc/ServiceClient/Errors/NotFoundError.html
86
+ - doc/ServiceClient/Errors/NotImplementedError.html
87
+ - doc/ServiceClient/Errors/PaymentRequiredError.html
88
+ - doc/ServiceClient/Errors/PreconditionFailedError.html
89
+ - doc/ServiceClient/Errors/PreconditionRequiredError.html
90
+ - doc/ServiceClient/Errors/ProxyAuthenticationRequiredError.html
91
+ - doc/ServiceClient/Errors/RequestEntityTooLargeError.html
92
+ - doc/ServiceClient/Errors/RequestHeaderFieldsTooLargeError.html
93
+ - doc/ServiceClient/Errors/RequestTimeoutError.html
94
+ - doc/ServiceClient/Errors/RequestURITooLongError.html
95
+ - doc/ServiceClient/Errors/RequestedRangeNotSatisfiableError.html
96
+ - doc/ServiceClient/Errors/RetryWithError.html
97
+ - doc/ServiceClient/Errors/ServiceUnavailableError.html
98
+ - doc/ServiceClient/Errors/TooManyRequestsError.html
99
+ - doc/ServiceClient/Errors/UnauthorizedError.html
100
+ - doc/ServiceClient/Errors/UnavailableForLegalReasonsError.html
101
+ - doc/ServiceClient/Errors/UnprocessableEntityError.html
102
+ - doc/ServiceClient/Errors/UnsupportedMediaTypeError.html
103
+ - doc/ServiceClient/Errors/UpgradeRequiredError.html
104
+ - doc/ServiceClient/Errors/VariantAlsoNegotiatesError.html
105
+ - doc/ServiceClient/ParamsRequired.html
106
+ - doc/ServiceClient/Response.html
107
+ - doc/_index.html
108
+ - doc/class_list.html
109
+ - doc/css/common.css
110
+ - doc/css/full_list.css
111
+ - doc/css/style.css
112
+ - doc/file.README.html
113
+ - doc/file_list.html
114
+ - doc/frames.html
115
+ - doc/index.html
116
+ - doc/js/app.js
117
+ - doc/js/full_list.js
118
+ - doc/js/jquery.js
119
+ - doc/method_list.html
120
+ - doc/top-level-namespace.html
56
121
  - lib/service_client.rb
57
122
  - lib/service_client/base.rb
58
123
  - lib/service_client/errors.rb
@@ -73,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
73
138
  requirements:
74
139
  - - ">="
75
140
  - !ruby/object:Gem::Version
76
- version: 2.3.0
141
+ version: 2.7.1
77
142
  required_rubygems_version: !ruby/object:Gem::Requirement
78
143
  requirements:
79
144
  - - ">="