class PayCertify::Insurance

Constants

API_ENDPOINT
MANDATORY_FIELDS
OPTIONAL_FIELDS

Attributes

api_public_key[RW]
api_secret_key[RW]
client_id[RW]
token[RW]
attributes[RW]
errors[RW]
response[RW]

Public Class Methods

configure() { |self| ... } click to toggle source
# File lib/paycertify/insurance.rb, line 55
def configure(&block)
  yield self if block_given?

  connection = Faraday.new(url: API_ENDPOINT, ssl: {verify: false}) do |faraday|
    faraday.request :url_encoded
    faraday.response :logger
    faraday.adapter  Faraday.default_adapter
  end

  response = connection.get do |request|
    request.url 'api/v1/token'
    request.headers['api-public-key'] = api_public_key
    request.headers['api-secret-key'] = api_secret_key
    request.headers['api-client-id']  = client_id
  end

  json = JSON.parse(response.body)
  
  self.token = json['jwt']

  return {
    api_public_key: api_public_key,
    api_secret_key: api_secret_key,
    client_id: client_id,
    token: token
  }
end
new(attributes) click to toggle source
# File lib/paycertify/insurance.rb, line 22
def initialize(attributes)
  raise NoCredentialsError, 'No token found for api_client/secret/client_id combination.' unless token.present?

  self.attributes = HashWithIndifferentAccess.new(attributes)
  self.errors = {}

  validate!
end

Public Instance Methods

save!() click to toggle source
# File lib/paycertify/insurance.rb, line 35
def save!
  data = attributes.slice *[MANDATORY_FIELDS + OPTIONAL_FIELDS].flatten
  data[:ship_to_billing_addr] = true unless data[:shipping_address].present?

  api_response = connection.post do |request|
    request.url path_for('orders')
    request.headers['Content-Type'] = 'application/json'
    request.headers['Authorization'] = "JWT #{token}"
    request.body = JSON.generate(data)
  end

  self.response = Response.new(api_response)
  self.errors = errors.merge(response) unless response.success?

  response
end
success?() click to toggle source
# File lib/paycertify/insurance.rb, line 31
def success?
  response.success?
end

Private Instance Methods

connection() click to toggle source
# File lib/paycertify/insurance.rb, line 106
def connection
  @connection ||= Faraday.new(url: API_ENDPOINT, ssl: {verify: false}) do |faraday|
    faraday.request :url_encoded
    faraday.response :logger
    faraday.adapter  Faraday.default_adapter
  end
end
path_for(path) click to toggle source
# File lib/paycertify/insurance.rb, line 114
def path_for(path)
  return "api/v1/#{client_id}/#{path}/"
end
validate!() click to toggle source
# File lib/paycertify/insurance.rb, line 98
def validate!
  MANDATORY_FIELDS.each do |field|
    unless attributes[field].present?
      self.errors[field] = "Required attribute not present"
    end
  end
end