diandian-oauth 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +21 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +33 -0
- data/README.md +4 -0
- data/diandian-oauth.gemspec +19 -0
- data/lib/diandian/model/base.rb +28 -0
- data/lib/diandian/model/blog.rb +11 -0
- data/lib/diandian/model/post.rb +20 -0
- data/lib/diandian/model/user.rb +7 -0
- data/lib/diandian/model.rb +0 -0
- data/lib/diandian.rb +9 -0
- data/lib/diandian_oauth/api/interface.rb +251 -0
- data/lib/diandian_oauth/api.rb +32 -0
- data/lib/diandian_oauth/client.rb +73 -0
- data/lib/diandian_oauth/exceptions.rb +19 -0
- data/lib/diandian_oauth.rb +1 -1
- data/test/diandian_oauth/api_test.rb +21 -0
- data/test/diandian_oauth/authorize_test.rb +24 -0
- data/test/diandian_oauth/interface_test.rb +40 -0
- data/test/diandian_oauth/logger_test.rb +16 -0
- data/test/diandian_oauth/test_helper.rb +4 -0
- metadata +61 -5
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.2.7)
|
5
|
+
activesupport (= 3.2.7)
|
6
|
+
builder (~> 3.0.0)
|
7
|
+
activesupport (3.2.7)
|
8
|
+
i18n (~> 0.6)
|
9
|
+
multi_json (~> 1.0)
|
10
|
+
builder (3.0.0)
|
11
|
+
faraday (0.8.1)
|
12
|
+
multipart-post (~> 1.1)
|
13
|
+
httpauth (0.1)
|
14
|
+
i18n (0.6.0)
|
15
|
+
jwt (0.1.5)
|
16
|
+
multi_json (>= 1.0)
|
17
|
+
multi_json (1.3.6)
|
18
|
+
multipart-post (1.1.5)
|
19
|
+
oauth2 (0.8.0)
|
20
|
+
faraday (~> 0.8)
|
21
|
+
httpauth (~> 0.1)
|
22
|
+
jwt (~> 0.1.4)
|
23
|
+
multi_json (~> 1.0)
|
24
|
+
rack (~> 1.2)
|
25
|
+
rack (1.4.1)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
activemodel (~> 3.2.6)
|
32
|
+
activesupport (~> 3.2.1)
|
33
|
+
oauth2 (~> 0.8.0)
|
data/README.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'diandian-oauth'
|
3
|
+
s.version = '0.0.3'
|
4
|
+
s.date = '2012-07-24'
|
5
|
+
s.summary = "Client for diandian API"
|
6
|
+
s.description = "A Ruby Client for diandian API"
|
7
|
+
s.authors = ["Rainer Du"]
|
8
|
+
s.email = 'dusiyu@diandian.com'
|
9
|
+
s.files = ["lib/diandian_oauth.rb"]
|
10
|
+
s.homepage = 'https://github.com/secretworry/diandian-oauth'
|
11
|
+
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
s.test_files = `git ls-files -- test/*`.split("\n")
|
14
|
+
s.require_paths = ["lib"]
|
15
|
+
|
16
|
+
s.add_dependency("oauth2", '~> 0.8.0')
|
17
|
+
s.add_dependency("activemodel", '~> 3.2.6')
|
18
|
+
s.add_dependency("activesupport", '~> 3.2.1')
|
19
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Diandian
|
2
|
+
module Model
|
3
|
+
class Base
|
4
|
+
include ActiveModel::AttributeMethods
|
5
|
+
include ActiveModel::Serializers::JSON
|
6
|
+
include ActiveModel::Validations
|
7
|
+
|
8
|
+
def initialize( attributes = {})
|
9
|
+
@attributes = attributes
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def model_name model_name = nil
|
14
|
+
@model_name = model_name if model_name
|
15
|
+
@model_name ||= self.name.downcase
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def model_name
|
20
|
+
self.class.model_name
|
21
|
+
end
|
22
|
+
|
23
|
+
def id
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Diandain
|
2
|
+
module Model
|
3
|
+
class Post < Diandian::Model::Base
|
4
|
+
# define common attributes
|
5
|
+
define_attribute_methods [:format, :blogName, :blogCName, :postId, :postUrl, :type, :createTime, :tag, :commentCount, :reblogCount, :likeCount]
|
6
|
+
# define reblogInfo related attribute
|
7
|
+
define_attribute_methods [:reblogedFromUrl, :reblogedFromName, :reblogedFromTitle, :reblogedRootUrl, :reblogedRootName, :reblogedRootTitle]
|
8
|
+
# define text specified attributes
|
9
|
+
define_attribute_methods [:title, :body]
|
10
|
+
# define photo specified attributes
|
11
|
+
define_attribute_methods [:caption, :photos]
|
12
|
+
# define audio specified attributes
|
13
|
+
define_attribute_methods [:musicId, :audioType, :cover, :caption, :musicName, :playerUrl, :musicSinger, :coverNormal, :coverLarge, :coverSmall, :albumName, :musicName, :albumId, :artistId]
|
14
|
+
# define video specified attributes
|
15
|
+
define_attribute_methods [:caption, :sourceUrl, :sourceTitle, :videoType, :videoId, :playerUrl, :videoImageUrl]
|
16
|
+
# define link specified attributes
|
17
|
+
define_attribute_methods [:title, :url, :description]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
File without changes
|
data/lib/diandian.rb
ADDED
@@ -0,0 +1,251 @@
|
|
1
|
+
|
2
|
+
module DiandianOAuth
|
3
|
+
class API
|
4
|
+
module Interface
|
5
|
+
def self.included base
|
6
|
+
base.send :include, InstanceMethods
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
module ClassMethods
|
10
|
+
def url_for path, options = {}
|
11
|
+
protocol = options[:protocol] || "https"
|
12
|
+
version = options[:version] || API::VERSION
|
13
|
+
host = options[:host] || API::HOST
|
14
|
+
path = if options[:no_version] then path else "/#{version}#{path}" end
|
15
|
+
"#{protocol}://#{host}#{path}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def interface name, interface_class
|
19
|
+
self.interfaces[name.to_sym] = interface_class.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def interfaces
|
23
|
+
@interfaces ||= {}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module InstanceMethods
|
28
|
+
def interface interface_name
|
29
|
+
self.class.interfaces[interface_name]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
class Param
|
33
|
+
attr_accessor :name, :required
|
34
|
+
|
35
|
+
DEFAULT_OPTIONS = { required: true }
|
36
|
+
def initialize name, options = {}
|
37
|
+
options = DEFAULT_OPTIONS.merge( options)
|
38
|
+
@name = name
|
39
|
+
@required = options[:required]
|
40
|
+
end
|
41
|
+
end #Param
|
42
|
+
|
43
|
+
class Base
|
44
|
+
def request_url params = {}
|
45
|
+
raise 'subclasses must implement this method'
|
46
|
+
end
|
47
|
+
|
48
|
+
# when the access_token is out-of-date, refresh it automatically
|
49
|
+
def apply! access_token, params, &block
|
50
|
+
access_token.refresh! if access_token.expired?
|
51
|
+
self.apply access_token, params, &block
|
52
|
+
end
|
53
|
+
|
54
|
+
def apply access_token, params, &block
|
55
|
+
raise TokenExpiredException if access_token.expired?
|
56
|
+
params ||= {}
|
57
|
+
action = case request_verb
|
58
|
+
when /get/ then :get
|
59
|
+
when /post/ then :post
|
60
|
+
when /put/ then :put
|
61
|
+
when /delete/ then :delete
|
62
|
+
else raise "unrecognized verb '#{request_verb}'"
|
63
|
+
end # case
|
64
|
+
options = {
|
65
|
+
:params => extract_params(params),
|
66
|
+
:raise_errors => false,
|
67
|
+
:parse => :json
|
68
|
+
}
|
69
|
+
request_url = self.request_url(params)
|
70
|
+
if DiandianOAuth.logger.debug?
|
71
|
+
DiandianOAuth.logger.debug("request with action:'#{action}', request_url:'#{request_url}'")
|
72
|
+
end
|
73
|
+
access_token.request( action, request_url, options, &block)
|
74
|
+
end
|
75
|
+
protected
|
76
|
+
def request_verb
|
77
|
+
self.class.verb
|
78
|
+
end
|
79
|
+
def extract_params params
|
80
|
+
params.symbolize_keys!
|
81
|
+
self.class.params.each_pair.reduce({}) do |result, ele|
|
82
|
+
key = ele[0]
|
83
|
+
param = ele[1]
|
84
|
+
param_value = params[key]
|
85
|
+
if param_value && param.required
|
86
|
+
result[key] = param_value
|
87
|
+
elsif param.required
|
88
|
+
raise ParamIsRequiredException.new( "'#{key}' is required")
|
89
|
+
end
|
90
|
+
result
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
class << self
|
95
|
+
def param name, options = {}
|
96
|
+
params[name.to_sym] = Param.new( name.to_s, options)
|
97
|
+
end
|
98
|
+
def verb verb=nil
|
99
|
+
@verb = verb if verb
|
100
|
+
@verb ||= 'get'
|
101
|
+
end
|
102
|
+
def params
|
103
|
+
@params ||= {}
|
104
|
+
end
|
105
|
+
end # class
|
106
|
+
end # Base
|
107
|
+
|
108
|
+
class UserInfo < Base
|
109
|
+
def request_url params={}
|
110
|
+
API.url_for '/user/info'
|
111
|
+
end
|
112
|
+
end #UserInfo
|
113
|
+
|
114
|
+
class UserLikes < Base
|
115
|
+
param :limit, :required => false
|
116
|
+
param :offset, :required => false
|
117
|
+
def request_url params={}
|
118
|
+
API.url_for '/user/likes'
|
119
|
+
end
|
120
|
+
end #UserLikes
|
121
|
+
|
122
|
+
class UserFollowings < Base
|
123
|
+
param :limit, :required => false
|
124
|
+
param :offset, :required => false
|
125
|
+
def request_url params={}
|
126
|
+
API.url_for '/user/following'
|
127
|
+
end
|
128
|
+
end #UserFollowing
|
129
|
+
|
130
|
+
class MyTags < Base
|
131
|
+
def request_url params={}
|
132
|
+
API.url_for '/tag/mytags'
|
133
|
+
end
|
134
|
+
end #TagMyTags
|
135
|
+
|
136
|
+
class Blog < Base
|
137
|
+
protected
|
138
|
+
def blog_request_url params
|
139
|
+
blog_cname = params[:blogCName]
|
140
|
+
raise ParamIsRequiredException.new("blogCName is required for interface #{self.class.name}") unless blog_cname
|
141
|
+
API.url_for "/blog/#{blog_cname}"
|
142
|
+
end
|
143
|
+
end #BlogInterface
|
144
|
+
|
145
|
+
class BlogInfo < Base
|
146
|
+
def request_url params={}
|
147
|
+
"#{blog_request_url}/info"
|
148
|
+
end
|
149
|
+
end #BlogInfo
|
150
|
+
|
151
|
+
class BlogAvatar < Base
|
152
|
+
VALID_SIZES = %w{57 72 114 144}
|
153
|
+
def request_url params={}
|
154
|
+
path = [self.blog_request_url(params), 'avatar'].join('/')
|
155
|
+
size = params[:size]
|
156
|
+
if size
|
157
|
+
unless BlogAvatar.is_valid_size size
|
158
|
+
raise IllegalParamException.new("'#{size}' is illegal")
|
159
|
+
end
|
160
|
+
end # if
|
161
|
+
end
|
162
|
+
|
163
|
+
def self.is_valid_size size
|
164
|
+
VALID_SIZES.include? size.to_s
|
165
|
+
end
|
166
|
+
|
167
|
+
end #BlogAvatar
|
168
|
+
|
169
|
+
class BlogFollowers < Base
|
170
|
+
param :limit, :required => false
|
171
|
+
param :offset, :required => false
|
172
|
+
def request_url params={}
|
173
|
+
blog_cname = params[:blogCName]
|
174
|
+
raise ParamIsRequiredException.new("blogCName is required for interface #{self.class.name}") unless blog_cname
|
175
|
+
API.url_for "/blog/#{blog_cname}/info"
|
176
|
+
end
|
177
|
+
end #BlogFollowers
|
178
|
+
|
179
|
+
class Posts < Base
|
180
|
+
param :tag, :required => false
|
181
|
+
param :limit, :required => false
|
182
|
+
param :offset, :required => false
|
183
|
+
param :id, :required => false
|
184
|
+
param :reblogInfo, :required => false
|
185
|
+
param :notesInfo, :required => false
|
186
|
+
param :filter, :required => false
|
187
|
+
def request_url params={}
|
188
|
+
blog_cname = params[:blogCName]
|
189
|
+
raise ParamIsRequiredException.new("blogCName is required for interface #{self.class.name}") unless blog_cname
|
190
|
+
path = "/blog/#{blog_cname}/posts"
|
191
|
+
if params[:type]
|
192
|
+
path = path + "/#{type}"
|
193
|
+
end
|
194
|
+
API.url_for path
|
195
|
+
end
|
196
|
+
end # Posts
|
197
|
+
|
198
|
+
class HomeFeeds < Base
|
199
|
+
param :limit, :required => false
|
200
|
+
param :offset, :required => false
|
201
|
+
param :type, :required => false
|
202
|
+
param :sinceId, :required => false
|
203
|
+
param :reblogInfo, :required => false
|
204
|
+
param :notesInfo, :required => false
|
205
|
+
param :filter, :required => false
|
206
|
+
def request_url params={}
|
207
|
+
API.url_fo "/user/home"
|
208
|
+
end
|
209
|
+
end # Home
|
210
|
+
|
211
|
+
class TagFeeds < Base
|
212
|
+
param :limit, :required => false
|
213
|
+
param :tag, :required => false
|
214
|
+
param :sinceId, :required => false
|
215
|
+
param :reblogInfo, :required => false
|
216
|
+
param :notesInfo, :required => false
|
217
|
+
param :filter, :required => false
|
218
|
+
|
219
|
+
def request_url params={}
|
220
|
+
tag = params[:tag]
|
221
|
+
raise ParamIsRequiredException("tag is required for interface #{self.class.name}") unless tag
|
222
|
+
API.url_for "/tag/posts/#{tag}"
|
223
|
+
end
|
224
|
+
end #TagFeeds
|
225
|
+
|
226
|
+
class Follow < Base
|
227
|
+
verb :post
|
228
|
+
param :blogCName, :required => true
|
229
|
+
def request_url params={}
|
230
|
+
API.url_for "/user/follow"
|
231
|
+
end
|
232
|
+
end # Follow
|
233
|
+
|
234
|
+
class Unfollow < Base
|
235
|
+
verb :post
|
236
|
+
param :blogCName, :required => true
|
237
|
+
def request_url params={}
|
238
|
+
API.url_for '/user/unfollow'
|
239
|
+
end
|
240
|
+
end # Unfollow
|
241
|
+
|
242
|
+
class Submissions < Base
|
243
|
+
def request_url params={}
|
244
|
+
blog_cname = params[:blogCName]
|
245
|
+
raise ParamIsRequiredException("blogCName is required for interface #{self.class.name}") unless blog_cname
|
246
|
+
API.url_for "/blog/#{blog_cname}/submission"
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end # Interface
|
250
|
+
end
|
251
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'diandian_oauth/api/interface'
|
2
|
+
module DiandianOAuth
|
3
|
+
class API
|
4
|
+
HOST='api.diandian.com'
|
5
|
+
VERSION='v1'
|
6
|
+
|
7
|
+
|
8
|
+
def authorize_url
|
9
|
+
'https://api.diandian.com/oauth/authorize'
|
10
|
+
end
|
11
|
+
|
12
|
+
def token_url
|
13
|
+
'https://api.diandian.com/oauth/token'
|
14
|
+
end
|
15
|
+
|
16
|
+
include Interface
|
17
|
+
interface :user_info, Interface::UserInfo
|
18
|
+
interface :user_likes, Interface::UserLikes
|
19
|
+
interface :user_followings, Interface::UserFollowings
|
20
|
+
interface :my_tags, Interface::MyTags
|
21
|
+
interface :blog, Interface::Blog
|
22
|
+
interface :blog_info, Interface::BlogInfo
|
23
|
+
interface :blog_avatar, Interface::BlogAvatar
|
24
|
+
interface :blog_followers, Interface::BlogFollowers
|
25
|
+
interface :posts, Interface::Posts
|
26
|
+
interface :home_feeds, Interface::HomeFeeds
|
27
|
+
interface :tag_feeds, Interface::TagFeeds
|
28
|
+
interface :follow, Interface::Follow
|
29
|
+
interface :unfollow, Interface::Unfollow
|
30
|
+
interface :submissions, Interface::Submissions
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'oauth2'
|
2
|
+
module DiandianOAuth
|
3
|
+
class Client
|
4
|
+
attr_accessor :api
|
5
|
+
def initialize client_id, client_secret, options={}
|
6
|
+
@api = options[:api] || DiandianOAuth::API.new
|
7
|
+
@redirect_uri = options[:redirect_uri]
|
8
|
+
@client = OAuth2::Client.new client_id, client_secret, {
|
9
|
+
:authorize_url => @api.authorize_url,
|
10
|
+
:token_url => @api.token_url
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
def authorize_url response_type='code', scope=[]
|
15
|
+
if response_type.is_a? Array
|
16
|
+
scope = response_type
|
17
|
+
response_type = 'code'
|
18
|
+
end
|
19
|
+
@client.authorize_url(
|
20
|
+
:client_id => @client.id,
|
21
|
+
:response_type => response_type,
|
22
|
+
:scope => scope.join(',')
|
23
|
+
)
|
24
|
+
end
|
25
|
+
def access_token= hash
|
26
|
+
@access_token = case hash
|
27
|
+
when Hash then OAuth2::AccessToken.from_hash(@client, hash)
|
28
|
+
when OAuth2::AccessToken then hash
|
29
|
+
else
|
30
|
+
raise 'illegal argument hash'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
def access_token code_or_hash=nil
|
34
|
+
return @access_token if @access_token
|
35
|
+
return nil unless code_or_hash
|
36
|
+
if code_or_hash.is_a? Hash
|
37
|
+
@access_token = OAuth2::AccessToken.from_hash(@client, code_or_hash)
|
38
|
+
else
|
39
|
+
begin
|
40
|
+
DiandianOAuth.logger.warn('redirect_url is required for authorization_code grant type') unless @redirect_uri
|
41
|
+
@access_token = @client.get_token(
|
42
|
+
:client_id => @client.id,
|
43
|
+
:client_secret => @client.secret,
|
44
|
+
:grant_type => 'authorization_code',
|
45
|
+
:code => code_or_hash,
|
46
|
+
:redirect_uri => @redirect_uri
|
47
|
+
)
|
48
|
+
rescue OAuth2::Error => e
|
49
|
+
DiandianOAuth.logger.error e.to_s
|
50
|
+
raise e
|
51
|
+
end
|
52
|
+
end
|
53
|
+
@access_token
|
54
|
+
end
|
55
|
+
|
56
|
+
def method_missing(meth, *args, &block)
|
57
|
+
force = meth.to_s =~ /!$/
|
58
|
+
meth = $` if force
|
59
|
+
interface = @api.interface meth.to_sym
|
60
|
+
if interface
|
61
|
+
access_token = self.access_token
|
62
|
+
raise 'access_token is required' unless access_token
|
63
|
+
if force
|
64
|
+
interface.apply! access_token, args[0], &block
|
65
|
+
else
|
66
|
+
interface.apply access_token, args[0], &block
|
67
|
+
end # force
|
68
|
+
else
|
69
|
+
super
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module DiandianOAuth
|
2
|
+
|
3
|
+
class OAuthException < ::Exception
|
4
|
+
end
|
5
|
+
|
6
|
+
class TokenExpiredException < OAuthException
|
7
|
+
end
|
8
|
+
|
9
|
+
class RefreshTokenExpireException < OAuthException
|
10
|
+
end
|
11
|
+
# APIException
|
12
|
+
class APIException < OAuthException
|
13
|
+
end
|
14
|
+
class ParamIsRequiredException < APIException
|
15
|
+
end
|
16
|
+
class IllegalParamException < APIException
|
17
|
+
end
|
18
|
+
# End APIException
|
19
|
+
end
|
data/lib/diandian_oauth.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class APITest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
test 'authorize_url' do
|
6
|
+
api = DiandianOAuth::API.new
|
7
|
+
puts api.authorize_url
|
8
|
+
end
|
9
|
+
|
10
|
+
test 'token_url' do
|
11
|
+
api = DiandianOAuth::API.new
|
12
|
+
puts api.token_url
|
13
|
+
end
|
14
|
+
|
15
|
+
test 'user_info' do
|
16
|
+
api = DiandianOAuth::API.new
|
17
|
+
user_info_interface = api.interface :user_info
|
18
|
+
assert_instance_of(DiandianOAuth::API::Interface::UserInfo, user_info_interface)
|
19
|
+
puts user_info_interface.request_url
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class AuthorizeTest < ActiveSupport::TestCase
|
4
|
+
CLIENT_ID="3zgJwues92"
|
5
|
+
CLIENT_SECRET="1rxLZr5R4sweGUATGig25VBc1ka1xDEpkIqJ"
|
6
|
+
|
7
|
+
test 'authorize_url' do
|
8
|
+
assert_nothing_raised do
|
9
|
+
client = DiandianOAuth::Client.new CLIENT_ID, CLIENT_SECRET
|
10
|
+
puts client.authorize_url %w{read write}
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'access_token' do
|
15
|
+
code = '7KhUBh'
|
16
|
+
unless code.empty?
|
17
|
+
assert_nothing_raised do
|
18
|
+
client = DiandianOAuth::Client.new CLIENT_ID, CLIENT_SECRET, :redirect_uri => 'http://example.com/callback'
|
19
|
+
access_token = client.access_token code
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class InterfaceTest < ActiveSupport::TestCase
|
4
|
+
CLIENT_ID="3zgJwues92"
|
5
|
+
CLIENT_SECRET="1rxLZr5R4sweGUATGig25VBc1ka1xDEpkIqJ"
|
6
|
+
ACCESS_TOKEN = {
|
7
|
+
:access_token => 'adbab585-ea39-47e0-87ce-da7700fd3d9b',
|
8
|
+
:refresh_token => "79852349-5f2e-4e3b-93b8-845e239e61ff",
|
9
|
+
:token_type => "bearer",
|
10
|
+
:expires_in => 604799,
|
11
|
+
:expires_at => 1344317317,
|
12
|
+
:scope => "write read",
|
13
|
+
:uid => "11449"
|
14
|
+
}
|
15
|
+
|
16
|
+
test 'refresh_token' do
|
17
|
+
client = self.client
|
18
|
+
client.access_token= ACCESS_TOKEN
|
19
|
+
p client.access_token.refresh!
|
20
|
+
end
|
21
|
+
|
22
|
+
test 'user_info' do
|
23
|
+
client = self.client
|
24
|
+
client.access_token= ACCESS_TOKEN
|
25
|
+
begin
|
26
|
+
p client.user_info
|
27
|
+
rescue DiandianOAuth::API::TokenExpiredException => e
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
test 'posts' do
|
32
|
+
client = self.client
|
33
|
+
client.access_token= ACCESS_TOKEN
|
34
|
+
p client.posts :blogCName => 'secretworry'
|
35
|
+
end
|
36
|
+
|
37
|
+
def client
|
38
|
+
@client ||= DiandianOAuth::Client.new CLIENT_ID, CLIENT_SECRET, :redirect_uri => 'http://example.com/callback'
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class LoggerTest < ActiveSupport::TestCase
|
4
|
+
test 'default logger' do
|
5
|
+
assert_nothing_raised do
|
6
|
+
DiandianOAuth.logger.info("test")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
test 'change logger' do
|
11
|
+
assert_nothing_raised do
|
12
|
+
DiandianOAuth.logger(STDERR)
|
13
|
+
DiandianOAuth.logger.info("test")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: diandian-oauth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Rainer Du
|
@@ -11,8 +11,40 @@ bindir: bin
|
|
11
11
|
cert_chain: []
|
12
12
|
|
13
13
|
date: 2012-07-24 00:00:00 Z
|
14
|
-
dependencies:
|
15
|
-
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: oauth2
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.8.0
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: activemodel
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 3.2.6
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: activesupport
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.2.1
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id003
|
16
48
|
description: A Ruby Client for diandian API
|
17
49
|
email: dusiyu@diandian.com
|
18
50
|
executables: []
|
@@ -22,7 +54,27 @@ extensions: []
|
|
22
54
|
extra_rdoc_files: []
|
23
55
|
|
24
56
|
files:
|
57
|
+
- .gitignore
|
58
|
+
- Gemfile
|
59
|
+
- Gemfile.lock
|
60
|
+
- README.md
|
61
|
+
- diandian-oauth.gemspec
|
62
|
+
- lib/diandian.rb
|
63
|
+
- lib/diandian/model.rb
|
64
|
+
- lib/diandian/model/base.rb
|
65
|
+
- lib/diandian/model/blog.rb
|
66
|
+
- lib/diandian/model/post.rb
|
67
|
+
- lib/diandian/model/user.rb
|
25
68
|
- lib/diandian_oauth.rb
|
69
|
+
- lib/diandian_oauth/api.rb
|
70
|
+
- lib/diandian_oauth/api/interface.rb
|
71
|
+
- lib/diandian_oauth/client.rb
|
72
|
+
- lib/diandian_oauth/exceptions.rb
|
73
|
+
- test/diandian_oauth/api_test.rb
|
74
|
+
- test/diandian_oauth/authorize_test.rb
|
75
|
+
- test/diandian_oauth/interface_test.rb
|
76
|
+
- test/diandian_oauth/logger_test.rb
|
77
|
+
- test/diandian_oauth/test_helper.rb
|
26
78
|
homepage: https://github.com/secretworry/diandian-oauth
|
27
79
|
licenses: []
|
28
80
|
|
@@ -50,5 +102,9 @@ rubygems_version: 1.8.15
|
|
50
102
|
signing_key:
|
51
103
|
specification_version: 3
|
52
104
|
summary: Client for diandian API
|
53
|
-
test_files:
|
54
|
-
|
105
|
+
test_files:
|
106
|
+
- test/diandian_oauth/api_test.rb
|
107
|
+
- test/diandian_oauth/authorize_test.rb
|
108
|
+
- test/diandian_oauth/interface_test.rb
|
109
|
+
- test/diandian_oauth/logger_test.rb
|
110
|
+
- test/diandian_oauth/test_helper.rb
|