uniqueCalculator 1.1 → 1.2.7

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.
@@ -8,52 +8,58 @@ module ApimaticCalculator
8
8
  class BaseController
9
9
  attr_accessor :config, :http_call_back
10
10
 
11
- def initialize(config, http_call_back: nil)
12
- @config = config
13
- @http_call_back = http_call_back
14
-
15
- @global_headers = {
16
- 'user-agent' => get_user_agent
17
- }
18
- end
19
-
20
- def validate_parameters(args)
21
- args.each do |_name, value|
22
- raise ArgumentError, "Required parameter #{_name} cannot be nil." if value.nil?
23
- end
11
+ def self.user_agent
12
+ 'APIMATIC 3.0'
24
13
  end
25
14
 
26
- def validate_parameters_types(args)
27
- args.each do |_name, type|
28
- key, val = type.first
29
- APIHelper.validate_types(key, val) unless key.nil?
30
- end
31
- end
32
15
 
33
- def execute_request(request, binary: false)
34
- @http_call_back&.on_before_request(request)
16
+ GLOBAL_ERRORS = {
17
+ 'default' => ErrorCase.new
18
+ .error_message('HTTP response not OK.')
19
+ .exception_type(APIException)
20
+ }.freeze
35
21
 
36
- APIHelper.clean_hash(request.headers)
37
- request.headers.merge!(@global_headers)
22
+ # Initialization constructor.
23
+ # @param [GlobalConfiguration] global_configuration The instance of GlobalConfiguration.
24
+ def initialize(global_configuration)
25
+ @global_configuration = global_configuration
26
+ @config = @global_configuration.client_configuration
27
+ @http_call_back = @config.http_callback
28
+ @api_call = ApiCall.new(@global_configuration)
29
+ end
38
30
 
39
- response = if binary
40
- config.http_client.execute_as_binary(request)
41
- else
42
- config.http_client.execute_as_string(request)
43
- end
44
- @http_call_back&.on_after_response(response)
31
+ # Creates a new builder for the Api Call instance.
32
+ # @return [ApiCall] The instance of ApiCall.
33
+ def new_api_call_builder
34
+ @api_call.new_builder
35
+ end
45
36
 
46
- response
37
+ # Creates a new instance of the request builder.
38
+ # @param [HttpMethodEnum] http_method The HTTP method to use in the request.
39
+ # @param [String] path The endpoint path to use in the request.
40
+ # @param [String] server The server to extract the base uri for the request.
41
+ # @return [RequestBuilder] The instance of RequestBuilder.
42
+ def new_request_builder(http_method, path, server)
43
+ RequestBuilder.new
44
+ .http_method(http_method)
45
+ .path(path)
46
+ .server(server)
47
47
  end
48
48
 
49
- def validate_response(response)
50
- raise APIException.new 'HTTP Response Not OK', response unless
51
- response.status_code.between?(200, 208) # [200,208] = HTTP OK
49
+ # Creates a new instance of the response handler.
50
+ # @return [ResponseHandler] The instance of ResponseHandler.
51
+ def new_response_handler
52
+ ResponseHandler.new
52
53
  end
53
54
 
54
- def get_user_agent
55
- user_agent = 'APIMATIC 3.0'
56
- user_agent
55
+ # Creates a new instance of the parameter.
56
+ # @param [String|optional] key The key of the parameter.
57
+ # @param [Object] value The value of the parameter.
58
+ # @return [Parameter] The instance of Parameter.
59
+ def new_parameter(value, key: nil)
60
+ Parameter.new
61
+ .key(key)
62
+ .value(value)
57
63
  end
58
64
  end
59
65
  end
@@ -6,10 +6,6 @@
6
6
  module ApimaticCalculator
7
7
  # SimpleCalculatorController
8
8
  class SimpleCalculatorController < BaseController
9
- def initialize(config, http_call_back: nil)
10
- super(config, http_call_back: http_call_back)
11
- end
12
-
13
9
  # Calculates the expression using the specified operation.
14
10
  # @param [OperationTypeEnum] operation Required parameter: The operator to
15
11
  # apply on the variables
