Class: Iyzi::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/iyzi/request.rb

Constant Summary collapse

AUTHORIZATION_HEADER_NAME =
'Authorization'.freeze
RANDOM_HEADER_NAME =
'x-iyzi-rnd'.freeze
AUTHORIZATION_HEADER_STRING =
'IYZWS %s:%s'.freeze
DEFAULT_LOCALE =
'tr'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, path, options = {}) ⇒ Request

Returns a new instance of Request.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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

Instance Attribute Details

#configObject

Returns the value of attribute config.



11
12
13
# File 'lib/iyzi/request.rb', line 11

def config
  @config
end

#methodObject

Returns the value of attribute method.



11
12
13
# File 'lib/iyzi/request.rb', line 11

def method
  @method
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/iyzi/request.rb', line 11

def options
  @options
end

#pathObject

Returns the value of attribute path.



11
12
13
# File 'lib/iyzi/request.rb', line 11

def path
  @path
end

#pkiObject

Returns the value of attribute pki.



11
12
13
# File 'lib/iyzi/request.rb', line 11

def pki
  @pki
end

#random_stringObject

Returns the value of attribute random_string.



11
12
13
# File 'lib/iyzi/request.rb', line 11

def random_string
  @random_string
end

Instance Method Details

#auth_header_stringObject



69
70
71
# File 'lib/iyzi/request.rb', line 69

def auth_header_string
  format(AUTHORIZATION_HEADER_STRING, config.api_key, request_hash_digest)
end

#auth_headersObject



62
63
64
65
66
67
# File 'lib/iyzi/request.rb', line 62

def auth_headers
  {
    AUTHORIZATION_HEADER_NAME => auth_header_string,
    RANDOM_HEADER_NAME        => random_string
  }
end

#callObject



47
48
49
50
51
52
53
54
55
# 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

#connectionObject



38
39
40
41
42
43
44
45
# 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?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/iyzi/request.rb', line 85

def has_pki?
  !pki.nil? && !pki.to_s.empty?
end

#iyzi_optionsObject



34
35
36
# File 'lib/iyzi/request.rb', line 34

def iyzi_options
  Utils.hash_to_properties(options.compact)
end

#params_will_be_hashedObject



77
78
79
# File 'lib/iyzi/request.rb', line 77

def params_will_be_hashed
  config.api_key + random_string + config.secret + pki
end

#request_hash_digestObject



73
74
75
# File 'lib/iyzi/request.rb', line 73

def request_hash_digest
  Digest::SHA1.base64digest(params_will_be_hashed)
end

#responseObject



57
58
59
60
# File 'lib/iyzi/request.rb', line 57

def response
  @response ||= Utils.properties_to_hash(call.body)
  block_given? ? yield(@response) : @response
end

#secure_random_stringObject



81
82
83
# File 'lib/iyzi/request.rb', line 81

def secure_random_string
  SecureRandom.urlsafe_base64(6, false)
end

#to_pkiObject

implement if needed



90
91
92
# File 'lib/iyzi/request.rb', line 90

def to_pki
  ''
end