weibo_focus 1.0.8 → 1.0.9

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.
Files changed (46) hide show
  1. data/.gitignore +17 -0
  2. data/example/.sass-cache/0d4365dd4f25a1b2574fe0fe2e903692b6df08f1/screen.sassc +0 -0
  3. data/example/.sass-cache/80b2331c862c6e430e0af18caf62d12d982610f0/screen.sassc +0 -0
  4. data/example/.sass-cache/faf9751221db0b5ce76d45351b9591c3bfc59441/screen.sassc +0 -0
  5. data/example/Gemfile +7 -0
  6. data/example/Gemfile~ +7 -0
  7. data/example/config.ru +2 -0
  8. data/example/example.rb +84 -0
  9. data/example/example.rb~ +84 -0
  10. data/example/views/index.haml +42 -0
  11. data/example/views/layout.haml +12 -0
  12. data/example/views/screen.sass +13 -0
  13. data/example/weibo.rb +7 -0
  14. data/lib/weibo_focus/access_token.rb +20 -0
  15. data/lib/weibo_focus/api/v2/account.rb +31 -0
  16. data/lib/weibo_focus/api/v2/base.rb +64 -0
  17. data/lib/weibo_focus/api/v2/comments.rb +51 -0
  18. data/lib/weibo_focus/api/v2/common.rb +28 -0
  19. data/lib/weibo_focus/api/v2/favorites.rb +59 -0
  20. data/lib/weibo_focus/api/v2/friendships.rb +64 -0
  21. data/lib/weibo_focus/api/v2/location.rb +83 -0
  22. data/lib/weibo_focus/api/v2/notification.rb +12 -0
  23. data/lib/weibo_focus/api/v2/place.rb +123 -0
  24. data/lib/weibo_focus/api/v2/register.rb +14 -0
  25. data/lib/weibo_focus/api/v2/remind.rb +23 -0
  26. data/lib/weibo_focus/api/v2/search.rb +38 -0
  27. data/lib/weibo_focus/api/v2/short_url.rb +49 -0
  28. data/lib/weibo_focus/api/v2/statuses.rb +117 -0
  29. data/lib/weibo_focus/api/v2/suggestions.rb +45 -0
  30. data/lib/weibo_focus/api/v2/tags.rb +36 -0
  31. data/lib/weibo_focus/api/v2/trends.rb +39 -0
  32. data/lib/weibo_focus/api/v2/users.rb +30 -0
  33. data/lib/weibo_focus/base.rb +6 -0
  34. data/lib/weibo_focus/client.rb +102 -0
  35. data/lib/weibo_focus/config.rb +28 -4
  36. data/lib/weibo_focus/config.rb~ +30 -0
  37. data/lib/weibo_focus/oauth.rb +6 -0
  38. data/lib/weibo_focus/strategy/auth_code.rb +12 -0
  39. data/lib/weibo_focus/version.rb +1 -1
  40. data/lib/weibo_focus/version.rb~ +3 -0
  41. data/lib/weibo_focus.rb +20 -9
  42. data/lib/weibo_focus.rb~ +51 -0
  43. data/spec/weibo_spec.rb +44 -0
  44. data/weibo_focus.gemspec +11 -13
  45. data/weibo_focus.gemspec~ +25 -0
  46. metadata +128 -6
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/example/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source :rubygems
2
+
3
+ gem 'sinatra'
4
+ gem "haml"
5
+ gem "sass"
6
+ gem 'weibo_focus'
7
+ gem 'time-ago-in-words'
data/example/Gemfile~ ADDED
@@ -0,0 +1,7 @@
1
+ source :rubygems
2
+
3
+ gem 'sinatra'
4
+ gem "haml"
5
+ gem "sass"
6
+ gem 'weibo_focus'
7
+ gem 'time-ago-in-words'
data/example/config.ru ADDED
@@ -0,0 +1,2 @@
1
+ require './example.rb'
2
+ run Sinatra::Application
@@ -0,0 +1,84 @@
1
+ # encoding: utf-8
2
+
3
+ require 'weibo_focus'
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
+
10
+ Weibo::Config.api_key = "1713173982"
11
+ Weibo::Config.api_secret = "62170dacd0fa1c70d4eb1263a0855744"
12
+ Weibo::Config.redirect_uri = "http://192.168.184.130:4567/callback"
13
+
14
+ get '/' do
15
+ client = Weibo::Client.new
16
+ if session[:access_token] && !client.authorized?
17
+ token = client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})
18
+ p "*" * 80 + "validated"
19
+ p token.inspect
20
+ p token.validated?
21
+
22
+ unless token.validated?
23
+ reset_session
24
+ redirect '/connect'
25
+ return
26
+ end
27
+ end
28
+ if session[:uid]
29
+ @user = client.users.show_by_uid(session[:uid])
30
+ @statuses = client.statuses
31
+ end
32
+ haml :index
33
+ end
34
+
35
+ get '/connect' do
36
+ client = Weibo::Client.new
37
+ redirect client.authorize_url
38
+ end
39
+
40
+ get '/callback' do
41
+ client = Weibo::Client.new
42
+ access_token = client.auth_code.get_token(params[:code].to_s)
43
+ session[:uid] = access_token.params["uid"]
44
+ session[:access_token] = access_token.token
45
+ session[:expires_at] = access_token.expires_at
46
+ p "*" * 80 + "callback"
47
+ p access_token.inspect
48
+ @user = client.users.show_by_uid(session[:uid].to_i)
49
+ redirect '/'
50
+ end
51
+
52
+ get '/logout' do
53
+ reset_session
54
+ redirect '/'
55
+ end
56
+
57
+ get '/screen.css' do
58
+ content_type 'text/css'
59
+ sass :screen
60
+ end
61
+
62
+ post '/update' do
63
+ client = Weibo::Client.new
64
+ client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})
65
+ statuses = client.statuses
66
+
67
+ unless params[:file] && (tmpfile = params[:file][:tempfile]) && (name = params[:file][:filename])
68
+ statuses.update(params[:status])
69
+ else
70
+ status = params[:status] || '图片'
71
+ pic = File.open(tmpfile.path)
72
+ statuses.upload(status, pic)
73
+ end
74
+
75
+ redirect '/'
76
+ end
77
+
78
+ helpers do
79
+ def reset_session
80
+ session[:uid] = nil
81
+ session[:access_token] = nil
82
+ session[:expires_at] = nil
83
+ end
84
+ end
@@ -0,0 +1,84 @@
1
+ # encoding: utf-8
2
+
3
+ require 'weibo_focus'
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
+
10
+ Weibo::Config.api_key = "1713173982"
11
+ Weibo::Config.api_secret = "62170dacd0fa1c70d4eb1263a0855744"
12
+ Weibo::Config.redirect_uri = "http://192.168.184.130:4567/callback"
13
+
14
+ get '/' do
15
+ client = Weibo::Client.new
16
+ if session[:access_token] && !client.authorized?
17
+ token = client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})
18
+ p "*" * 80 + "validated"
19
+ p token.inspect
20
+ p token.validated?
21
+
22
+ unless token.validated?
23
+ reset_session
24
+ redirect '/connect'
25
+ return
26
+ end
27
+ end
28
+ if session[:uid]
29
+ @user = client.users.show_by_uid(session[:uid])
30
+ @statuses = client.statuses
31
+ end
32
+ haml :index
33
+ end
34
+
35
+ get '/connect' do
36
+ client = Weibo::Client.new
37
+ redirect client.authorize_url
38
+ end
39
+
40
+ get '/callback' do
41
+ client = Weibo::Client.new
42
+ access_token = client.auth_code.get_token(params[:code].to_s)
43
+ session[:uid] = access_token.params["uid"]
44
+ session[:access_token] = access_token.token
45
+ session[:expires_at] = access_token.expires_at
46
+ p "*" * 80 + "callback"
47
+ p access_token.inspect
48
+ @user = client.users.show_by_uid(session[:uid].to_i)
49
+ redirect '/'
50
+ end
51
+
52
+ get '/logout' do
53
+ reset_session
54
+ redirect '/'
55
+ end
56
+
57
+ get '/screen.css' do
58
+ content_type 'text/css'
59
+ sass :screen
60
+ end
61
+
62
+ post '/update' do
63
+ client = Weibo::Client.new
64
+ client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})
65
+ statuses = client.statuses
66
+
67
+ unless params[:file] && (tmpfile = params[:file][:tempfile]) && (name = params[:file][:filename])
68
+ statuses.update(params[:status])
69
+ else
70
+ status = params[:status] || '图片'
71
+ pic = File.open(tmpfile.path)
72
+ statuses.upload(status, pic)
73
+ end
74
+
75
+ redirect '/'
76
+ end
77
+
78
+ helpers do
79
+ def reset_session
80
+ session[:uid] = nil
81
+ session[:access_token] = nil
82
+ session[:expires_at] = nil
83
+ end
84
+ end
@@ -0,0 +1,42 @@
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
+
@@ -0,0 +1,12 @@
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"
@@ -0,0 +1,13 @@
1
+ .statuses
2
+ background-color: #CECECE
3
+ padding: 20px
4
+
5
+ .status
6
+ background-color: white
7
+ margin: 10px
8
+ padding: 20px
9
+ font-size: 12px
10
+
11
+ .time_and_by
12
+ margin-top: 10px
13
+ color: #8F8F8F
data/example/weibo.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "weibo_focus"
2
+
3
+ puts Weibo::VERSION
4
+ Weibo::Config.api_key = "12345678"
5
+ puts Weibo::Config.api_key
6
+
7
+ puts Weibo::INTFOCUS
@@ -0,0 +1,20 @@
1
+ module Weibo
2
+ class AccessToken < OAuth2::AccessToken
3
+
4
+ def validated?
5
+ !!@expires_at && !expired?
6
+ end
7
+
8
+ def expired?
9
+ expires? && (expires_at < 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 time_convertion(time, time_zone)
17
+ time.getutc.getlocal(time_zone)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,31 @@
1
+ module Weibo
2
+ module Api
3
+ module V2
4
+ class Account < Base
5
+
6
+ #read interfaces
7
+ def get_privacy(opt={})
8
+ hashie get("account/get_privacy.json", :params => opt)
9
+ end
10
+
11
+ def profile_school_list(opt={})
12
+ hashie get("account/profile/school_list.json", :params => opt)
13
+ end
14
+
15
+ def rate_limit_status(opt={})
16
+ hashie get("account/rate_limit_status.json", :params => opt)
17
+ end
18
+
19
+ def get_uid(opt={})
20
+ hashie get("account/get_uid.json", :params => opt)
21
+ end
22
+
23
+ #write interfaces
24
+ def end_session(opt={})
25
+ hashie get("account/end_session.json", :params => opt)
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,64 @@
1
+ require 'hashie'
2
+ require 'json'
3
+ require 'net/http/post/multipart'
4
+
5
+ module Weibo
6
+ module Api
7
+ module V2
8
+ class Base
9
+ extend Forwardable
10
+
11
+ def_delegators :@access_token, :get, :post, :put, :delete
12
+
13
+ @@API_VERSION = 2
14
+
15
+ def initialize(access_token)
16
+ @access_token = access_token
17
+ end
18
+
19
+ def hashie(response)
20
+ json_body = JSON.parse(response.body)
21
+ if json_body.is_a? Array
22
+ Array.new(json_body.count){|i| Hashie::Mash.new(json_body[i])}
23
+ else
24
+ Hashie::Mash.new json_body
25
+ end
26
+ end
27
+
28
+ protected
29
+ def self.mime_type(file)
30
+ case
31
+ when file =~ /\.jpg/ then 'image/jpg'
32
+ when file =~ /\.gif$/ then 'image/gif'
33
+ when file =~ /\.png$/ then 'image/png'
34
+ else 'application/octet-stream'
35
+ end
36
+ end
37
+
38
+ CRLF = "\r\n"
39
+ def self.build_multipart_bodies(parts)
40
+ boundary = Time.now.to_i.to_s(16)
41
+ body = ""
42
+ parts.each do |key, value|
43
+ esc_key = CGI.escape(key.to_s)
44
+ body << "--#{boundary}#{CRLF}"
45
+ if value.respond_to?(:read)
46
+ body << "Content-Disposition: form-data; name=\"#{esc_key}\"; filename=\"#{File.basename(value.path)}\"#{CRLF}"
47
+ body << "Content-Type: #{mime_type(value.path)}#{CRLF*2}"
48
+ body << value.read
49
+ else
50
+ body << "Content-Disposition: form-data; name=\"#{esc_key}\"#{CRLF*2}#{value}"
51
+ end
52
+ body << CRLF
53
+ end
54
+ body << "--#{boundary}--#{CRLF*2}"
55
+ {
56
+ :body => body,
57
+ :headers => {"Content-Type" => "multipart/form-data; boundary=#{boundary}"}
58
+ }
59
+ end
60
+
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,51 @@
1
+ module Weibo
2
+ module Api
3
+ module V2
4
+ class Comments < Base
5
+
6
+ #read interfaces
7
+ def show(id, opt={})
8
+ hashie get("comments/show.json", :params => {:id => id}.merge(opt))
9
+ end
10
+
11
+ def by_me(opt={})
12
+ hashie get("comments/by_me.json", :params => opt)
13
+ end
14
+
15
+ def to_me(opt={})
16
+ hashie get("comments/to_me.json", :params => opt)
17
+ end
18
+
19
+ def timeline(opt={})
20
+ hashie get("comments/timeline.json", :params => opt)
21
+ end
22
+
23
+ def mentions(opt={})
24
+ hashie get("comments/mentions.json", :params => opt)
25
+ end
26
+
27
+ def show_batch(cids, opt={})
28
+ hashie get("comments/show_batch.json", :params => {:cids => cids}.merge(opt))
29
+ end
30
+
31
+ #write interfaces
32
+ def create(comment, id, opt={})
33
+ hashie post("comments/create.json", :params => {:comment => comment, :id => id}.merge(opt))
34
+ end
35
+
36
+ def destroy(cid, opt={})
37
+ hashie post("comments/destroy.json", :params => {:cid => cid}.merge(opt))
38
+ end
39
+
40
+ def destroy_batch(cids, opt={})
41
+ hashie post("comments/destroy_batch.json", :params => {:cids => cids}.merge(opt))
42
+ end
43
+
44
+ def reply(cid, id, comment, opt={})
45
+ hashie post("comments/reply.json", :params => {:cid => cid, :id => id, :comment => comment}.merge(opt))
46
+ end
47
+
48
+ end
49
+ end
50
+ end
51
+ end