weibo_2 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +25 -0
- data/example/example.rb +17 -9
- data/example/views/layout.haml +3 -1
- data/lib/weibo_2/access_token.rb +12 -0
- data/lib/weibo_2/client.rb +2 -2
- data/lib/weibo_2/version.rb +17 -1
- data/spec/client_spec.rb +44 -0
- data/weibo_2.gemspec +3 -1
- metadata +21 -3
data/README.md
CHANGED
@@ -77,4 +77,29 @@ It should work.
|
|
77
77
|
pic = File.open(tmpfile.path)
|
78
78
|
client.statuses.upload(params[:status], pic)
|
79
79
|
```
|
80
|
+
## Setting up SSL certificates
|
81
|
+
|
82
|
+
This gem using [faraday](https://github.com/technoweenie/faraday) for connection, which supports ssl. According to [this article](https://github.com/technoweenie/faraday/wiki/Setting-up-SSL-certificates), you can do as following to support ssl connection.
|
83
|
+
|
84
|
+
### Ubuntu
|
85
|
+
|
86
|
+
To locate your SSL certificate folder, type `openssl version -a`. Append `/certs` to the OPENSSLDIR listed, here it would be `/usr/lib/ssl/certs`.
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
client = WeiboOAuth2::Client.new(YOUR_KEY, YOUR_SECRET, :ssl => {:ca_path => "/usr/lib/ssl/certs"})
|
90
|
+
# or as below if you have set WeiboOAuth2::Config.api_key and WeiboOAuth2::Config.api_secret already
|
91
|
+
# client = WeiboOAuth2::Client.new('', '', :ssl => {:ca_path => "/usr/lib/ssl/certs"})
|
92
|
+
```
|
93
|
+
|
94
|
+
### On Heroku, Fedora, CentOS
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
client = WeiboOAuth2::Client.new(YOUR_KEY, YOUR_SECRET, :ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'})
|
98
|
+
# or as below if you have set WeiboOAuth2::Config.api_key and WeiboOAuth2::Config.api_secret already
|
99
|
+
# client = WeiboOAuth2::Client.new('', '', :ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'})
|
100
|
+
```
|
101
|
+
|
102
|
+
For Fedora and CentOS, use the path and file `/etc/pki/tls/certs/ca-bundle.crt` instead, or find your system path with `openssl version -a`.
|
103
|
+
|
104
|
+
|
80
105
|
|
data/example/example.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'weibo_2'
|
2
4
|
require 'time-ago-in-words'
|
3
5
|
|
@@ -10,18 +12,17 @@ WeiboOAuth2::Config.api_key = ENV['KEY']
|
|
10
12
|
WeiboOAuth2::Config.api_secret = ENV['SECRET']
|
11
13
|
WeiboOAuth2::Config.redirect_uri = ENV['REDIR_URI']
|
12
14
|
|
13
|
-
client = WeiboOAuth2::Client.new
|
14
|
-
|
15
15
|
get '/' do
|
16
|
-
|
17
|
-
puts session.inspect
|
18
|
-
puts !client.authorized?
|
16
|
+
client = WeiboOAuth2::Client.new
|
19
17
|
if session[:access_token] && !client.authorized?
|
20
18
|
token = client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})
|
21
|
-
|
22
|
-
|
19
|
+
p "*" * 80 + "validated"
|
20
|
+
p token.inspect
|
21
|
+
p token.validated?
|
22
|
+
|
23
|
+
unless token.validated?
|
23
24
|
reset_session
|
24
|
-
redirect 'connect'
|
25
|
+
redirect '/connect'
|
25
26
|
return
|
26
27
|
end
|
27
28
|
end
|
@@ -33,14 +34,18 @@ get '/' do
|
|
33
34
|
end
|
34
35
|
|
35
36
|
get '/connect' do
|
37
|
+
client = WeiboOAuth2::Client.new
|
36
38
|
redirect client.authorize_url
|
37
39
|
end
|
38
40
|
|
39
41
|
get '/callback' do
|
42
|
+
client = WeiboOAuth2::Client.new
|
40
43
|
access_token = client.auth_code.get_token(params[:code].to_s)
|
41
44
|
session[:uid] = access_token.params["uid"]
|
42
45
|
session[:access_token] = access_token.token
|
43
46
|
session[:expires_at] = access_token.expires_at
|
47
|
+
p "*" * 80 + "callback"
|
48
|
+
p access_token.inspect
|
44
49
|
@user = client.users.show_by_uid(session[:uid].to_i)
|
45
50
|
redirect '/'
|
46
51
|
end
|
@@ -56,13 +61,16 @@ get '/screen.css' do
|
|
56
61
|
end
|
57
62
|
|
58
63
|
post '/update' do
|
64
|
+
client = WeiboOAuth2::Client.new
|
65
|
+
client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})
|
59
66
|
statuses = client.statuses
|
60
67
|
|
61
68
|
unless params[:file] && (tmpfile = params[:file][:tempfile]) && (name = params[:file][:filename])
|
62
69
|
statuses.update(params[:status])
|
63
70
|
else
|
71
|
+
status = params[:status] || '图片'
|
64
72
|
pic = File.open(tmpfile.path)
|
65
|
-
statuses.upload(
|
73
|
+
statuses.upload(status, pic)
|
66
74
|
end
|
67
75
|
|
68
76
|
redirect '/'
|
data/example/views/layout.haml
CHANGED
data/lib/weibo_2/access_token.rb
CHANGED
@@ -4,5 +4,17 @@ module WeiboOAuth2
|
|
4
4
|
def validated?
|
5
5
|
!!@expires_at && !expired?
|
6
6
|
end
|
7
|
+
|
8
|
+
def expired?
|
9
|
+
expires? && (expires_at < self.time_convertion(Time.now, '+08:00').to_i)
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
#Convert Time from a time zone to another time zone
|
14
|
+
#'+08:00' or '-08:00'
|
15
|
+
#return Time
|
16
|
+
def self.time_convertion(time, time_zone)
|
17
|
+
time.getutc.getlocal(time_zone)
|
18
|
+
end
|
7
19
|
end
|
8
20
|
end
|
data/lib/weibo_2/client.rb
CHANGED
@@ -30,8 +30,8 @@ module WeiboOAuth2
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def get_token_from_hash(hash)
|
33
|
-
access_token = hash.delete(
|
34
|
-
opts = {:expires_at => hash
|
33
|
+
access_token = hash.delete(:access_token) || hash.delete('access_token')
|
34
|
+
opts = {:expires_at => (hash.delete(:expires_at) || hash.delete('expires_at')),
|
35
35
|
:header_format => "OAuth2 %s",
|
36
36
|
:param_name => "access_token"}
|
37
37
|
|
data/lib/weibo_2/version.rb
CHANGED
@@ -1,3 +1,19 @@
|
|
1
1
|
module WeiboOAuth2
|
2
|
-
|
2
|
+
|
3
|
+
class Version
|
4
|
+
MAJOR = 0 unless defined? MAJOR
|
5
|
+
MINOR = 0 unless defined? MINOR
|
6
|
+
PATCH = 8 unless defined? PATCH
|
7
|
+
PRE = nil unless defined? PRE
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
# @return [String]
|
12
|
+
def to_s
|
13
|
+
[MAJOR, MINOR, PATCH, PRE].compact.join('.')
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
3
19
|
end
|
data/spec/client_spec.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'weibo_2'
|
2
|
+
|
3
|
+
WeiboOAuth2::Config.api_key = 'abc'
|
4
|
+
WeiboOAuth2::Config.api_secret = 'def'
|
5
|
+
WeiboOAuth2::Config.redirect_uri = 'https://example.com/callback'
|
6
|
+
|
7
|
+
describe WeiboOAuth2::Client do
|
8
|
+
it 'should assign id and secret from config' do
|
9
|
+
subject.id.should == 'abc'
|
10
|
+
subject.secret.should == 'def'
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should assign site from default' do
|
14
|
+
subject.site.should == 'https://api.weibo.com/2/'
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should get authorize_url' do
|
18
|
+
authorize_url = 'https://api.weibo.com/oauth2/authorize?client_id=abc&response_type=code&redirect_uri=https%3A%2F%2Fexample.com%2Fcallback'
|
19
|
+
subject.authorize_url.should == authorize_url
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should leave Faraday::Connection#ssl unset' do
|
23
|
+
subject.connection.ssl.should == {}
|
24
|
+
end
|
25
|
+
|
26
|
+
it "defaults raise_errors to true" do
|
27
|
+
subject.options[:raise_errors].should be_true
|
28
|
+
end
|
29
|
+
|
30
|
+
it "allows true/false for raise_errors option" do
|
31
|
+
client = OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com', :raise_errors => false)
|
32
|
+
client.options[:raise_errors].should be_false
|
33
|
+
client = OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com', :raise_errors => true)
|
34
|
+
client.options[:raise_errors].should be_true
|
35
|
+
end
|
36
|
+
|
37
|
+
it "allows get/post for access_token_method option" do
|
38
|
+
client = OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com', :access_token_method => :get)
|
39
|
+
client.options[:access_token_method].should == :get
|
40
|
+
client = OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com', :access_token_method => :post)
|
41
|
+
client.options[:access_token_method].should == :post
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/weibo_2.gemspec
CHANGED
@@ -13,7 +13,9 @@ Gem::Specification.new do |gem|
|
|
13
13
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
14
|
gem.name = "weibo_2"
|
15
15
|
gem.require_paths = ["lib"]
|
16
|
-
gem.version = WeiboOAuth2::
|
16
|
+
gem.version = WeiboOAuth2::Version
|
17
|
+
|
18
|
+
gem.add_development_dependency "rspec", "~> 2.6"
|
17
19
|
|
18
20
|
gem.add_runtime_dependency 'oauth2', "~> 0.8.0"
|
19
21
|
gem.add_runtime_dependency 'hashie', "~> 1.2.0"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weibo_2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,24 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.6'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.6'
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
name: oauth2
|
16
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -121,6 +137,7 @@ files:
|
|
121
137
|
- lib/weibo_2/oauth.rb
|
122
138
|
- lib/weibo_2/strategy/auth_code.rb
|
123
139
|
- lib/weibo_2/version.rb
|
140
|
+
- spec/client_spec.rb
|
124
141
|
- weibo_2.gemspec
|
125
142
|
homepage: ''
|
126
143
|
licenses: []
|
@@ -146,5 +163,6 @@ rubygems_version: 1.8.24
|
|
146
163
|
signing_key:
|
147
164
|
specification_version: 3
|
148
165
|
summary: A oauth2 gem for weibo
|
149
|
-
test_files:
|
166
|
+
test_files:
|
167
|
+
- spec/client_spec.rb
|
150
168
|
has_rdoc:
|