Skip to content

Commit

Permalink
Refactor Methods::ActivateMethod to improve readability of digest met…
Browse files Browse the repository at this point in the history
…hod.
  • Loading branch information
damselem committed Jul 21, 2013
1 parent a85cf27 commit ea01fa9
Showing 1 changed file with 38 additions and 31 deletions.
69 changes: 38 additions & 31 deletions lib/klarna/methods/activate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,60 @@ def self.name
def self.params(store_id, store_secret, api_version, client_name, params)
[
store_id,
digest(store_id, store_secret, api_version, client_name, params),
digest(api_version, client_name, store_id, params[:rno], params[:optional_info], store_secret),
params[:rno],
params.fetch(:optional_info, {})
]
end

private

OPTIONAL_INFO_KEYS =
[
:bclass,
:cust_no,
:flags,
:ocr,
:orderid1,
:orderid2,
:reference,
:reference_code
]
def self.digest(api_version, client_name, store_id, rno, optional_info, store_secret)
version = api_version.gsub('.', ':')
optional_info_for_digest = optional_info_for_digest(optional_info)

def self.digest(store_id, store_secret, api_version, client_name, params)
digest_array = [api_version.gsub(".", ":"), client_name, store_id, params[:rno]]
message = [version, client_name, store_id, rno, *optional_info_for_digest, store_secret].join(':')
Digest::SHA512.base64digest(message)
end

optional_info = params[:optional_info]
add_optional_info(digest_array, optional_info) unless optional_info.nil?

digest_array.push(store_secret)
OPTIONAL_INFO_KEYS = [
:bclass,
:cust_no,
:flags,
:ocr,
:orderid1,
:orderid2,
:reference,
:reference_code
]

message = digest_array.join(':')
Digest::SHA512.base64digest(message)
end
def self.optional_info_for_digest(optional_info)
array = []

def self.add_optional_info(digest_array, optional_info)
OPTIONAL_INFO_KEYS.each do |key|
if optional_info.has_key?(key)
digest_array.push(optional_info[key])
end
if optional_info
array.concat optional_info_values(optional_info)
array.concat articles(optional_info)
end

if optional_info.has_key?(:artnos)
optional_info[:artnos].each do |article|
digest_array.push(article[:artno])
digest_array.push(article[:qty])
end
array
end

def self.optional_info_values(optional_info)
optional_info.values_at(*OPTIONAL_INFO_KEYS).compact
end

def self.articles(optional_info)
articles = []

optional_info[:artnos].each do |article|
articles.push article[:artno]
articles.push article[:qty]
end

articles
end

end
end
end
end

0 comments on commit ea01fa9

Please sign in to comment.