tida_sina_weibo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tida_sina_weibo.gemspec
4
+ gemspec
5
+
6
+ gem 'settingslogic'
7
+ gem 'rest-client'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 babaru
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # TidaSinaWeibo
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'tida_sina_weibo'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tida_sina_weibo
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ module TidaSinaWeibo
2
+
3
+ class ApiSettings < Settingslogic
4
+ source "#{Rails.root}/config/application.yml"
5
+ namespace Rails.env
6
+ end
7
+
8
+ end
@@ -0,0 +1,32 @@
1
+ defaults: &defaults
2
+ api:
3
+ status:
4
+ update: 'https://api.weibo.com/2/statuses/update.json'
5
+ upload: 'https://upload.api.weibo.com/2/statuses/upload.json'
6
+ user_timeline: 'https://api.weibo.com/2/statuses/user_timeline.json'
7
+ user_timeline_ids: 'https://api.weibo.com/2/statuses/user_timeline/ids.json'
8
+ show: 'https://api.weibo.com/2/statuses/show.json'
9
+ repost_timeline: 'https://api.weibo.com/2/statuses/repost_timeline.json'
10
+ repost_timeline_ids: 'https://api.weibo.com/2/statuses/repost_timeline/ids.json'
11
+ queryid: 'https://api.weibo.com/2/statuses/queryid.json'
12
+ querymid: 'https://api.weibo.com/2/statuses/querymid.json'
13
+ comments:
14
+ show: 'https://api.weibo.com/2/comments/show.json'
15
+ user:
16
+ show: 'https://api.weibo.com/2/users/show.json'
17
+ domain_show: 'https://api.weibo.com/2/users/domain_show.json'
18
+ tags: 'https://api.weibo.com/2/tags.json'
19
+ friendships:
20
+ followers: 'https://api.weibo.com/2/friendships/followers.json'
21
+ followers_ids: 'https://api.weibo.com/2/friendships/followers/ids.json'
22
+ show: 'https://api.weibo.com/2/friendships/show.json'
23
+
24
+
25
+ development:
26
+ <<: *defaults
27
+
28
+ test:
29
+ <<: *defaults
30
+
31
+ production:
32
+ <<: *defaults
@@ -0,0 +1,163 @@
1
+ require "rest-client"
2
+ require "api_settings"
3
+
4
+ module TidaSinaWeibo
5
+ class ApiWrapper
6
+ attr_reader :access_token
7
+ attr_reader :token_provider
8
+ attr_reader :request_data
9
+
10
+ def initialize(token_prvdr)
11
+ @token_provider = token_prvdr
12
+ @access_token = @token_provider.token
13
+ end
14
+
15
+ def status_update(content)
16
+ post_request(Settings.weibo.api.status.update, :status => content)
17
+ end
18
+
19
+ def status_upload(content, image_path)
20
+ post_request(Settings.weibo.api.status.upload, :status => content, :pic => File.new(image_path, 'rb'))
21
+ end
22
+
23
+ def status_user_timeline(uid, page = 1, count = 100)
24
+ get_request(Settings.weibo.api.status.user_timeline, :params => {:uid => uid, :page => page, :count => count})
25
+ end
26
+
27
+ def status_repost_timeline(post_id, page = 1)
28
+ get_request(Settings.weibo.api.status.repost_timeline, :params => {:id => post_id, :page => page, :count => 50})
29
+ end
30
+
31
+ def comments_show(post_id, page = 1)
32
+ get_request(Settings.weibo.api.comments.show, :params => {:id => post_id, :page => page, :count => 50})
33
+ end
34
+
35
+ def status_show(wb_post_id)
36
+ get_request(Settings.weibo.api.status.show, :params => {:id => wb_post_id})
37
+ end
38
+
39
+ def user_show(uid, is_number_id = true)
40
+ if is_number_id
41
+ get_request(Settings.weibo.api.user.show, {:params => {:uid => uid}})
42
+ else
43
+ get_request(Settings.weibo.api.user.show, {:params => {:screen_name => uid}})
44
+ end
45
+ end
46
+
47
+ def user_domain_show(domain)
48
+ get_request(Settings.weibo.api.user.domain_show, :params => {:domain => domain})
49
+ end
50
+
51
+ def friendships_followers(uid, cursor)
52
+ get_request(Settings.weibo.api.friendships.followers, :params => {:uid => uid, :cursor => cursor})
53
+ end
54
+
55
+ def friendships_followers_ids(uid, cursor)
56
+ get_request(Settings.weibo.api.friendships.followers_ids, :params => {:uid => uid, :cursor => cursor})
57
+ end
58
+
59
+ def tags(uid)
60
+ get_request(Settings.weibo.api.tags, :params => {:uid => uid})
61
+ end
62
+
63
+ def queryid(mid)
64
+ data = get_request(Settings.weibo.api.status.queryid, {:params => {:mid => mid, :type => 1, :isBase62 => 1}})
65
+ end
66
+
67
+ def querymid(id, batch = false)
68
+ data = get_request(Settings.weibo.api.status.querymid, {:params => {:id => id, :type => 1, :is_batch => batch ? 1 : 0}})
69
+ end
70
+
71
+ def querymids(ids)
72
+ ids_str = ids.join(',')
73
+ querymid(ids_str, true)
74
+ end
75
+
76
+ private
77
+
78
+ def post_request(url, data)
79
+ rest_request(url, data, true)
80
+ end
81
+
82
+ def get_request(url, data)
83
+ r = rest_request(url, data, false)
84
+ return r
85
+ end
86
+
87
+ def rest_request(url, data, method_post)
88
+ result, code = do_request url, data, method_post
89
+
90
+ while code == -1
91
+ change_access_token
92
+ result, code = do_request url, data, method_post
93
+ end
94
+
95
+ return result
96
+ end
97
+
98
+ def do_request(url, data, method_post)
99
+ rd = data.clone
100
+ params = {:access_token => @access_token}
101
+ rd[:params] = data[:params].merge(params)
102
+ Rails.logger.debug "Request params hash is #{rd}"
103
+
104
+ parse_response do
105
+ if method_post
106
+ r = JSON.parse RestClient.post(url, rd)
107
+ Rails.logger.debug "- Request response: #{r}"
108
+ return r, 0
109
+ else
110
+ r = JSON.parse RestClient.get(url, rd)
111
+ Rails.logger.debug "- Request response: #{r}"
112
+ return r, 0
113
+ end
114
+ end
115
+ rescue RestClient::Exception => e
116
+ Rails.logger.error "! Request Failed #{e.to_s}"
117
+ r = JSON.parse e.http_body
118
+ Rails.logger.error "! Sina Weibo API Error Code: #{r["error_code"]} Error: #{r["error"]}"
119
+ api_error_code = r["error_code"].to_i
120
+
121
+ if invalid_access_token? api_error_code
122
+ return nil, -1
123
+ end
124
+
125
+ if reached_account_access_limit? api_error_code
126
+ return nil, -1
127
+ end
128
+
129
+ if reached_ip_access_limit? api_error_code
130
+ raise IPAccessLimitException.new
131
+ end
132
+
133
+ return nil, -2
134
+ end
135
+
136
+ def parse_response(&block)
137
+ JSON.parse(block.call)
138
+ end
139
+
140
+ def invalid_access_token?(error_code)
141
+ return error_code == 21332 || error_code == 21327
142
+ end
143
+
144
+ def reached_account_access_limit?(error_code)
145
+ return error_code == 10023 || error_code == 10024
146
+ end
147
+
148
+ def reached_ip_access_limit?(error_code)
149
+ return error_code == 10022
150
+ end
151
+
152
+ def change_access_token
153
+ Rails.logger.info "* Changing access token}"
154
+ Rails.logger.info "- Old access token is #{@access_token}"
155
+ self.token_provider.change
156
+ @access_token = self.token_provider.token
157
+ Rails.logger.info "- New access token is #{@access_token}"
158
+ if @access_token.nil?
159
+ raise NoAccessTokenAvaiableException.new
160
+ end
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,3 @@
1
+ module TidaSinaWeibo
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ require "tida_sina_weibo/version"
2
+ require 'tida_sina_weibo/api_wrapper'
3
+
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tida_sina_weibo/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "tida_sina_weibo"
8
+ gem.version = TidaSinaWeibo::VERSION
9
+ gem.authors = ["babaru"]
10
+ gem.email = ["babaru.trinit@gmail.com"]
11
+ gem.description = %q{SinaWeibo API gem}
12
+ gem.summary = %q{SinaWeibo API gem}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tida_sina_weibo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - babaru
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-08 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: SinaWeibo API gem
15
+ email:
16
+ - babaru.trinit@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/tida_sina_weibo.rb
27
+ - lib/tida_sina_weibo/api_settings.rb
28
+ - lib/tida_sina_weibo/api_settings.yml
29
+ - lib/tida_sina_weibo/api_wrapper.rb
30
+ - lib/tida_sina_weibo/version.rb
31
+ - tida_sina_weibo.gemspec
32
+ homepage: ''
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.24
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: SinaWeibo API gem
56
+ test_files: []