Class: Midpay::SignableHash

Inherits:
Hash
  • Object
show all
Includes:
HashExtensions
Defined in:
lib/midpay/signable_hash.rb

Instance Method Summary collapse

Methods included from HashExtensions

included

Instance Method Details

#algorithm_const(algorithm) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/midpay/signable_hash.rb', line 32

def algorithm_const algorithm
  algorithm = self[algorithm] if key?(algorithm) && self[algorithm]
  begin
    ::Digest.const_get(algorithm.to_s.upcase)
  rescue
    #
  end
end

#merge_if!(hash) ⇒ Object



25
26
27
28
29
30
# File 'lib/midpay/signable_hash.rb', line 25

def merge_if! hash
  hash.each do |i|
    self[i[0]] = i[1] unless self.key?(i[0])
  end if hash.respond_to?(:each)
  self
end

#sign(algorithm, &block) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/midpay/signable_hash.rb', line 7

def sign algorithm, &block
  data = yield(self.dup)
  begin
    algorithm_const(algorithm).send(:hexdigest, data)
  rescue
    nil
  end
end

#sign!(key, algorithm, &block) ⇒ Object



16
17
18
19
# File 'lib/midpay/signable_hash.rb', line 16

def sign! key, algorithm, &block
  self[key] = self.sign(algorithm, &block)
  self
end

#to_queryObject



21
22
23
# File 'lib/midpay/signable_hash.rb', line 21

def to_query
  collect{|k,v| "#{::URI.encode_www_form_component(k.to_s)}=#{::URI.encode_www_form_component(v.to_s)}" }.sort.join("&")
end