class Iyzi::Request

Constants

AUTHORIZATION_HEADER_NAME
AUTHORIZATION_HEADER_STRING
DEFAULT_LOCALE
RANDOM_HEADER_NAME

Attributes

config[RW]
method[RW]
options[RW]
path[RW]
pki[RW]
random_string[RW]

Public Class Methods

new(method, path, options = {}) click to toggle source
# File lib/iyzi/request.rb, line 13
def initialize(method, path, options = {})
  @method = method
  @path   = path
  # use default config which comes from initial setup
  # you can also send custom config object which you'd like to use
  @config             = options.delete(:config) || Iyzi.configuration
  @options            = options
  @options[:locale]   = options[:locale] || DEFAULT_LOCALE
  @options[:currency] = Currency.find(options[:currency]) if options[:currency].present?

  # In #add method of `PkiBuilder`, we ignore empty strings
  # So, in order to get valid signature, we need to make sure that
  # there is no empty value in request body
  @options.delete_if { |key, value| value.is_a?(String) && value.empty? }

  @pki                = to_pki
  @random_string      = secure_random_string
  # config must have all required params
  config.validate if has_pki?
end

Public Instance Methods

auth_header_string() click to toggle source
# File lib/iyzi/request.rb, line 69
def auth_header_string
  format(AUTHORIZATION_HEADER_STRING, config.api_key, request_hash_digest)
end
auth_headers() click to toggle source
# File lib/iyzi/request.rb, line 62
def auth_headers
  {
    AUTHORIZATION_HEADER_NAME => auth_header_string,
    RANDOM_HEADER_NAME        => random_string
  }
end
call() click to toggle source
# File lib/iyzi/request.rb, line 47
def call
  connection.send(method) do |req|
    req.url path
    req.headers['Accept'] = 'application/json'
    req.headers['Content-Type'] = 'application/json'
    req.headers.merge!(auth_headers) if has_pki?
    req.body = iyzi_options
  end
end
connection() click to toggle source
# File lib/iyzi/request.rb, line 38
def connection
  @conn ||= Faraday.new(url: config.base_url) do |faraday|
    faraday.request  :json
    faraday.response :logger if ENV['DEBUG']
    faraday.response :json, content_type: /\bjson$/
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
  end
end
has_pki?() click to toggle source
# File lib/iyzi/request.rb, line 85
def has_pki?
  !pki.nil? && !pki.to_s.empty?
end
iyzi_options() click to toggle source
# File lib/iyzi/request.rb, line 34
def iyzi_options
  Utils.hash_to_properties(options.compact)
end
params_will_be_hashed() click to toggle source
# File lib/iyzi/request.rb, line 77
def params_will_be_hashed
  config.api_key + random_string + config.secret + pki
end
request_hash_digest() click to toggle source
# File lib/iyzi/request.rb, line 73
def request_hash_digest
  Digest::SHA1.base64digest(params_will_be_hashed)
end
response() { |response| ... } click to toggle source
# File lib/iyzi/request.rb, line 57
def response
  @response ||= Utils.properties_to_hash(call.body)
  block_given? ? yield(@response) : @response
end
secure_random_string() click to toggle source
# File lib/iyzi/request.rb, line 81
def secure_random_string
  SecureRandom.urlsafe_base64(6, false)
end
to_pki() click to toggle source

implement if needed

# File lib/iyzi/request.rb, line 90
def to_pki
  ''
end