Class: CloudDoor::Token
- Inherits:
-
Object
- Object
- CloudDoor::Token
- Defined in:
- lib/cloud_door/token.rb
Constant Summary collapse
- TOKEN_ITEMS =
[ 'token_type', 'expires_in', 'scope', 'access_token', 'refresh_token', 'user_id', 'credentials' ]
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#credentials ⇒ Object
Returns the value of attribute credentials.
-
#data_path ⇒ Object
readonly
Returns the value of attribute data_path.
-
#expires_in ⇒ Object
Returns the value of attribute expires_in.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#token_file ⇒ Object
Returns the value of attribute token_file.
-
#token_name ⇒ Object
Returns the value of attribute token_name.
-
#token_type ⇒ Object
Returns the value of attribute token_type.
-
#user_id ⇒ Object
Returns the value of attribute user_id.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(token_name, data_path, id = nil) ⇒ Token
constructor
A new instance of Token.
- #set_attributes(attributes) ⇒ Object
- #set_locate(id) ⇒ Object
- #write_token ⇒ Object
Constructor Details
#initialize(token_name, data_path, id = nil) ⇒ Token
Returns a new instance of Token.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/cloud_door/token.rb', line 17 def initialize(token_name, data_path, id = nil) @token_name = token_name @data_path = data_path if id.nil? @token_file = @data_path + @token_name else token_dir = @data_path + "#{id}" unless File.exists?(token_dir) Dir.mkdir(token_dir) end @token_file = "#{token_dir}/#{@token_name}" end end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
3 4 5 |
# File 'lib/cloud_door/token.rb', line 3 def access_token @access_token end |
#credentials ⇒ Object
Returns the value of attribute credentials.
3 4 5 |
# File 'lib/cloud_door/token.rb', line 3 def credentials @credentials end |
#data_path ⇒ Object (readonly)
Returns the value of attribute data_path.
5 6 7 |
# File 'lib/cloud_door/token.rb', line 5 def data_path @data_path end |
#expires_in ⇒ Object
Returns the value of attribute expires_in.
3 4 5 |
# File 'lib/cloud_door/token.rb', line 3 def expires_in @expires_in end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
3 4 5 |
# File 'lib/cloud_door/token.rb', line 3 def refresh_token @refresh_token end |
#scope ⇒ Object
Returns the value of attribute scope.
3 4 5 |
# File 'lib/cloud_door/token.rb', line 3 def scope @scope end |
#token_file ⇒ Object
Returns the value of attribute token_file.
3 4 5 |
# File 'lib/cloud_door/token.rb', line 3 def token_file @token_file end |
#token_name ⇒ Object
Returns the value of attribute token_name.
3 4 5 |
# File 'lib/cloud_door/token.rb', line 3 def token_name @token_name end |
#token_type ⇒ Object
Returns the value of attribute token_type.
3 4 5 |
# File 'lib/cloud_door/token.rb', line 3 def token_type @token_type end |
#user_id ⇒ Object
Returns the value of attribute user_id.
3 4 5 |
# File 'lib/cloud_door/token.rb', line 3 def user_id @user_id end |
Class Method Details
.load_token(token_name, data_path, id = nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cloud_door/token.rb', line 53 def self.load_token(token_name, data_path, id = nil) if id.nil? token_file = data_path + token_name else token_file = data_path + "#{id}/#{token_name}" end return nil unless File.exist?(token_file) marshal = File.open(token_file).read token = Marshal.load(marshal) return nil unless token.is_a?(Token) token rescue nil end |
Instance Method Details
#set_attributes(attributes) ⇒ Object
39 40 41 42 43 |
# File 'lib/cloud_door/token.rb', line 39 def set_attributes(attributes) TOKEN_ITEMS.each do |item| instance_variable_set("@#{item}", attributes[item]) if attributes.key?(item) end end |
#set_locate(id) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/cloud_door/token.rb', line 31 def set_locate(id) token_dir = @data_path + "#{id}" unless File.exists?(token_dir) Dir.mkdir(token_dir) end @token_file = "#{token_dir}/#{@token_name}" end |
#write_token ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/cloud_door/token.rb', line 45 def write_token marshal = Marshal.dump(self) open(@token_file, 'wb') { |file| file << marshal } true rescue false end |