Class: WeChat::AccessToken
- Inherits:
-
Object
- Object
- WeChat::AccessToken
- Includes:
- REST::Utils, Symbolizable
- Defined in:
- lib/we_chat/access_token.rb
Instance Attribute Summary collapse
-
#access_token_data ⇒ Object
readonly
Returns the value of attribute access_token_data.
-
#access_token_file ⇒ Object
readonly
Returns the value of attribute access_token_file.
-
#appid ⇒ Object
readonly
Returns the value of attribute appid.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(client, appid, secret, access_token_file) ⇒ AccessToken
constructor
A new instance of AccessToken.
- #retrieve ⇒ Object
- #valid?(data) ⇒ Boolean
- #value ⇒ Object
Constructor Details
#initialize(client, appid, secret, access_token_file) ⇒ AccessToken
Returns a new instance of AccessToken.
12 13 14 15 16 17 |
# File 'lib/we_chat/access_token.rb', line 12 def initialize(client, appid, secret, access_token_file) @client = client @appid = appid @secret = secret @access_token_file = access_token_file end |
Instance Attribute Details
#access_token_data ⇒ Object (readonly)
Returns the value of attribute access_token_data.
10 11 12 |
# File 'lib/we_chat/access_token.rb', line 10 def access_token_data @access_token_data end |
#access_token_file ⇒ Object (readonly)
Returns the value of attribute access_token_file.
10 11 12 |
# File 'lib/we_chat/access_token.rb', line 10 def access_token_file @access_token_file end |
#appid ⇒ Object (readonly)
Returns the value of attribute appid.
10 11 12 |
# File 'lib/we_chat/access_token.rb', line 10 def appid @appid end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
10 11 12 |
# File 'lib/we_chat/access_token.rb', line 10 def client @client end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
10 11 12 |
# File 'lib/we_chat/access_token.rb', line 10 def secret @secret end |
Class Method Details
.get_access_token(file_path = nil) ⇒ Object
48 49 50 51 52 |
# File 'lib/we_chat/access_token.rb', line 48 def get_access_token(file_path = nil) file_path ||= WeChat.wechat_access_token_file_path data = JSON.parse(File.read(file_path)) data["access_token"] end |
Instance Method Details
#retrieve ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/we_chat/access_token.rb', line 19 def retrieve response = WeChat::REST::Request.new(client, :get, '/token', params: { grant_type: 'client_credential', appid: appid, secret: secret }).perform File.open(access_token_file, 'w') { |file| file.write(response.to_json) } if valid?(response) return @access_token_data = response end |
#valid?(data) ⇒ Boolean
41 42 43 44 |
# File 'lib/we_chat/access_token.rb', line 41 def valid?(data) access_token = data.nil? ? '' : data[:access_token] !access_token.blank? end |
#value ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/we_chat/access_token.rb', line 31 def value begin @access_token_data ||= symbolize_keys!(JSON.parse(File.read(access_token_file))) rescue => e Rails.logger.debug e retrieve end @access_token_data[:access_token] end |