renren2 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +81 -0
- data/Rakefile +26 -0
- data/lib/renren2.rb +20 -0
- data/lib/renren2/client.rb +220 -0
- data/lib/renren2/config.rb +28 -0
- data/lib/renren2/interface/admin.rb +32 -0
- data/lib/renren2/interface/base.rb +54 -0
- data/lib/renren2/interface/blog.rb +89 -0
- data/lib/renren2/interface/checkins.rb +27 -0
- data/lib/renren2/interface/feed.rb +47 -0
- data/lib/renren2/interface/friends.rb +100 -0
- data/lib/renren2/interface/invitations.rb +40 -0
- data/lib/renren2/interface/like.rb +54 -0
- data/lib/renren2/interface/notifications.rb +44 -0
- data/lib/renren2/interface/pages.rb +126 -0
- data/lib/renren2/interface/pay.rb +66 -0
- data/lib/renren2/interface/photos.rb +178 -0
- data/lib/renren2/interface/places.rb +40 -0
- data/lib/renren2/interface/requests.rb +25 -0
- data/lib/renren2/interface/share.rb +58 -0
- data/lib/renren2/interface/status.rb +108 -0
- data/lib/renren2/interface/users.rb +62 -0
- data/lib/renren2/strategy/auth_code.rb +28 -0
- data/lib/renren2/version.rb +3 -0
- data/lib/tasks/renren2_tasks.rake +4 -0
- metadata +104 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
= Renren2
|
2
|
+
|
3
|
+
A Ruby wrapper for Renren API(OAuth2).It is based on {OAuth2 gem}[https://github.com/intridea/oauth2], thanks for his hard-working.I have wrapped most of the APIs Renren defined.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem install renren2
|
8
|
+
|
9
|
+
== Usage Examples
|
10
|
+
|
11
|
+
Config your api_key, api_secret and redrect_uri somewhere like development.rb.
|
12
|
+
|
13
|
+
Renren2::Config.api_key = "1234567890"
|
14
|
+
Renren2::Config.api_secret = "somethinglooksstrageandhardtoremember"
|
15
|
+
Renren2::Config.redirect_uri = "http://www.example.com/callback"
|
16
|
+
|
17
|
+
Ok, now you are ready to enjoy it. Renren has provided several ways to get your access token, and you can easily get it using Renren2.
|
18
|
+
|
19
|
+
1.The Authorization Code strategy with response_type set to code
|
20
|
+
|
21
|
+
# get authorize_url
|
22
|
+
client = Renren2::Client.new
|
23
|
+
client.auth_code.authorize_url
|
24
|
+
# => "https://graph.renren.com/oauth/authorize?response_type=code&client_id=1234567890&redirect_uri=http%3A%2F%2Fwww.example.com%2fcallback"
|
25
|
+
|
26
|
+
# get token using authorization_code
|
27
|
+
# Renren2::Client.from_code is a shortcut for client.auth_code.get_token("authorization_code_value")
|
28
|
+
client = Renren2::Client.from_code("authorization_code_value")
|
29
|
+
|
30
|
+
2.The Authorization Code strategy with response_type set to token
|
31
|
+
|
32
|
+
# get authorize_url with response_type set to token
|
33
|
+
client = Renren::Client.new
|
34
|
+
client.auth_code.authorize_url(:response_type => "token")
|
35
|
+
# => "https://graph.renren.com/oauth/authorize?response_type=token&client_id=1234567890&redirect_uri=http%3A%2F%2Fwww.example.com%2fcallback"
|
36
|
+
|
37
|
+
# get token from callback hash like this /callback#access_token=6874dd3766b35536abcb76a9e3a57867&expires_in=2594445&scope=photo_upload+send_invitation
|
38
|
+
# note that the access_token is different with renren defined, just for demonstration.
|
39
|
+
client = Renren2::Client.from_hash(:access_token => "6874dd3766b35536abcb76a9e3a57867", :expires_in => 86400)
|
40
|
+
|
41
|
+
3.The Resource Owner Password Credentials strategy
|
42
|
+
|
43
|
+
# get token with user's password
|
44
|
+
client = Renren::Client.new
|
45
|
+
client.password.get_token('username', 'password')
|
46
|
+
|
47
|
+
4.Refresh your token
|
48
|
+
|
49
|
+
Note that you could refresh your token only when you can get the refresh_token.
|
50
|
+
|
51
|
+
|
52
|
+
client.refresh!
|
53
|
+
|
54
|
+
You can check if you are authorized by
|
55
|
+
|
56
|
+
client.is_authorized?
|
57
|
+
# => true
|
58
|
+
|
59
|
+
If you are authorized, then you can do whatever you want now.
|
60
|
+
|
61
|
+
response = client.users.get_logged_in_user
|
62
|
+
# => #<OAuth2::Response:: ...>
|
63
|
+
|
64
|
+
response.parsed
|
65
|
+
# => {"uid"=>1234567890}
|
66
|
+
|
67
|
+
|
68
|
+
== API
|
69
|
+
|
70
|
+
You can find them in /lib/renren2/interface/.Note that all methods follow the patten of
|
71
|
+
|
72
|
+
{resource}.{the_rest_path_joined_by_underline}(required_params, opts={})
|
73
|
+
|
74
|
+
So {friends.getSameFriends}[http://wiki.dev.renren.com/wiki/Friends.getSameFriends] will turn to
|
75
|
+
|
76
|
+
friends.get_same_friends(uid1, uid2, opts={})
|
77
|
+
|
78
|
+
|
79
|
+
== Copyright
|
80
|
+
|
81
|
+
Copyright (c) 2011 Acenqiu. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'Renren2'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
Bundler::GemHelper.install_tasks
|
26
|
+
|
data/lib/renren2.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'renren2/client'
|
2
|
+
require 'renren2/config'
|
3
|
+
require 'renren2/strategy/auth_code.rb'
|
4
|
+
require 'renren2/interface/base'
|
5
|
+
require 'renren2/interface/admin'
|
6
|
+
require 'renren2/interface/blog'
|
7
|
+
require 'renren2/interface/checkins'
|
8
|
+
require 'renren2/interface/feed'
|
9
|
+
require 'renren2/interface/friends'
|
10
|
+
require 'renren2/interface/invitations'
|
11
|
+
require 'renren2/interface/like'
|
12
|
+
require 'renren2/interface/notifications'
|
13
|
+
require 'renren2/interface/pages'
|
14
|
+
require 'renren2/interface/pay'
|
15
|
+
require 'renren2/interface/photos'
|
16
|
+
require 'renren2/interface/places'
|
17
|
+
require 'renren2/interface/requests'
|
18
|
+
require 'renren2/interface/share'
|
19
|
+
require 'renren2/interface/status'
|
20
|
+
require 'renren2/interface/users'
|
@@ -0,0 +1,220 @@
|
|
1
|
+
require 'oauth2'
|
2
|
+
|
3
|
+
module Renren2
|
4
|
+
|
5
|
+
# The Renren2::Client class
|
6
|
+
class Client < OAuth2::Client
|
7
|
+
|
8
|
+
attr_reader :redirect_uri
|
9
|
+
attr_accessor :token
|
10
|
+
|
11
|
+
# Initializes a new Client from a authentication_code
|
12
|
+
#
|
13
|
+
# @param [String] code The Authorization Code value
|
14
|
+
# @param [Hash] opts the options to create the client with
|
15
|
+
# @option opts [Hash] :connection_opts ({}) Hash of connection options to pass to initialize Faraday with
|
16
|
+
# @option opts [FixNum] :max_redirects (5) maximum number of redirects to follow
|
17
|
+
# @yield [builder] The Faraday connection builder
|
18
|
+
def self.from_code(code, opts={}, &block)
|
19
|
+
client = self.new(opts, &block)
|
20
|
+
client.auth_code.get_token(code)
|
21
|
+
|
22
|
+
client
|
23
|
+
end
|
24
|
+
|
25
|
+
# Initializes a new Client from a hash
|
26
|
+
#
|
27
|
+
# @param [Hash] a Hash contains access_token and expires
|
28
|
+
# @param [Hash] opts the options to create the client with
|
29
|
+
# @option opts [Hash] :connection_opts ({}) Hash of connection options to pass to initialize Faraday with
|
30
|
+
# @option opts [FixNum] :max_redirects (5) maximum number of redirects to follow
|
31
|
+
# @yield [builder] The Faraday connection builder
|
32
|
+
def self.from_hash(hash, opts={}, &block)
|
33
|
+
client = self.new(opts, &block)
|
34
|
+
client.get_token_from_hash(hash)
|
35
|
+
|
36
|
+
client
|
37
|
+
end
|
38
|
+
|
39
|
+
# Initializes a new Client
|
40
|
+
#
|
41
|
+
# @param [Hash] opts the options to create the client with
|
42
|
+
# @option opts [Hash] :connection_opts ({}) Hash of connection options to pass to initialize Faraday with
|
43
|
+
# @option opts [FixNum] :max_redirects (5) maximum number of redirects to follow
|
44
|
+
# @yield [builder] The Faraday connection builder
|
45
|
+
def initialize(opts={}, &block)
|
46
|
+
id = Renren2::Config.api_key
|
47
|
+
secret = Renren2::Config.api_secret
|
48
|
+
@redirect_uri = Renren2::Config.redirect_uri
|
49
|
+
|
50
|
+
options = {:site => "https://graph.renren.com",
|
51
|
+
:authorize_url => "/oauth/authorize",
|
52
|
+
:token_url => "/oauth/token",
|
53
|
+
:raise_errors => false,
|
54
|
+
:ssl => {:verify => false}}.merge(opts)
|
55
|
+
|
56
|
+
super(id, secret, options, &block)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Whether or not the client is authorized
|
60
|
+
#
|
61
|
+
# @return [Boolean]
|
62
|
+
def is_authorized?
|
63
|
+
!!token && !token.expired?
|
64
|
+
end
|
65
|
+
|
66
|
+
# Initializes an AccessToken by making a request to the token endpoint
|
67
|
+
#
|
68
|
+
# @param [Hash] params a Hash of params for the token endpoint
|
69
|
+
# @param [Hash] access token options, to pass to the AccessToken object
|
70
|
+
# @return [AccessToken] the initalized AccessToken
|
71
|
+
def get_token(params, access_token_opts={})
|
72
|
+
params = params.merge(:parse => :json)
|
73
|
+
access_token_opts = access_token_opts.merge({:header_format => "OAuth2 %s", :param_name => "oauth_token"})
|
74
|
+
|
75
|
+
@token = super(params, access_token_opts)
|
76
|
+
end
|
77
|
+
|
78
|
+
# Initializes an AccessToken from a hash
|
79
|
+
#
|
80
|
+
# @param [Hash] hash a Hash contains access_token and expires
|
81
|
+
# @return [AccessToken] the initalized AccessToken
|
82
|
+
def get_token_from_hash(hash)
|
83
|
+
access_token = hash.delete('access_token') || hash.delete(:access_token) || hash.delete('oauth_token') || hash.delete(:oauth_token)
|
84
|
+
opts = {:expires_at => hash.delete("expires") || hash.delete(:expires),
|
85
|
+
:header_format => "OAuth2 %s",
|
86
|
+
:param_name => "oauth_token"}.merge(hash)
|
87
|
+
|
88
|
+
@token = OAuth2::AccessToken.new(self, access_token, opts)
|
89
|
+
end
|
90
|
+
|
91
|
+
# Refreshes the current Access Token
|
92
|
+
#
|
93
|
+
# @return [AccessToken] a new AccessToken
|
94
|
+
# @note options should be carried over to the new AccessToken
|
95
|
+
def refresh!(params={})
|
96
|
+
@token = token.refresh!(params)
|
97
|
+
end
|
98
|
+
|
99
|
+
#
|
100
|
+
# Some info
|
101
|
+
#
|
102
|
+
|
103
|
+
def user
|
104
|
+
token.params["user"] if token
|
105
|
+
end
|
106
|
+
|
107
|
+
def user_id
|
108
|
+
user["id"] if user
|
109
|
+
end
|
110
|
+
|
111
|
+
def scope
|
112
|
+
token.params["scope"] if token
|
113
|
+
end
|
114
|
+
|
115
|
+
def include_scope?(scope_name)
|
116
|
+
return true
|
117
|
+
if token && token.params["scope"]
|
118
|
+
token.params["scope"].split.include? scope_name
|
119
|
+
else
|
120
|
+
false
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def hash_for_save
|
125
|
+
if token
|
126
|
+
token.params.merge({"expires_in" => token.expires_in,
|
127
|
+
"expires_at" => token.expires_at,
|
128
|
+
"refresh_token" => token.refresh_token,
|
129
|
+
"access_token" => token.token})
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
#
|
134
|
+
# Strategies
|
135
|
+
#
|
136
|
+
|
137
|
+
# The Authorization Code strategy
|
138
|
+
#
|
139
|
+
# @see http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.1
|
140
|
+
def auth_code
|
141
|
+
@auth_code ||= Renren2::Strategy::AuthCode.new(self)
|
142
|
+
end
|
143
|
+
|
144
|
+
# The Resource Owner Password Credentials strategy
|
145
|
+
#
|
146
|
+
# @see http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.3
|
147
|
+
def password
|
148
|
+
super
|
149
|
+
end
|
150
|
+
|
151
|
+
#
|
152
|
+
# APIs
|
153
|
+
#
|
154
|
+
|
155
|
+
def admin
|
156
|
+
@admin ||= Renren2::Interface::Admin.new(self)
|
157
|
+
end
|
158
|
+
|
159
|
+
def blog
|
160
|
+
@blog ||= Renren2::Interface::Blog.new(self)
|
161
|
+
end
|
162
|
+
|
163
|
+
def checkins
|
164
|
+
@checkins ||= Renren2::Interface::Checkins.new(self)
|
165
|
+
end
|
166
|
+
|
167
|
+
def feed
|
168
|
+
@feed ||= Renren2::Interface::Feed.new(self)
|
169
|
+
end
|
170
|
+
|
171
|
+
def friends
|
172
|
+
@friends ||= Renren2::Interface::Friends.new(self)
|
173
|
+
end
|
174
|
+
|
175
|
+
def invitations
|
176
|
+
@invitations ||= Renren2::Interface::Invitations.new(self)
|
177
|
+
end
|
178
|
+
|
179
|
+
def like
|
180
|
+
@like ||= Renren2::Interface::Like.new(self)
|
181
|
+
end
|
182
|
+
|
183
|
+
def notifications
|
184
|
+
@notifications ||= Renren2::Interface::Notifications.new(self)
|
185
|
+
end
|
186
|
+
|
187
|
+
def pages
|
188
|
+
@pages ||= Renren2::Interface::Pages.new(self)
|
189
|
+
end
|
190
|
+
|
191
|
+
def pay
|
192
|
+
@pay ||= Renren2::Interface::Pay.new(self)
|
193
|
+
end
|
194
|
+
|
195
|
+
def photos
|
196
|
+
@photos ||= Renren2::Interface::Photos.new(self)
|
197
|
+
end
|
198
|
+
|
199
|
+
def places
|
200
|
+
@places ||= Renren2::Interface::Places.new(self)
|
201
|
+
end
|
202
|
+
|
203
|
+
def requests
|
204
|
+
@requests ||= Renren2::Interface::Requests.new(self)
|
205
|
+
end
|
206
|
+
|
207
|
+
def share
|
208
|
+
@share ||= Renren2::Interface::Share.new(self)
|
209
|
+
end
|
210
|
+
|
211
|
+
def status
|
212
|
+
@status ||= Renren2::Interface::Status.new(self)
|
213
|
+
end
|
214
|
+
|
215
|
+
|
216
|
+
def users
|
217
|
+
@users ||= Renren2::Interface::Users.new(self)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Renren2
|
2
|
+
module Config
|
3
|
+
|
4
|
+
def self.api_key=(val)
|
5
|
+
@@api_key = val
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.api_key
|
9
|
+
@@api_key
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.api_secret=(val)
|
13
|
+
@@api_secret = val
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.api_secret
|
17
|
+
@@api_secret
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.redirect_uri=(val)
|
21
|
+
@@redirect_uri = val
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.redirect_uri
|
25
|
+
@@redirect_uri
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# test passed: 2011-11-06 14:50:22
|
3
|
+
module Renren2
|
4
|
+
module Interface
|
5
|
+
|
6
|
+
# Admin Interface
|
7
|
+
#
|
8
|
+
# @see http://wiki.dev.renren.com/wiki/API
|
9
|
+
class Admin < Base
|
10
|
+
|
11
|
+
# 得到一个应用当天可以发送的通知和邀请的配额
|
12
|
+
#
|
13
|
+
# @see http://wiki.dev.renren.com/wiki/Admin.getAllocation
|
14
|
+
def get_allocation
|
15
|
+
post 'admin.getAllocation'
|
16
|
+
end
|
17
|
+
|
18
|
+
# 对给定的文本进行过滤,返回过滤后的文本,如果不合法,则给出错误信息
|
19
|
+
#
|
20
|
+
# @param [String] text 需检查的文本内容
|
21
|
+
# @param [Hash] opts
|
22
|
+
# @option opts [int] :type 过滤的文本类型,1代表纯文本,2代表包含脚本或标签的文本。缺省值为1
|
23
|
+
#
|
24
|
+
# @see http://wiki.dev.renren.com/wiki/Admin.textFilter
|
25
|
+
def text_filter(text, opts={})
|
26
|
+
post 'admin.textFilter', :body => {:text => text}.merge(opts)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Renren2
|
2
|
+
module Interface
|
3
|
+
|
4
|
+
# The Base class of API
|
5
|
+
class Base
|
6
|
+
def initialize(client)
|
7
|
+
@client = client
|
8
|
+
end
|
9
|
+
|
10
|
+
def check_scope(scope_name)
|
11
|
+
unless @client.include_scope? scope_name
|
12
|
+
raise "You haven't got the scope " + scope_name
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def request(verb, path, opts={}, &block)
|
17
|
+
unless @client.is_authorized?
|
18
|
+
raise "I can't find a valid access_token. Forgot to get it or expired?"
|
19
|
+
end
|
20
|
+
|
21
|
+
# Note that it is a hack here. Renren hasn't started using http://graph.renren.com as its api server, so
|
22
|
+
# we still use http://api.renren.com/restserver.do as a temporary solution. And the :path param will act
|
23
|
+
# as the :method params in querystring.
|
24
|
+
params = {:access_token => @client.token.token,
|
25
|
+
:method => path,
|
26
|
+
:v => "1.0",
|
27
|
+
:format => "JSON"}.merge(opts.delete(:body) || {})
|
28
|
+
.merge(opts.delete(:params) || {})
|
29
|
+
opts.merge!(:params => params.merge(:sig => compute_sig(params)), :parse => :json)
|
30
|
+
|
31
|
+
# use the old server
|
32
|
+
path = "http://api.renren.com/restserver.do"
|
33
|
+
|
34
|
+
@client.token.request(verb, path, opts, &block)
|
35
|
+
end
|
36
|
+
|
37
|
+
def get(path, opts={}, &block)
|
38
|
+
request(:get, path, opts, &block)
|
39
|
+
end
|
40
|
+
|
41
|
+
def post(path, opts={}, &block)
|
42
|
+
request(:post, path, opts, &block)
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def compute_sig(params={})
|
48
|
+
str = params.collect {|k,v| "#{k}=#{v}"}.sort.join("") + @client.secret
|
49
|
+
Digest::MD5.hexdigest(str)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|