Class: GLogin::Cookie::Open

Inherits:
Object
  • Object
show all
Defined in:
lib/glogin/cookie.rb

Overview

Open

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json, secret, context = '') ⇒ Open

Here comes the JSON you receive from Auth.user().

The JSON is a Hash where every key is a string. When the class is instantiated, its methods ‘id`, `login`, and `avatar_url` may be used to retrieve the data inside the JSON, but this is not what this class is mainly about.

The method to_s returns an encrypted cookie string, that may be sent to the user as a Set-Cookie HTTP header.



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/glogin/cookie.rb', line 91

def initialize(json, secret, context = '')
  raise 'JSON can\'t be nil' if json.nil?
  raise 'JSON must contain "id" key' if json['id'].nil?
  @id = json['id'].to_s
  @login = (json['login'] || '').to_s
  @avatar_url = (json['avatar_url'] || '').to_s
  @bearer = (json['bearer'] || '').to_s
  raise 'Secret can\'t be nil' if secret.nil?
  @secret = secret
  raise 'Context can\'t be nil' if context.nil?
  @context = context.to_s
end

Instance Attribute Details

#avatar_urlObject (readonly)

Returns the value of attribute avatar_url.



81
82
83
# File 'lib/glogin/cookie.rb', line 81

def avatar_url
  @avatar_url
end

#idObject (readonly)

Returns the value of attribute id.



81
82
83
# File 'lib/glogin/cookie.rb', line 81

def id
  @id
end

#loginObject (readonly)

Returns the value of attribute login.



81
82
83
# File 'lib/glogin/cookie.rb', line 81

def 
  @login
end

Instance Method Details

#to_sObject

Returns the text you should drop back to the user as a cookie.



105
106
107
108
109
110
111
112
113
114
# File 'lib/glogin/cookie.rb', line 105

def to_s
  Codec.new(@secret).encrypt(
    [
      @id,
      @login,
      @avatar_url,
      @context
    ].join(GLogin::SPLIT)
  )
end