Class: ChefAnalytics::Token
- Inherits:
-
Object
- Object
- ChefAnalytics::Token
- Includes:
- Chef::Mixin::ParamsValidate
- Defined in:
- lib/chef-analytics/token.rb
Class Method Summary collapse
-
.from_file(filename) ⇒ Object
Class methods.
- .from_hash(hash) ⇒ Object
- .from_json(json) ⇒ Object (also: json_create)
Instance Method Summary collapse
- #access_token(arg = nil) ⇒ Object
- #authentication_header ⇒ Object
- #client_name(arg = nil) ⇒ Object
- #expired? ⇒ Boolean
- #expiry(arg = nil) ⇒ Object
- #identity_server_url(arg = nil) ⇒ Object
-
#initialize ⇒ Token
constructor
A new instance of Token.
- #inspect ⇒ Object
- #refresh_token(arg = nil) ⇒ Object
- #to_file(file_name) ⇒ Object
- #to_hash ⇒ Object
- #to_json(*a) ⇒ Object
- #to_s ⇒ Object
- #token_type(arg = nil) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize ⇒ Token
Returns a new instance of Token.
27 28 29 30 31 32 33 34 |
# File 'lib/chef-analytics/token.rb', line 27 def initialize @access_token = nil @token_type = nil @refresh_token = nil @expiry = nil @identity_server_url = nil @client_name = nil end |
Class Method Details
.from_file(filename) ⇒ Object
Class methods
117 118 119 120 121 122 123 124 |
# File 'lib/chef-analytics/token.rb', line 117 def self.from_file(filename) if File.exists?(filename) && File.readable?(filename) contents = IO.read(filename) ChefAnalytics::Token.from_json(contents) else raise IOError, "Cannot open or read #{filename}!" end end |
.from_hash(hash) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/chef-analytics/token.rb', line 130 def self.from_hash(hash) token = ChefAnalytics::Token.new token.access_token hash['access_token'] token.token_type hash['token_type'] token.refresh_token hash['refresh_token'] if hash.has_key?('expires_in') token.expiry DateTime.now + Rational(hash['expires_in'],86400) else token.expiry DateTime.rfc3339(hash['expiry']) end token.identity_server_url hash['identity_server_url'] if hash['identity_server_url'] token.client_name hash['client_name'] if hash['client_name'] token end |
.from_json(json) ⇒ Object Also known as: json_create
126 127 128 |
# File 'lib/chef-analytics/token.rb', line 126 def self.from_json(json) ChefAnalytics::Token.from_hash(Chef::JSONCompat.from_json(json)) end |
Instance Method Details
#access_token(arg = nil) ⇒ Object
36 37 38 39 |
# File 'lib/chef-analytics/token.rb', line 36 def access_token(arg=nil) set_or_return(:access_token, arg, :kind_of => String) end |
#authentication_header ⇒ Object
67 68 69 |
# File 'lib/chef-analytics/token.rb', line 67 def authentication_header {"Authorization" => "Bearer #{access_token}"} end |
#client_name(arg = nil) ⇒ Object
61 62 63 64 |
# File 'lib/chef-analytics/token.rb', line 61 def client_name(arg=nil) set_or_return(:client_name, arg, :kind_of => String) end |
#expired? ⇒ Boolean
106 107 108 |
# File 'lib/chef-analytics/token.rb', line 106 def expired? @expiry < DateTime.now end |
#expiry(arg = nil) ⇒ Object
51 52 53 54 |
# File 'lib/chef-analytics/token.rb', line 51 def expiry(arg=nil) set_or_return(:expiry, arg, :kind_of => DateTime) end |
#identity_server_url(arg = nil) ⇒ Object
56 57 58 59 |
# File 'lib/chef-analytics/token.rb', line 56 def identity_server_url(arg=nil) set_or_return(:identity_server_url, arg, :kind_of => String) end |
#inspect ⇒ Object
101 102 103 104 |
# File 'lib/chef-analytics/token.rb', line 101 def inspect "ChefAnalytics::Token access_token:'#{access_token}' token_type:'#{token_type}' " + "refresh_token:'#{refresh_token}' expiry:'#{expiry.rfc3339}'" end |
#refresh_token(arg = nil) ⇒ Object
46 47 48 49 |
# File 'lib/chef-analytics/token.rb', line 46 def refresh_token(arg=nil) set_or_return(:refresh_token, arg, :kind_of => String) end |
#to_file(file_name) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/chef-analytics/token.rb', line 86 def to_file(file_name) Chef::Log.debug("Saving token to #{file_name}") dir = ::File.dirname(file_name) ::FileUtils.mkdir_p(dir) #object = to_json File.open(file_name, "w") do |newfile| newfile.puts Chef::JSONCompat.to_json_pretty(self) newfile.flush end end |
#to_hash ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/chef-analytics/token.rb', line 71 def to_hash { 'access_token' => access_token, 'token_type' => token_type, 'refresh_token' => refresh_token, 'expiry' => expiry.rfc3339, 'client_name' => client_name, 'identity_server_url' => identity_server_url } end |
#to_json(*a) ⇒ Object
82 83 84 |
# File 'lib/chef-analytics/token.rb', line 82 def to_json(*a) Chef::JSONCompat.to_json(to_hash, *a) end |
#to_s ⇒ Object
97 98 99 |
# File 'lib/chef-analytics/token.rb', line 97 def to_s "token[#{@access_token}]" end |
#token_type(arg = nil) ⇒ Object
41 42 43 44 |
# File 'lib/chef-analytics/token.rb', line 41 def token_type(arg=nil) set_or_return(:token_type, arg, :kind_of => String) end |
#valid? ⇒ Boolean
110 111 112 |
# File 'lib/chef-analytics/token.rb', line 110 def valid? !expired? end |