Class: SignedGlobalID

Inherits:
GlobalID show all
Defined in:
lib/global_id/signed_global_id.rb

Defined Under Namespace

Classes: ExpiredMessage

Constant Summary collapse

DEFAULT_PURPOSE =
"default"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from GlobalID

#uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GlobalID

#as_json, create, deprecator, eager_load!, find, #find, #hash, #model_class

Constructor Details

#initialize(gid, options = {}) ⇒ SignedGlobalID

Returns a new instance of SignedGlobalID.



59
60
61
62
63
64
# File 'lib/global_id/signed_global_id.rb', line 59

def initialize(gid, options = {})
  super
  @verifier = self.class.pick_verifier(options)
  @purpose = self.class.pick_purpose(options)
  @expires_at = pick_expiration(options)
end

Class Attribute Details

.expires_inObject

Returns the value of attribute expires_in.



8
9
10
# File 'lib/global_id/signed_global_id.rb', line 8

def expires_in
  @expires_in
end

.verifierObject

Returns the value of attribute verifier.



8
9
10
# File 'lib/global_id/signed_global_id.rb', line 8

def verifier
  @verifier
end

Instance Attribute Details

#expires_atObject (readonly)

Returns the value of attribute expires_at.



57
58
59
# File 'lib/global_id/signed_global_id.rb', line 57

def expires_at
  @expires_at
end

#purposeObject (readonly)

Returns the value of attribute purpose.



57
58
59
# File 'lib/global_id/signed_global_id.rb', line 57

def purpose
  @purpose
end

#verifierObject (readonly)

Returns the value of attribute verifier.



57
58
59
# File 'lib/global_id/signed_global_id.rb', line 57

def verifier
  @verifier
end

Class Method Details

.parse(sgid, options = {}) ⇒ Object



10
11
12
# File 'lib/global_id/signed_global_id.rb', line 10

def parse(sgid, options = {})
  super verify(sgid.to_s, options), options
end

.pick_purpose(options) ⇒ Object



24
25
26
# File 'lib/global_id/signed_global_id.rb', line 24

def pick_purpose(options)
  options.fetch :for, DEFAULT_PURPOSE
end

.pick_verifier(options) ⇒ Object

Grab the verifier from options and fall back to SignedGlobalID.verifier. Raise ArgumentError if neither is available.



16
17
18
19
20
# File 'lib/global_id/signed_global_id.rb', line 16

def pick_verifier(options)
  options.fetch :verifier do
    verifier || raise(ArgumentError, 'Pass a `verifier:` option with an `ActiveSupport::MessageVerifier` instance, or set a default SignedGlobalID.verifier.')
  end
end

Instance Method Details

#==(other) ⇒ Object



71
72
73
# File 'lib/global_id/signed_global_id.rb', line 71

def ==(other)
  super && @purpose == other.purpose
end

#inspectObject

:nodoc:



75
76
77
# File 'lib/global_id/signed_global_id.rb', line 75

def inspect # :nodoc:
  "#<#{self.class.name}:#{'%#016x' % (object_id << 1)}>"
end

#to_sObject Also known as: to_param



66
67
68
# File 'lib/global_id/signed_global_id.rb', line 66

def to_s
  @sgid ||= @verifier.generate(@uri.to_s, purpose: purpose, expires_at: expires_at)
end