@@ -17,29 +13,19 @@ module ApimaticCalculator
17
13
  # @param [Float] y Required parameter: The RHS value
18
14
  # @return [Float] response from the API call
19
15
  def get_calculate(options = {})
20
- # Prepare query url.
21
- _query_builder = config.get_base_uri
22
- _query_builder << '/{operation}'
23
- _query_builder = APIHelper.append_url_with_template_parameters(
24
- _query_builder,
25
- 'operation' => { 'value' => options['operation'], 'encode' => true }
26
- )
27
- _query_builder = APIHelper.append_url_with_query_parameters(
28
- _query_builder,
29
- 'x' => options['x'],
30
- 'y' => options['y']
31
- )
32
- _query_url = APIHelper.clean_url _query_builder
33
-
34
- # Prepare and execute HttpRequest.
35
- _request = config.http_client.get(
36
- _query_url
37
- )
38
- _response = execute_request(_request)
39
- validate_response(_response)
40
-
41
- # Return appropriate response type.
42
- _response.raw_body.to_f
16
+ new_api_call_builder
17
+ .request(new_request_builder(HttpMethodEnum::GET,
18
+ '/{operation}',
19
+ Server::CALCULATOR)
20
+ .template_param(new_parameter(options['operation'], key: 'operation')
21
+ .should_encode(true))
22
+ .query_param(new_parameter(options['x'], key: 'x'))
23
+ .query_param(new_parameter(options['y'], key: 'y')))
24
+ .response(new_response_handler
25
+ .deserializer(APIHelper.method(:deserialize_primitive_types))
26
+ .deserialize_into(proc do |response| response.to_f end)
27
+ .is_primitive_response(true))
28
+ .execute
43
29
  end
44
30
  end
45
31
  end
@@ -5,16 +5,6 @@
5
5
 
6
6
  module ApimaticCalculator
7
7
  # Class for exceptions when there is a network error, status code error, etc.
8
- class APIException < StandardError
9
- attr_reader :response, :response_code
10
-
11
- # The constructor.
12
- # @param [String] The reason for raising an exception.
13
- # @param [HttpResponse] The HttpReponse of the API call.
14
- def initialize(reason, response)
15
- super(reason)
16
- @response = response
17
- @response_code = response.status_code
18
- end
8
+ class APIException < CoreLibrary::ApiException
19
9
  end
20
10
  end
@@ -5,20 +5,6 @@
5
5
 
6
6
  module ApimaticCalculator
7
7
  # HttpCallBack allows defining callables for pre and post API calls.
8
- class HttpCallBack
9
- # A controller will call this method before making an HTTP Request.
10
- # @param [HttpRequest] The HttpRequest object which the HttpClient
11
- # will execute.
12
- def on_before_request(_http_request)
13
- raise NotImplementedError, 'This method needs
14
- to be implemented in a child class.'
15
- end
16
-
17
- # A controller will call this method after making an HTTP Request.
18
- # @param [HttpResponse] The HttpReponse of the API call.
19
- def on_after_response(_http_response)
20
- raise NotImplementedError, 'This method needs
21
- to be implemented in a child class.'
22
- end
8
+ class HttpCallBack < CoreLibrary::HttpCallback
23
9
  end
24
10
  end
@@ -5,9 +5,6 @@
5
5
 
6
6
  module ApimaticCalculator
7
7
  # HTTP Methods Enumeration.
8
- class HttpMethodEnum
9
- HTTPMETHODENUM = [GET = 'GET'.freeze, POST = 'POST'.freeze,
10
- PUT = 'PUT'.freeze, PATCH = 'PATCH'.freeze,
11
- DELETE = 'DELETE'.freeze, HEAD = 'HEAD'.freeze].freeze
8
+ class HttpMethodEnum < CoreLibrary::HttpMethod
12
9
  end
13
10
  end
@@ -5,50 +5,6 @@
5
5
 
6
6
  module ApimaticCalculator
7
7
  # Represents a single Http Request.
