hot-coffee-ruby-twitter 0.1.5
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.rdoc +3 -0
- data/lib/twitter/common.rb +30 -0
- data/lib/twitter/rest_api.rb +92 -0
- data/lib/twitter.rb +5 -0
- metadata +56 -0
data/README.rdoc
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require 'cgi'
|
|
2
|
+
|
|
3
|
+
module Twitter
|
|
4
|
+
class Util
|
|
5
|
+
def self.to_params_str(params)
|
|
6
|
+
return nil if params.empty?
|
|
7
|
+
params.each do |key, value|
|
|
8
|
+
params[key] = CGI.escape(CGI.escapeHTML(value.to_s))
|
|
9
|
+
end
|
|
10
|
+
params.to_a.map {|v| v.join('=')}.join('&')
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.unescapeHTML(statuses)
|
|
14
|
+
statuses.each_with_index do |status, index|
|
|
15
|
+
statuses[index]['text'] = CGI.unescapeHTML(status['text'])
|
|
16
|
+
end
|
|
17
|
+
statuses
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
module Twitter
|
|
23
|
+
class ResponseError < StandardError
|
|
24
|
+
attr_reader :response
|
|
25
|
+
def initialize(msg, res)
|
|
26
|
+
super(msg)
|
|
27
|
+
@response = res
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
require 'net/https'
|
|
2
|
+
require 'json'
|
|
3
|
+
require 'twitter/common'
|
|
4
|
+
|
|
5
|
+
module Twitter
|
|
6
|
+
class RESTClient
|
|
7
|
+
API_ADDRESS = 'twitter.com'
|
|
8
|
+
|
|
9
|
+
def initialize(options = {})
|
|
10
|
+
@login = options[:login]
|
|
11
|
+
@password = options[:password]
|
|
12
|
+
|
|
13
|
+
@port = options[:port] || 80
|
|
14
|
+
@ssl_port = options[:ssl_port] || 443
|
|
15
|
+
@proxy_addr = options[:proxy_addr]
|
|
16
|
+
@proxy_port = options[:proxy_port]
|
|
17
|
+
@proxy_user = options[:proxy_user]
|
|
18
|
+
@proxy_pass = options[:proxy_pass]
|
|
19
|
+
|
|
20
|
+
@header = {'User-Agent' => "twitter-ruby/#{Twitter::VERSION}"}
|
|
21
|
+
@header.merge! options[:header] if options[:header]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def get(path, params = {}, require_auth = true)
|
|
25
|
+
path += '.json'
|
|
26
|
+
|
|
27
|
+
params_str = Util.to_params_str(params)
|
|
28
|
+
path += "?#{params_str}" if params_str
|
|
29
|
+
|
|
30
|
+
req = Net::HTTP::Get.new(path, @header)
|
|
31
|
+
req.basic_auth @login, @password if require_auth
|
|
32
|
+
|
|
33
|
+
http_request(req, require_auth)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def post(path, params = {}, require_auth = true)
|
|
37
|
+
path += '.json'
|
|
38
|
+
|
|
39
|
+
req = Net::HTTP::Post.new(path, @header)
|
|
40
|
+
req.body = Util.to_params_str(params)
|
|
41
|
+
req.basic_auth @login, @password if require_auth
|
|
42
|
+
|
|
43
|
+
http_request(req, require_auth)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def delete(path, params = {}, require_auth = true)
|
|
47
|
+
path += '.json'
|
|
48
|
+
|
|
49
|
+
params_str = Util.to_params_str(params)
|
|
50
|
+
path += "?#{params_str}" if params_str
|
|
51
|
+
|
|
52
|
+
req = Net::HTTP::Post.new(path, @header)
|
|
53
|
+
req.basic_auth @login, @password if require_auth
|
|
54
|
+
|
|
55
|
+
http_request(req, require_auth)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
def http_request(req, require_auth)
|
|
60
|
+
if require_auth
|
|
61
|
+
res = https.request(req)
|
|
62
|
+
else
|
|
63
|
+
res = http.request(req)
|
|
64
|
+
end
|
|
65
|
+
data = JSON.parse(res.body)
|
|
66
|
+
|
|
67
|
+
case res.code
|
|
68
|
+
when '200'
|
|
69
|
+
Util.unescapeHTML(data)
|
|
70
|
+
else
|
|
71
|
+
raise Twitter::ResponseError.new(data['error'], res)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def http
|
|
76
|
+
Net::HTTP.new(
|
|
77
|
+
API_ADDRESS, @port,
|
|
78
|
+
@proxy_addr, @proxy_port, @proxy_user, @proxy_pass
|
|
79
|
+
)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def https
|
|
83
|
+
https = Net::HTTP.new(
|
|
84
|
+
API_ADDRESS, @ssl_port,
|
|
85
|
+
@proxy_addr, @proxy_port, @proxy_user, @proxy_pass
|
|
86
|
+
)
|
|
87
|
+
https.use_ssl = true
|
|
88
|
+
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
89
|
+
https
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
data/lib/twitter.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: hot-coffee-ruby-twitter
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.5
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- hot_coffee
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-05-16 00:00:00 -07:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description: ruby-twitter is base of using Twitter API
|
|
17
|
+
email: coffee242@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files:
|
|
23
|
+
- README.rdoc
|
|
24
|
+
files:
|
|
25
|
+
- lib/twitter.rb
|
|
26
|
+
- lib/twitter/common.rb
|
|
27
|
+
- lib/twitter/rest_api.rb
|
|
28
|
+
- README.rdoc
|
|
29
|
+
has_rdoc: true
|
|
30
|
+
homepage: http://github.com/hot-coffee/ruby-twitter
|
|
31
|
+
post_install_message:
|
|
32
|
+
rdoc_options: []
|
|
33
|
+
|
|
34
|
+
require_paths:
|
|
35
|
+
- lib
|
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: "0"
|
|
41
|
+
version:
|
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: "0"
|
|
47
|
+
version:
|
|
48
|
+
requirements: []
|
|
49
|
+
|
|
50
|
+
rubyforge_project:
|
|
51
|
+
rubygems_version: 1.2.0
|
|
52
|
+
signing_key:
|
|
53
|
+
specification_version: 2
|
|
54
|
+
summary: Twitter client library
|
|
55
|
+
test_files: []
|
|
56
|
+
|