lib/easy_weibo/client.rb in easy_weibo-0.1.0 vs lib/easy_weibo/client.rb in easy_weibo-0.1.1
- old
+ new
@@ -6,10 +6,12 @@
module EasyWeibo
class Client
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"
attr_writer :token, :code
def initialize
@code = nil
@@ -54,9 +56,40 @@
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
+
+ # https://open.weibo.com/wiki/2/statuses/user_timeline
+ 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
+
+ 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