8
- class HttpRequest
9
- attr_accessor :http_method, :query_url, :headers,
10
- :parameters, :username, :password,
11
- :context
12
-
13
- # The constructor.
14
- # @param [HttpMethodEnum] The HTTP method.
15
- # @param [String] The URL to send the request to.
16
- # @param [Hash, Optional] The headers for the HTTP Request.
17
- # @param [Hash, Optional] The parameters for the HTTP Request.
18
- # @param [Hash, Optional] The context for the HTTP Request.
19
- def initialize(http_method,
20
- query_url,
21
- headers: {},
22
- parameters: {},
23
- context: {})
24
- @http_method = http_method
25
- @query_url = query_url
26
- @headers = headers
27
- @parameters = parameters
28
- @context = context
29
- end
30
-
31
- # Add a header to the HttpRequest.
32
- # @param [String] The name of the header.
33
- # @param [String] The value of the header.
34
- def add_header(name, value)
35
- @headers[name] = value
36
- end
37
-
38
- # Add a parameter to the HttpRequest.
39
- # @param [String] The name of the parameter.
40
- # @param [String] The value of the parameter.
41
- def add_parameter(name, value)
42
- @parameters[name] = value
43
- end
44
-
45
- # Add a query parameter to the HttpRequest.
46
- # @param [String] The name of the query parameter.
47
- # @param [String] The value of the query parameter.
48
- def add_query_parameter(name, value)
49
- @query_url = APIHelper.append_url_with_query_parameters(@query_url,
50
- name => value)
51
- @query_url = APIHelper.clean_url(@query_url)
52
- end
8
+ class HttpRequest < CoreLibrary::HttpRequest
53
9
  end
54
10
  end
@@ -5,25 +5,6 @@
5
5
 
6
6
  module ApimaticCalculator
7
7
  # Http response received.
8
- class HttpResponse
9
- attr_reader :status_code, :reason_phrase, :headers, :raw_body, :request
10
-
11
- # The constructor
12
- # @param [Integer] The status code returned by the server.
13
- # @param [String] The reason phrase returned by the server.
14
- # @param [Hash] The headers sent by the server in the response.
15
- # @param [String] The raw body of the response.
16
- # @param [HttpRequest] The request that resulted in this response.
17
- def initialize(status_code,
18
- reason_phrase,
19
- headers,
20
- raw_body,
21
- request)
22
- @status_code = status_code
23
- @reason_phrase = reason_phrase
24
- @headers = headers
25
- @raw_body = raw_body
26
- @request = request
27
- end
8
+ class HttpResponse < CoreLibrary::HttpResponse
28
9
  end
29
10
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  module ApimaticCalculator
7
7
  # Base model.
8
- class BaseModel
8
+ class BaseModel < CoreLibrary::BaseModel
9
9
  # Returns a Hash representation of the current object.
10
10
  def to_hash
11
11
  hash = {}
@@ -6,151 +6,6 @@
6
6
  require 'date'
7
7
  module ApimaticCalculator
8
8
  # A utility that supports dateTime conversion to different formats
