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

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

Constructor Details

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

Returns a new instance of SignedGlobalID.


51
52
53
54
55
56
# File 'lib/global_id/signed_global_id.rb', line 51

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


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

def expires_in
  @expires_in
end

.verifierObject

Returns the value of attribute verifier


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

def verifier
  @verifier
end

Instance Attribute Details

#expires_atObject (readonly)

Returns the value of attribute expires_at


49
50
51
# File 'lib/global_id/signed_global_id.rb', line 49

def expires_at
  @expires_at
end

#purposeObject (readonly)

Returns the value of attribute purpose


49
50
51
# File 'lib/global_id/signed_global_id.rb', line 49

def purpose
  @purpose
end

#verifierObject (readonly)

Returns the value of attribute verifier


49
50
51
# File 'lib/global_id/signed_global_id.rb', line 49

def verifier
  @verifier
end

Class Method Details

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


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

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

.pick_purpose(options) ⇒ Object


27
28
29
# File 'lib/global_id/signed_global_id.rb', line 27

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.


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

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


69
70
71
# File 'lib/global_id/signed_global_id.rb', line 69

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

#to_hObject


63
64
65
66
67
# File 'lib/global_id/signed_global_id.rb', line 63

def to_h
  # Some serializers decodes symbol keys to symbols, others to strings.
  # Using string keys remedies that.
  { 'gid' => @uri.to_s, 'purpose' => purpose, 'expires_at' => encoded_expiration }
end

#to_sObject Also known as: to_param


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

def to_s
  @sgid ||= @verifier.generate(to_h)
end