Class: Sigil::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sigil/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, key) ⇒ Base

Returns a new instance of Base.

Raises:



6
7
8
9
10
11
12
# File 'lib/sigil/base.rb', line 6

def initialize(params, key)
  raise Sigil::Error, "Params must be a Hash" unless params.kind_of?(Hash)

  @params = params.to_options!
  @key = key.to_s
  @signature = nil
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



4
5
6
# File 'lib/sigil/base.rb', line 4

def key
  @key
end

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'lib/sigil/base.rb', line 4

def params
  @params
end

#signatureObject (readonly)

Returns the value of attribute signature.



4
5
6
# File 'lib/sigil/base.rb', line 4

def signature
  @signature
end

Class Method Details

.sign(string, key) ⇒ Object



26
27
28
29
# File 'lib/sigil/base.rb', line 26

def self.sign(string, key)
  hmacd = OpenSSL::HMAC.new(key, OpenSSL::Digest::SHA1.new)
  hmacd.update(string)
end

Instance Method Details

#to_queryObject



14
15
16
# File 'lib/sigil/base.rb', line 14

def to_query
  @params.to_query
end

#verify(provided_signature) ⇒ Object



31
32
33
34
35
# File 'lib/sigil/base.rb', line 31

def verify(provided_signature)
  verify!(provided_signature)
rescue Sigil::Error
  false
end

#verify!(provided_signature) ⇒ Object

Raises:



37
38
39
40
41
42
43
# File 'lib/sigil/base.rb', line 37

def verify!(provided_signature)
  raise Sigil::Error, "Params not set" if params.empty?
  raise Sigil::Error, "Signature not set" if provided_signature.blank?
  raise Sigil::Error, "Signature does not match" unless signature == provided_signature

  true
end