Class: WechatClient::OAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/wechat_client/o_auth.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ OAuth

Returns a new instance of OAuth.



5
6
7
# File 'lib/wechat_client/o_auth.rb', line 5

def initialize options
  set_valuables options[:code]
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



3
4
5
# File 'lib/wechat_client/o_auth.rb', line 3

def access_token
  @access_token
end

#codeObject (readonly)

Returns the value of attribute code.



3
4
5
# File 'lib/wechat_client/o_auth.rb', line 3

def code
  @code
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



3
4
5
# File 'lib/wechat_client/o_auth.rb', line 3

def expires_in
  @expires_in
end

#openidObject (readonly)

Returns the value of attribute openid.



3
4
5
# File 'lib/wechat_client/o_auth.rb', line 3

def openid
  @openid
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



3
4
5
# File 'lib/wechat_client/o_auth.rb', line 3

def refresh_token
  @refresh_token
end

#scopeObject (readonly)

Returns the value of attribute scope.



3
4
5
# File 'lib/wechat_client/o_auth.rb', line 3

def scope
  @scope
end

#unionidObject (readonly)

Returns the value of attribute unionid.



3
4
5
# File 'lib/wechat_client/o_auth.rb', line 3

def unionid
  @unionid
end

Class Method Details



28
29
30
31
32
33
34
35
36
37
# File 'lib/wechat_client/o_auth.rb', line 28

def self.generate_auth_link url, state = "", scope = "snsapi_userinfo"
  params = {
    appid: WechatClient.client.options.app_id,
    redirect_uri: url,
    response_type: "code",
    scope: scope,
    state: state
  }
  "https://open.weixin.qq.com/connect/oauth2/authorize?#{params.to_query}#wechat_redirect"
end

Instance Method Details

#authObject



9
10
11
12
13
14
15
# File 'lib/wechat_client/o_auth.rb', line 9

def auth
  if @scope == "snsapi_base"
    WechatClient.client.base_info openid: @openid
  else
    WechatClient.client.auth_info access_token: @access_token, openid: @openid
  end
end

#refresh_authObject



17
18
19
# File 'lib/wechat_client/o_auth.rb', line 17

def refresh_auth
  WechatClient.client.refresh_auth_access_token refresh_token: @refresh_token
end

#valid?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'lib/wechat_client/o_auth.rb', line 21

def valid?
  WechatClient.client.auth_access_token_validation access_token: @access_token,\
   openid: @openid do |json|
    json["errmsg"].downcase == "ok"
  end
end