weibo_2 0.1.2 → 0.1.3
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.
- checksums.yaml +6 -14
- data/.gitignore +1 -0
- data/README.md +10 -7
- data/lib/weibo_2/api/v2/base.rb +28 -13
- data/lib/weibo_2/api/v2/place.rb +1 -1
- data/lib/weibo_2/api/v2/statuses.rb +1 -1
- data/lib/weibo_2/version.rb +1 -1
- metadata +6 -13
- data/example/.sass-cache/80b2331c862c6e430e0af18caf62d12d982610f0/screen.sassc +0 -0
- data/example/.sass-cache/faf9751221db0b5ce76d45351b9591c3bfc59441/screen.sassc +0 -0
- data/example/Gemfile +0 -7
- data/example/config.ru +0 -2
- data/example/example.rb +0 -85
- data/example/views/index.haml +0 -42
- data/example/views/layout.haml +0 -12
- data/example/views/screen.sass +0 -13
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
metadata.gz: !binary |-
|
9
|
-
ODM1YzIyMDllMzkzNjlkZjIxOWE0YjRhZGY0ZGM5OWJmZjEwNzMwZjY1YjRl
|
10
|
-
MmVjYzQ5M2U2MDVjMTAzNmM2ZmY1ZTg5YWVhNTQwYWQ2NzAzN2FlNzU0NDcw
|
11
|
-
Zjg0ZjRlMWM1ZTk5MDVlYTQzZjZlODc3Yjc3NjA2MzJlYjI5Yzk=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
NjAwZjVkZjM4N2RkZDM5YTg3ZmY2YzI5YzMyOTdiMjdlYmNlZGFhNDY3MmU5
|
14
|
-
MzRhMzZjYjJhZjJmNmY3OTc0ZDdkM2M3NTMzOWY5ZTlmYWU0YjEyZmFjZTZl
|
15
|
-
YzUzMWViY2NkNDdhYWM0MGM1NDk2YjNjOTI4Nzc4MzQ3YzkyODY=
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1b299aa2089651b433978bf94453b12d7d774195
|
4
|
+
data.tar.gz: 6d4e020781516c7f8fd8e35f2c4c9ddb81df011e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ef22e739cb358b42a7c485a1ef0af9f9ba0820529723c4dc9713b904c398f4f4741172dd89d6a5402b67150834c30b3cb1557a2bbfd4333a961bbab601d2c78a
|
7
|
+
data.tar.gz: 847c227c70be1d65873c94cd560ce7aa65e2840f1c2c1312013c6763902b9904e0cf1bb5c1192f1383e7f844d4bd8bb3a88a71fff6e2652da42d38407d359d6a
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@ Note: I got a lot of inspiration from [this gem](https://github.com/ballantyne/w
|
|
2
2
|
|
3
3
|
# WeiboOAuth2
|
4
4
|
|
5
|
-
WeioOAuth2 is a Ruby gem that provides a wrapper for interacting with sina weibo's v2 [API](http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2), which is currently the latest version. Check out the link if you are interested in browsing the details. The output data format is Hashie::Mash, check out [here](https://github.com/intridea/hashie#mash). If you are not familiar with oauth2, I recommend that you read this [article](http://open.weibo.com/wiki
|
5
|
+
WeioOAuth2 is a Ruby gem that provides a wrapper for interacting with sina weibo's v2 [API](http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2), which is currently the latest version. Check out the link if you are interested in browsing the details. The output data format is Hashie::Mash, check out [here](https://github.com/intridea/hashie#mash). If you are not familiar with oauth2, I recommend that you read this [article](http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6%E8%AF%B4%E6%98%8E).
|
6
6
|
|
7
7
|
## Requirements
|
8
8
|
|
@@ -20,9 +20,9 @@ $ gem install weibo_2
|
|
20
20
|
|
21
21
|
## Basic Usage
|
22
22
|
|
23
|
-
The example
|
23
|
+
The example have been moved to [here](https://github.com/simsicon/weibo_2_example). It wiil be more convenient to update.
|
24
24
|
|
25
|
-
|
25
|
+
The [example](https://github.com/simsicon/weibo_2_example) written with sinatra shows how to ask for oauth2 permission, get the token and send status with picture. It should cover basic usage in all ruby apps. You can run your own demo!
|
26
26
|
|
27
27
|
```bash
|
28
28
|
$ KEY=change_this_to_your_key SECRET=change_this_to_your_secret REDIR_URI=change_this_to_your_redir_uri ruby example.rb
|
@@ -70,13 +70,16 @@ It should work.
|
|
70
70
|
client.statuses.update(params[:status])
|
71
71
|
```
|
72
72
|
|
73
|
-
Upload a picture
|
73
|
+
Upload a picture.
|
74
74
|
|
75
75
|
```ruby
|
76
|
-
tmpfile = params[:file]
|
77
|
-
|
78
|
-
client.statuses.upload(params[:status], pic)
|
76
|
+
tmpfile = params[:file].delete(:tempfile)
|
77
|
+
File.open(tmpfile.path, 'rb'){|pic| client.statuses.upload(params[:status], pic, params[:file])}
|
79
78
|
```
|
79
|
+
|
80
|
+
pass params[:file] into upload method as options could help weibo_2 to build post body, useful options as:
|
81
|
+
* filename, filename with extension of the uploading file, example 'pic.jpg'
|
82
|
+
* type, mime type of the uploading file, example 'image/jpeg'
|
80
83
|
## Setting up SSL certificates
|
81
84
|
|
82
85
|
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.
|
data/lib/weibo_2/api/v2/base.rb
CHANGED
@@ -26,32 +26,47 @@ module WeiboOAuth2
|
|
26
26
|
end
|
27
27
|
|
28
28
|
protected
|
29
|
-
def
|
29
|
+
def mime_type(file)
|
30
30
|
case
|
31
|
-
when file =~ /\.
|
31
|
+
when file =~ /\.jpeg$/i then 'image/jpeg'
|
32
|
+
when file =~ /\.jpg$/i then 'image/jpeg'
|
32
33
|
when file =~ /\.gif$/i then 'image/gif'
|
33
34
|
when file =~ /\.png$/i then 'image/png'
|
34
|
-
|
35
|
+
when file =~ /\.tiff$/i then 'image/tiff'
|
36
|
+
else 'image/jpeg'
|
35
37
|
end
|
36
38
|
end
|
39
|
+
|
40
|
+
def bin_encode(chunk)
|
41
|
+
chunk.force_encoding(Encoding::BINARY) if chunk.respond_to? :force_encoding
|
42
|
+
end
|
37
43
|
|
38
44
|
CRLF = "\r\n"
|
39
|
-
def
|
45
|
+
def build_multipart_bodies(parts, opts)
|
40
46
|
boundary = Time.now.to_i.to_s(16)
|
41
|
-
|
47
|
+
|
48
|
+
body = bin_encode('')
|
49
|
+
|
42
50
|
parts.each do |key, value|
|
43
51
|
esc_key = CGI.escape(key.to_s)
|
44
|
-
body << "--#{boundary}#{CRLF}"
|
52
|
+
body << bin_encode("--#{boundary}#{CRLF}")
|
45
53
|
if value.respond_to?(:read)
|
46
|
-
|
47
|
-
|
48
|
-
|
54
|
+
filename = opts.delete(:filename) || File.basename(value.path)
|
55
|
+
mime_type = opts.delete(:type) || mime_type(value.path)
|
56
|
+
|
57
|
+
body << bin_encode("Content-Disposition: form-data; name=\"#{esc_key}\"; filename=\"#{filename}\"#{CRLF}")
|
58
|
+
body << bin_encode("Content-Type: #{mime_type}#{CRLF*2}")
|
59
|
+
body << bin_encode(value.read)
|
49
60
|
else
|
50
|
-
body << "Content-Disposition: form-data; name=\"#{esc_key}\"#{CRLF*2}#{value}"
|
61
|
+
body << bin_encode("Content-Disposition: form-data; name=\"#{esc_key}\"#{CRLF*2}#{value}")
|
51
62
|
end
|
52
|
-
body <<
|
53
|
-
|
54
|
-
|
63
|
+
body << bin_encode("Content-Transfer-Encoding: binary")
|
64
|
+
body << bin_encode(CRLF)
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
body << bin_encode("--#{boundary}--#{CRLF*2}")
|
69
|
+
|
55
70
|
{
|
56
71
|
:body => body,
|
57
72
|
:headers => {"Content-Type" => "multipart/form-data; boundary=#{boundary}"}
|
data/lib/weibo_2/api/v2/place.rb
CHANGED
@@ -98,7 +98,7 @@ module WeiboOAuth2
|
|
98
98
|
end
|
99
99
|
|
100
100
|
def pois_add_photo(poiid, status, pic, opt={})
|
101
|
-
multipart =
|
101
|
+
multipart = build_multipart_bodies({"poiid" => poiid, "status" => status, "pic" => pic}, opt)
|
102
102
|
hashie post("place/pois/add_photo.json", :headers => multipart[:headers], :body => multipart[:body])
|
103
103
|
end
|
104
104
|
|
@@ -99,7 +99,7 @@ module WeiboOAuth2
|
|
99
99
|
|
100
100
|
|
101
101
|
def upload(status, pic, opt={})
|
102
|
-
multipart =
|
102
|
+
multipart = build_multipart_bodies({"status" => status, "pic" => pic}, opt)
|
103
103
|
hashie post("statuses/upload.json", :headers => multipart[:headers], :body => multipart[:body])
|
104
104
|
end
|
105
105
|
|
data/lib/weibo_2/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weibo_2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- simsicon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-05-
|
11
|
+
date: 2013-05-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -80,7 +80,7 @@ dependencies:
|
|
80
80
|
- - ~>
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 1.6.7
|
83
|
-
description:
|
83
|
+
description: "WeioOAuth2 is a Ruby gem that provides a wrapper for interacting with
|
84
84
|
sina weibo's v2 API, \n which is currently the latest version.
|
85
85
|
The output data format is Hashie::Mash"
|
86
86
|
email:
|
@@ -94,14 +94,6 @@ files:
|
|
94
94
|
- LICENSE
|
95
95
|
- README.md
|
96
96
|
- Rakefile
|
97
|
-
- example/.sass-cache/80b2331c862c6e430e0af18caf62d12d982610f0/screen.sassc
|
98
|
-
- example/.sass-cache/faf9751221db0b5ce76d45351b9591c3bfc59441/screen.sassc
|
99
|
-
- example/Gemfile
|
100
|
-
- example/config.ru
|
101
|
-
- example/example.rb
|
102
|
-
- example/views/index.haml
|
103
|
-
- example/views/layout.haml
|
104
|
-
- example/views/screen.sass
|
105
97
|
- lib/weibo_2.rb
|
106
98
|
- lib/weibo_2/access_token.rb
|
107
99
|
- lib/weibo_2/api/v2/account.rb
|
@@ -139,12 +131,12 @@ require_paths:
|
|
139
131
|
- lib
|
140
132
|
required_ruby_version: !ruby/object:Gem::Requirement
|
141
133
|
requirements:
|
142
|
-
- -
|
134
|
+
- - '>='
|
143
135
|
- !ruby/object:Gem::Version
|
144
136
|
version: '0'
|
145
137
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
138
|
requirements:
|
147
|
-
- -
|
139
|
+
- - '>='
|
148
140
|
- !ruby/object:Gem::Version
|
149
141
|
version: '0'
|
150
142
|
requirements: []
|
@@ -155,3 +147,4 @@ specification_version: 4
|
|
155
147
|
summary: A oauth2 gem for weibo
|
156
148
|
test_files:
|
157
149
|
- spec/client_spec.rb
|
150
|
+
has_rdoc:
|
Binary file
|
Binary file
|
data/example/Gemfile
DELETED
data/example/config.ru
DELETED
data/example/example.rb
DELETED
@@ -1,85 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'weibo_2'
|
4
|
-
require 'time-ago-in-words'
|
5
|
-
|
6
|
-
%w(rubygems bundler).each { |dependency| require dependency }
|
7
|
-
Bundler.setup
|
8
|
-
%w(sinatra haml sass).each { |dependency| require dependency }
|
9
|
-
enable :sessions
|
10
|
-
|
11
|
-
WeiboOAuth2::Config.api_key = ENV['KEY']
|
12
|
-
WeiboOAuth2::Config.api_secret = ENV['SECRET']
|
13
|
-
WeiboOAuth2::Config.redirect_uri = ENV['REDIR_URI']
|
14
|
-
|
15
|
-
get '/' do
|
16
|
-
client = WeiboOAuth2::Client.new
|
17
|
-
if session[:access_token] && !client.authorized?
|
18
|
-
token = client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})
|
19
|
-
p "*" * 80 + "validated"
|
20
|
-
p token.inspect
|
21
|
-
p token.validated?
|
22
|
-
|
23
|
-
unless token.validated?
|
24
|
-
reset_session
|
25
|
-
redirect '/connect'
|
26
|
-
return
|
27
|
-
end
|
28
|
-
end
|
29
|
-
if session[:uid]
|
30
|
-
@user = client.users.show_by_uid(session[:uid])
|
31
|
-
@statuses = client.statuses
|
32
|
-
end
|
33
|
-
haml :index
|
34
|
-
end
|
35
|
-
|
36
|
-
get '/connect' do
|
37
|
-
client = WeiboOAuth2::Client.new
|
38
|
-
redirect client.authorize_url
|
39
|
-
end
|
40
|
-
|
41
|
-
get '/callback' do
|
42
|
-
client = WeiboOAuth2::Client.new
|
43
|
-
access_token = client.auth_code.get_token(params[:code].to_s)
|
44
|
-
session[:uid] = access_token.params["uid"]
|
45
|
-
session[:access_token] = access_token.token
|
46
|
-
session[:expires_at] = access_token.expires_at
|
47
|
-
p "*" * 80 + "callback"
|
48
|
-
p access_token.inspect
|
49
|
-
@user = client.users.show_by_uid(session[:uid].to_i)
|
50
|
-
redirect '/'
|
51
|
-
end
|
52
|
-
|
53
|
-
get '/logout' do
|
54
|
-
reset_session
|
55
|
-
redirect '/'
|
56
|
-
end
|
57
|
-
|
58
|
-
get '/screen.css' do
|
59
|
-
content_type 'text/css'
|
60
|
-
sass :screen
|
61
|
-
end
|
62
|
-
|
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]})
|
66
|
-
statuses = client.statuses
|
67
|
-
|
68
|
-
unless params[:file] && (tmpfile = params[:file][:tempfile]) && (name = params[:file][:filename])
|
69
|
-
statuses.update(params[:status])
|
70
|
-
else
|
71
|
-
status = params[:status] || '图片'
|
72
|
-
pic = File.open(tmpfile.path)
|
73
|
-
statuses.upload(status, pic)
|
74
|
-
end
|
75
|
-
|
76
|
-
redirect '/'
|
77
|
-
end
|
78
|
-
|
79
|
-
helpers do
|
80
|
-
def reset_session
|
81
|
-
session[:uid] = nil
|
82
|
-
session[:access_token] = nil
|
83
|
-
session[:expires_at] = nil
|
84
|
-
end
|
85
|
-
end
|
data/example/views/index.haml
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
-if @user
|
2
|
-
%p
|
3
|
-
%h5
|
4
|
-
hello! you are
|
5
|
-
=@user.screen_name
|
6
|
-
|
7
|
-
%h3
|
8
|
-
make a weibo!
|
9
|
-
%form{:action => "/update", :method => "POST", :enctype => "multipart/form-data"}
|
10
|
-
%textarea{:id => 'status', :name => "status", :cols => "62", :rows => "5"}
|
11
|
-
%br
|
12
|
-
%input{:id => 'file', :name => 'file', :type => 'file'}
|
13
|
-
%br
|
14
|
-
%input{:type => "submit", :value => "Send"}
|
15
|
-
|
16
|
-
%br
|
17
|
-
%br
|
18
|
-
%br
|
19
|
-
%h3
|
20
|
-
home timeline
|
21
|
-
.statuses
|
22
|
-
-@statuses.friends_timeline.statuses.each do |status|
|
23
|
-
.status
|
24
|
-
%p.text
|
25
|
-
=status.text
|
26
|
-
|
27
|
-
|
28
|
-
%br
|
29
|
-
-if status.bmiddle_pic
|
30
|
-
%img{:src => status.bmiddle_pic, :alt => "", :class => 'pic'}
|
31
|
-
%br
|
32
|
-
.time_and_by
|
33
|
-
=Time.parse(status.created_at).ago_in_words
|
34
|
-
by
|
35
|
-
=status.user.screen_name
|
36
|
-
from
|
37
|
-
=status.source
|
38
|
-
%br
|
39
|
-
%br
|
40
|
-
%br
|
41
|
-
%br
|
42
|
-
|
data/example/views/layout.haml
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
!!!
|
2
|
-
%html(xml:lang='en' lang='en' xmlns='http://www.w3.org/1999/xhtml')
|
3
|
-
%head
|
4
|
-
%meta(content='text/html;charset=UTF-8' http-equiv='content-type')
|
5
|
-
%title="weibo oauth2 api example"
|
6
|
-
%link{:href => "/screen.css", :rel =>"stylesheet", :type => "text/css", :media => "screen" }
|
7
|
-
%body
|
8
|
-
%h2="weibo oauth2 api example"
|
9
|
-
%a{:href => (session[:uid] ? "/logout" : "/connect")}=(session[:uid] ? "logout" : "connect")
|
10
|
-
= yield
|
11
|
-
%footer
|
12
|
-
%h6="http://github.com/simsicon/weibo_2"
|