service_client 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
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 +147 -0
  51. data/doc/ServiceClient/Response.html +793 -0
  52. data/doc/ServiceClient.html +146 -0
  53. data/doc/_index.html +576 -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 +216 -233
  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 +83 -23
  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 +65 -2
@@ -4,70 +4,126 @@ require 'httparty'
4
4
  require_relative 'response'
5
5
 
6
6
  module ServiceClient
7
- # ParamsRequired error
7
+ # Raised when a required param is nil.
8
+ #
9
+ # @example Raise an error if any of the required params are nil.
10
+ # raise ParamsRequired, "Params: #{params_required(params)} are required" if params_nil?(params)
8
11
  class ParamsRequired < StandardError; end
9
-
10
- # Base class
12
+ # This class provides a lightweight and flexible way to make HTTP requests
13
+ # in Ruby projects.
14
+ #
15
+ # This class can be used to make GET, POST, PUT, and DELETE requests to RESTful APIs.
16
+ # It simplifies the HTTP request process with a simple and intuitive API, configurable
17
+ # base URL, and default headers. It also provides a consistent and structured way to
18
+ # receive responses.
11
19
  class Base
12
20
  class << self
21
+ # Sets the base URL to be used for all requests.
22
+ #
23
+ # @param url [String] the base URL to be used
13
24
  def base_url(url = nil)
14
25
  @base_url = url
15
26
  end
16
27
 
28
+ # Sets the default headers to be sent with all requests.
29
+ #
30
+ # @param headers [Hash] the default headers to be sent
17
31
  def default_headers(headers = nil)
18
32
  @default_headers = headers
19
33
  end
20
34
 
35
+ # Makes a POST request to the specified URL.
36
+ #
37
+ # @param url [String] the URL to make the request to
38
+ # @param headers [Hash] additional headers to send with the request
39
+ # @param body [Hash] the request body to send with the request
21
40
  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)
41
+ request(:post, url, headers: headers, body: body)
27
42
  end
28
43
 
44
+ # Makes a GET request to the specified URL.
45
+ #
46
+ # @param url [String] the URL to make the request to
47
+ # @param headers [Hash] additional headers to send with the request
29
48
  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)
49
+ request(:get, url, headers: headers)
35
50
  end
36
51
 
52
+ # Makes a PUT request to the specified URL.
53
+ #
54
+ # @param url [String] the URL to make the request to
55
+ # @param headers [Hash] additional headers to send with the request
56
+ # @param body [Hash] the request body to send with the request
37
57
  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)
58
+ request(:put, url, headers: headers, body: body)
43
59
  end
44
60
 
61
+ # Makes a DELETE request to the specified URL.
62
+ #
63
+ # @param url [String] the URL to make the request to
64
+ # @param headers [Hash] additional headers to send with the request
45
65
  def delete(url = nil, headers: nil)
66
+ request(:delete, url, headers: headers)
67
+ end
68
+
69
+ private
70
+
71
+ # Makes an HTTP request to the specified URL.
72
+ #
73
+ # @param method [Symbol] the HTTP method to use (e.g. :get, :post)
74
+ # @param url [String] the URL to make the request to
75
+ # @param headers [Hash] additional headers to send with the request
76
+ # @param body [Hash] the request body to send with the request
77
+ def request(method, url, headers: nil, body: nil)
46
78
  raise_params_required(url: url)
47
79
 
48
- request = HTTParty.delete(build_url(url), headers: build_headers(headers))
80
+ options = {
81
+ headers: build_headers(headers),
82
+ body: body
83
+ }.compact
84
+
85
+ request = HTTParty.send(method, build_url(url), options)
49
86
 
50
87
  make_response(request)
51
88
  end
52
89
 
53
- private
54
-
90
+ # Converts the HTTP response into a structured response object.
91
+ #
92
+ # @param response [HTTParty::Response] the HTTP response object to convert
93
+ # @return [Response] the structured response object
55
94
  def make_response(response)
56
95
  Response.call(response)
57
96
  end
58
97
 
98
+ # Raises an error if any of the required params are nil.
99
+ #
100
+ # @param params [Hash] the params to check for nil values
101
+ # @raise [ParamsRequired] if any of the params are nil
102
+ # @return [nil] if all params are not nil
59
103
  def raise_params_required(params)
60
- raise ParamsRequired, "Params: #{nil_params(params)} are required" if params_nil?(params)
104
+ raise ParamsRequired, "Params: #{params_required(params)} are required" if params_nil?(params)
61
105
  end
62
106
 
63
- def nil_params(params)
107
+ # Returns an array of the keys of the params that are nil.
108
+ #
109
+ # @param params [Hash] the params to check for nil values
110
+ # @return [Array] the keys of the params that are nil
111
+ def params_required(params)
64
112
  params.select { |_, value| value.nil? }.keys
65
113
  end
66
114
 
115
+ # Returns true if any of the params are nil.
116
+ #
117
+ # @param params [Hash] the params to check for nil values
118
+ # @return [Boolean] true if any of the params are nil
67
119
  def params_nil?(params)
68
120
  params.values.any?(&:nil?)
69
121
  end
70
122
 
123
+ # Builds the URL to make the request to.
124
+ #
125
+ # @param path [String] the path to append to the base URL
126
+ # @return [String] the URL to make the request
71
127
  def build_url(path)
72
128
  base_url = instance_variable_get('@base_url')
73
129
 
@@ -76,6 +132,10 @@ module ServiceClient
76
132
  [base_url, path].compact.join('/')
77
133
  end
78
134
 
135
+ # Builds the headers to send with the request.
136
+ #
137
+ # @param headers [Hash] the headers to send with the request
138
+ # @return [Hash] the headers to send with the request
79
139
  def build_headers(headers)
80
140
  default_headers = instance_variable_get('@default_headers')
81
141
  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.4'
4
+ VERSION = '0.1.6'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: service_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alef Ojeda de Oliveira
@@ -54,7 +54,70 @@ files:
54
54
  - Rakefile
55
55
  - bin/console
56
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
57
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
58
121
  - lib/service_client.rb
59
122
  - lib/service_client/base.rb
60
123
  - lib/service_client/errors.rb
@@ -82,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
145
  - !ruby/object:Gem::Version
83
146
  version: '0'
84
147
  requirements: []
85
- rubygems_version: 3.4.7
148
+ rubygems_version: 3.1.2
86
149
  signing_key:
87
150
  specification_version: 4
88
151
  summary: Service client