Class: EasyWeibo::Client

Inherits:
Object
  • Object
show all
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"
USER_TIMELINE_URL =
"https://api.weibo.com/2/statuses/user_timeline.json"
USERS_SHOW_URL =
"https://api.weibo.com/2/users/show.json"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



16
17
18
19
# File 'lib/easy_weibo/client.rb', line 16

def initialize
  @code = nil
  @token = nil
end

Instance Attribute Details

#code=(value) ⇒ Object (writeonly)

Sets the attribute code

Parameters:

  • value

    the value to set the attribute code to.



14
15
16
# File 'lib/easy_weibo/client.rb', line 14

def code=(value)
  @code = value
end

#tokenObject

获取access_token



48
49
50
# File 'lib/easy_weibo/client.rb', line 48

def token
  @token ||= access_token["access_token"]
end

Instance Method Details

#access_token {|r| ... } ⇒ Object

Yields:

  • (r)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/easy_weibo/client.rb', line 29

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_urlObject

构造授权地址,获取code open.weibo.com/wiki/Oauth2/authorize



23
24
25
# File 'lib/easy_weibo/client.rb', line 23

def authorize_url
  "#{OAUTH2_AUTHORIZE_URL}?redirect_uri=#{EasyWeibo.redirect_uri}&client_id=#{EasyWeibo.app_key}&display=wap"
end

#statuses_share(text, url, pic = nil) {|r| ... } ⇒ Object

发布微博

Yields:

  • (r)


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/easy_weibo/client.rb', line 53

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

#user_timeline(uid, options = {}) {|r| ... } ⇒ Object

Yields:

  • (r)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/easy_weibo/client.rb', line 68

def user_timeline(uid, options = {})
  params = {
    access_token: token,
    uid: uid,
    since_id: options.delete(:since_id) || 0, # 若指定此参数,则返回ID比since_id大的微博(即比时间晚的微博),默认为0。
    max_id: options.delete(:max_id) || 0, # 若指定此参数,则返回ID小于或等于max_id的微博,默认为0。
    count: options.delete(:count) || 20, # 单页返回的记录条数,最大不超过100,超过100以100处理,默认为20。
    page: options.delete(:page) || 1, # 返回结果的页码,默认为1。
    base_app: options.delete(:base_app) || 0, # 是否只获取当前应用的数据。0为否(所有数据),1为是(仅当前应用),默认为0。
    feature: options.delete(:feature) || 0, # 过滤类型ID,0:全部、1:原创、2:图片、3:视频、4:音乐,默认为0。
    trim_user: options.delete(:trim_user) || 0, # 返回值中user字段开关,0:返回完整user字段、1:user字段仅返回user_id,默认为0。
  }

  resp = HTTPX.get(USER_TIMELINE_URL, params: params)

  r = ::JSON.parse(resp.body, quirks_mode: true)
  yield r if block_given?
  r
end

#users_show(uid) {|r| ... } ⇒ Object

Yields:

  • (r)


88
89
90
91
92
93
94
95
96
# File 'lib/easy_weibo/client.rb', line 88

def users_show(uid)
  params = { uid: uid, access_token: token }

  resp = HTTPX.get(USERS_SHOW_URL, params: params)

  r = ::JSON.parse(resp.body, quirks_mode: true)
  yield r if block_given?
  r
end