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

#app, #model_id, #model_name, #uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GlobalID

create, find, #find, #model_class, validate_app

Constructor Details

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

Returns a new instance of SignedGlobalID.



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

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.



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

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.



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

def expires_at
  @expires_at
end

#purposeObject (readonly)

Returns the value of attribute purpose.



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

def purpose
  @purpose
end

#verifierObject (readonly)

Returns the value of attribute verifier.



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

def verifier
  @verifier
end

Class Method Details

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



11
12
13
14
15
16
17
# File 'lib/global_id/signed_global_id.rb', line 11

def parse(sgid, options = {})
  if sgid.is_a? self
    sgid
  else
    super verify(sgid, options), options
  end
end

.pick_purpose(options) ⇒ Object



31
32
33
# File 'lib/global_id/signed_global_id.rb', line 31

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.



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

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



73
74
75
# File 'lib/global_id/signed_global_id.rb', line 73

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

#to_hObject



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

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



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

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