class TdTip::Models::Response

Response - performs request and handle response

Attributes

amount_with_tip[R]
currency[R]
error[R]
parameters[RW]
tip[R]

Public Class Methods

new(parameters) click to toggle source
# File lib/td_tip/models/response.rb, line 20
def initialize(parameters)
  @parameters = parameters
end

Public Instance Methods

get() click to toggle source

Performs request and handle response

# File lib/td_tip/models/response.rb, line 25
def get
  result = parse_and_symbolize_json
  @amount_with_tip = result[:amount_with_tip]
  @tip = result[:tip]
  @error = result[:error]
  @currency = parameters.currency
  self
end

Private Instance Methods

calculate_request() click to toggle source

Performs post request to Web Service

# File lib/td_tip/models/response.rb, line 49
def calculate_request
  self.class.post WS_METHOD,
                  query: parameters.to_params,
                  timeout: WS_TIMEOUT
end
other_errors() click to toggle source

Adds additional validation

# File lib/td_tip/models/response.rb, line 37
def other_errors
  errors.add(:error, error) unless error.blank?
end
parse_and_symbolize_json() click to toggle source

Process request response

# File lib/td_tip/models/response.rb, line 42
def parse_and_symbolize_json
  with_error_handling do
    JSON.parse(calculate_request.body).symbolize_keys!
  end
end
with_error_handling() { || ... } click to toggle source
# File lib/td_tip/models/response.rb, line 55
def with_error_handling
  yield
rescue HTTParty::Error, Net::ReadTimeout, JSON::ParserError => e
  { error: e.message }
end