Class: EasyWeibo::Client
- Inherits:
-
Object
- Object
- EasyWeibo::Client
- Defined in:
- lib/easy_weibo/client.rb
Constant Summary collapse
- OAUTH2_AUTHORIZE_URL =
"https://api.weibo.com/oauth2/authorize"
- OAUTH2_ACCESS_TOKEN_URL =
"https://api.weibo.com/oauth2/access_token"
- STATUSES_SHARE_URL =
"https://api.weibo.com/2/statuses/share.json"
Instance Attribute Summary collapse
-
#code ⇒ Object
writeonly
Sets the attribute code.
-
#token ⇒ Object
获取access_token.
Instance Method Summary collapse
- #access_token {|r| ... } ⇒ Object
-
#authorize_url ⇒ Object
构造授权地址,获取code open.weibo.com/wiki/Oauth2/authorize.
-
#initialize ⇒ Client
constructor
A new instance of Client.
-
#statuses_share(text, url, pic = nil) {|r| ... } ⇒ Object
发布微博.
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
14 15 16 17 |
# File 'lib/easy_weibo/client.rb', line 14 def initialize @code = nil @token = nil end |
Instance Attribute Details
#code=(value) ⇒ Object (writeonly)
Sets the attribute code
12 13 14 |
# File 'lib/easy_weibo/client.rb', line 12 def code=(value) @code = value end |
#token ⇒ Object
获取access_token
46 47 48 |
# File 'lib/easy_weibo/client.rb', line 46 def token @token ||= access_token["access_token"] end |
Instance Method Details
#access_token {|r| ... } ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/easy_weibo/client.rb', line 27 def access_token raise "code is nil" if @code.blank? payload = { client_id: EasyWeibo.app_key, client_secret: EasyWeibo.app_secret, grant_type: "authorization_code", code: @code, redirect_uri: EasyWeibo.redirect_uri, } resp = HTTPX.post(OAUTH2_ACCESS_TOKEN_URL, params: payload) r = ::JSON.parse(resp.body, quirks_mode: true) yield r if block_given? r end |
#authorize_url ⇒ Object
构造授权地址,获取code open.weibo.com/wiki/Oauth2/authorize
21 22 23 |
# File 'lib/easy_weibo/client.rb', line 21 def "#{OAUTH2_AUTHORIZE_URL}?redirect_uri=#{EasyWeibo.redirect_uri}&client_id=#{EasyWeibo.app_key}&display=wap" end |
#statuses_share(text, url, pic = nil) {|r| ... } ⇒ Object
发布微博
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/easy_weibo/client.rb', line 51 def statuses_share(text, url, pic = nil) # TODO: 抛出异常 必须做URLencode,内容不超过140个汉字 status = "#{text} #{url}" payload = { status: status } payload[:pic] = pic.is_a?(String) ? HTTP::FormData::File.new(pic) : pic unless pic.blank? resp = HTTPX.plugin(:multipart).post(STATUSES_SHARE_URL, params: { access_token: token }, form: payload) r = ::JSON.parse(resp.body, quirks_mode: true) yield r if block_given? r end |