Class: CloudDoor::Token

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_tokenObject

Returns the value of attribute access_token.



3
4
5
# File 'lib/cloud_door/token.rb', line 3

def access_token
  @access_token
end

#credentialsObject

Returns the value of attribute credentials.



3
4
5
# File 'lib/cloud_door/token.rb', line 3

def credentials
  @credentials
end

#data_pathObject (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_inObject

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_tokenObject

Returns the value of attribute refresh_token.



3
4
5
# File 'lib/cloud_door/token.rb', line 3

def refresh_token
  @refresh_token
end

#scopeObject

Returns the value of attribute scope.



3
4
5
# File 'lib/cloud_door/token.rb', line 3

def scope
  @scope
end

#token_fileObject

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_nameObject

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_typeObject

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_idObject

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_tokenObject



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