Class: ApimaticCalculator::SimpleCalculatorController

Inherits:
BaseController
  • Object
show all
Defined in:
lib/apimatic_calculator/controllers/simple_calculator_controller.rb

Overview

SimpleCalculatorController

Instance Attribute Summary

Attributes inherited from BaseController

#config, #http_call_back

Instance Method Summary collapse

Methods inherited from BaseController

#execute_request, #validate_parameters, #validate_response

Constructor Details

#initialize(config, http_call_back: nil) ⇒ SimpleCalculatorController

Returns a new instance of SimpleCalculatorController.



9
10
11
# File 'lib/apimatic_calculator/controllers/simple_calculator_controller.rb', line 9

def initialize(config, http_call_back: nil)
  super(config, http_call_back: http_call_back)
end

Instance Method Details

#get_calculate(options = {}) ⇒ Float

Calculates the expression using the specified operation. apply on the variables

Parameters:

  • operation (OperationTypeEnum)

    Required parameter: The operator to

  • x (Float)

    Required parameter: The LHS value

  • y (Float)

    Required parameter: The RHS value

Returns:

  • (Float)

    response from the API call



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/apimatic_calculator/controllers/simple_calculator_controller.rb', line 19

def get_calculate(options = {})
  # Prepare query url.
  _query_builder = config.get_base_uri
  _query_builder << '/{operation}'
  _query_builder = APIHelper.append_url_with_template_parameters(
    _query_builder,
    'operation' => { 'value' => options['operation'], 'encode' => true }
  )
  _query_builder = APIHelper.append_url_with_query_parameters(
    _query_builder,
    'x' => options['x'],
    'y' => options['y']
  )
  _query_url = APIHelper.clean_url _query_builder

  # Prepare and execute HttpRequest.
  _request = config.http_client.get(
    _query_url
  )
  _response = execute_request(_request)
  validate_response(_response)

  # Return appropriate response type.
  _response.raw_body.to_f
end