9
- class DateTimeHelper
10
- # Safely converts a DateTime object into a rfc1123 format string
11
- # @param [DateTime] The DateTime object
12
- # @return [String] The rfc1123 formatted datetime string
13
- def self.to_rfc1123(date_time)
14
- date_time&.httpdate
15
- end
16
-
17
- # Safely converts a map of DateTime objects into a map of rfc1123 format string
18
- # @param [hash] a map of DateTime objects
19
- # @return [hash] a map of rfc1123 formatted datetime string
20
- def self.to_rfc1123_map(date_time, hash, key)
21
- return if date_time.nil?
22
-
23
- hash[key] = {}
24
- date_time.each do |k, v|
25
- hash[key][k] =
26
- if v.is_a?(BaseModel)
27
- v.to_hash
28
- else
29
- v.is_a?(DateTime) ? DateTimeHelper.to_rfc1123(v) : v
30
- end
31
- end
32
- hash[key]
33
- end
34
-
35
- # Safely converts an array of DateTime objects into an array of rfc1123 format string
36
- # @param [Array] an array of DateTime objects
37
- # @return [Array] an array of rfc1123 formatted datetime string
38
- def self.to_rfc1123_array(date_time, hash, key)
39
- return if date_time.nil?
40
-
41
- hash[key] = date_time.map do |v|
42
- if v.is_a?(BaseModel)
43
- v.to_hash
44
- else
45
- v.is_a?(DateTime) ? DateTimeHelper.to_rfc1123(v) : v
46
- end
47
- end
48
- end
49
-
50
- # Safely converts a DateTime object into a unix format string
51
- # @param [DateTime] The DateTime object
52
- # @return [String] The unix formatted datetime string
53
- def self.to_unix(date_time)
54
- date_time.to_time.utc.to_i unless date_time.nil?
55
- end
56
-
57
- # Safely converts a map of DateTime objects into a map of unix format string
58
- # @param [hash] a map of DateTime objects
59
- # @return [hash] a map of unix formatted datetime string
60
- def self.to_unix_map(date_time, hash, key)
61
- return if date_time.nil?
62
-
63
- hash[key] = {}
64
- date_time.each do |k, v|
65
- hash[key][k] =
66
- if v.is_a?(BaseModel)
67
- v.to_hash
68
- else
69
- v.is_a?(DateTime) ? DateTimeHelper.to_unix(v) : v
70
- end
71
- end
72
- hash[key]
73
- end
74
-
75
- # Safely converts an array of DateTime objects into a map of unix format string
76
- # @param [hash] an array of DateTime objects
77
- # @return [hash] an array of unix formatted datetime string
78
- def self.to_unix_array(date_time, hash, key)
79
- return if date_time.nil?
80
-
81
- hash[key] = date_time.map do |v|
82
- if v.is_a?(BaseModel)
83
- v.to_hash
84
- else
85
- v.is_a?(DateTime) ? DateTimeHelper.to_unix(v) : v
86
- end
87
- end
88
- end
89
-
90
- # Safely converts a DateTime object into a rfc3339 format string
91
- # @param [DateTime] The DateTime object
92
- # @return [String] The rfc3339 formatted datetime string
93
- def self.to_rfc3339(date_time)
94
- date_time&.rfc3339
95
- end
96
-
97
- # Safely converts a map of DateTime objects into a map of rfc1123 format string
98
- # @param [hash] a map of DateTime objects
99
- # @return [hash] a map of rfc1123 formatted datetime string
100
- def self.to_rfc3339_map(date_time, hash, key)
101
- return if date_time.nil?
102
-
103
- hash[key] = {}
104
- date_time.each do |k, v|
105
- hash[key][k] =
106
- if v.is_a?(BaseModel)
107
- v.to_hash
108
- else
109
- v.is_a?(DateTime) ? DateTimeHelper.to_rfc3339(v) : v
110
- end
111
- end
112
- hash[key]
113
- end
114
-
115
- # Safely converts an array of DateTime objects into an array of rfc1123 format string
116
- # @param [Array] an array of DateTime objects
117
- # @return [Array] an array of rfc1123 formatted datetime string
118
- def self.to_rfc3339_array(date_time, hash, key)
119
- return if date_time.nil?
120
-
121
- hash[key] = date_time.map do |v|
122
- if v.is_a?(BaseModel)
123
- v.to_hash
124
- else
125
- v.is_a?(DateTime) ? DateTimeHelper.to_rfc3339(v) : v
126
- end
127
- end
128
- end
129
-
130
- # Safely converts a rfc1123 format string into a DateTime object
131
- # @param [String] The rfc1123 formatted datetime string
132
- # @return [DateTime] A DateTime object
133
- def self.from_rfc1123(date_time)
134
- DateTime.httpdate(date_time)
135
- end
136
-
137
- # Safely converts a unix format string into a DateTime object
138
- # @param [String] The unix formatted datetime string
139
- # @return [DateTime] A DateTime object
140
- def self.from_unix(date_time)
141
- Time.at(date_time.to_i).utc.to_datetime
142
- end
143
-
144
- # Safely converts a rfc3339 format string into a DateTime object
145
- # @param [String] The rfc3339 formatted datetime string
146
- # @return [DateTime] A DateTime object
147
- def self.from_rfc3339(date_time)
148
- # missing timezone information
149
- if date_time.end_with?('Z') || date_time.index('+')
150
- DateTime.rfc3339(date_time)
151
- else
152
- DateTime.rfc3339("#{date_time}Z")
153
- end
154
- end
9
+ class DateTimeHelper < CoreLibrary::DateTimeHelper
155
10
  end
156
11
  end
@@ -5,12 +5,12 @@
5
5
 
6
6
  module ApimaticCalculator
7
7
  # A utility to allow users to set the content-type for files
8
- class FileWrapper
9
- attr_reader :content_type, :file
10
-
8
+ class FileWrapper < CoreLibrary::FileWrapper
9
+ # The constructor.
10
+ # @param [File] file The file to be sent in the request.
11
+ # @param [string] content_type The content type of the provided file.
11
12
  def initialize(file, content_type: 'application/octet-stream')
12
- @file = file
13
- @content_type = content_type
13
+ super
14
14
  end
15
15
  end
16
16
  end
@@ -5,9 +5,12 @@
5
5
 
6
6
  require 'date'
7
7
  require 'json'
8
- require 'faraday'
9
- require 'certifi'
10
- require 'logging'
8
+ require 'apimatic_core_interfaces'
9
+ require 'apimatic_core'
10
+ require 'apimatic_faraday_client_adapter'
11
+ # rubocop:disable Style/MixinUsage
12
+ include CoreLibrary
13
+ # rubocop:enable Style/MixinUsage
11
14
 
12
15
  require_relative 'apimatic_calculator/api_helper'
13
16
  require_relative 'apimatic_calculator/client'
@@ -18,8 +21,6 @@ require_relative 'apimatic_calculator/utilities/date_time_helper'
18
21
 
19
22
  # Http
20
23
  require_relative 'apimatic_calculator/http/http_call_back'
21
- require_relative 'apimatic_calculator/http/http_client'
22
- require_relative 'apimatic_calculator/http/faraday_client'
23
24
  require_relative 'apimatic_calculator/http/http_method_enum'
24
25
  require_relative 'apimatic_calculator/http/http_request'
25
26
  require_relative 'apimatic_calculator/http/http_response'
@@ -30,7 +31,6 @@ require_relative 'apimatic_calculator/models/operation_type_enum'
30
31
 
31
32
  # Exceptions
32
33
  require_relative 'apimatic_calculator/exceptions/api_exception'
33
- require_relative 'apimatic_calculator/exceptions/validation_exception'
34
34
 
35
35
  require_relative 'apimatic_calculator/configuration'
36
36
 
@@ -9,7 +9,6 @@ require 'minitest/hell'
9
9
  require 'minitest/pride'
10
10
  require 'minitest/proveit'
11
11
  require 'apimatic_calculator'
12
- require_relative '../test_helper'
13
12
  require_relative '../http_response_catcher'
14
13
 
15
14
  class ControllerTestBase < Minitest::Test
@@ -17,5 +16,13 @@ class ControllerTestBase < Minitest::Test
17
16
  include ApimaticCalculator
18
17
 
19
18
  # Create configuration and set any test parameters
20
- CONFIG = Configuration.new
19
+ def create_configuration
20
+ Configuration.new(http_callback: HttpResponseCatcher.new)
21
+ end
22
+
23
+ # Initializes the base test controller
24
+ def setup_class
25
+ _config = create_configuration
26
+ @client = Client.new(config: _config)
27
+ end
21
28
  end
@@ -8,8 +8,9 @@ require_relative 'controller_test_base'
8
8
  class SimpleCalculatorControllerTests < ControllerTestBase
9
9
  # Called only once for the class before any test has executed
10
10
  def setup
11
- @response_catcher = HttpResponseCatcher.new
12
- @controller = SimpleCalculatorController.new CONFIG, http_call_back: @response_catcher
11
+ setup_class
12
+ @controller = @client.simple_calculator
13
+ @response_catcher = @controller.http_call_back
13
14
  end
14
15
 
15
16
  # Check if multiplication works