hot-coffee-twitter-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
File without changes
data/lib/twitter.rb ADDED
@@ -0,0 +1 @@
1
+ require 'twitter/rest_api'
@@ -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))
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,83 @@
1
+ require 'net/https'
2
+ require 'json'
3
+ require 'twitter/common'
4
+
5
+ module Twitter
6
+ VERSION = '0.1.0'
7
+ class RESTClient
8
+ API_ADDRESS = 'twitter.com'
9
+
10
+ def initialize(options = {})
11
+ @login = options[:login]
12
+ @password = options[:password]
13
+
14
+ @port = options[:port] || 80
15
+ @ssl_port = options[:ssl_port] || 443
16
+ @proxy_addr = options[:proxy_addr]
17
+ @proxy_port = options[:proxy_port]
18
+ @proxy_user = options[:proxy_user]
19
+ @proxy_pass = options[:proxy_pass]
20
+
21
+ @http = Net::HTTP.new(API_ADDRESS, @port,
22
+ @proxy_addr, @proxy_port, @proxy_user, @proxy_pass)
23
+ @https = Net::HTTP.new(API_ADDRESS, @ssl_port,
24
+ @proxy_addr, @proxy_port, @proxy_user, @proxy_pass)
25
+ @https.use_ssl = true
26
+ @https.verify_mode = OpenSSL::SSL::VERIFY_NONE
27
+
28
+ @header = {'User-Agent' => "twitter-ruby/#{Twitter::VERSION}"}
29
+ @header.merge! options[:header] if options[:header]
30
+ end
31
+
32
+ def get(path, params = {}, require_auth = true)
33
+ path << '.json'
34
+
35
+ params_str = Util.to_params_str(params)
36
+ path << "?#{params_str}" if params_str
37
+
38
+ req = Net::HTTP::Get.new(path, @header)
39
+ req.basic_auth @login, @password if require_auth
40
+
41
+ http_request(req, require_auth)
42
+ end
43
+
44
+ def post(path, params = {}, require_auth = true)
45
+ path << '.json'
46
+
47
+ req = Net::HTTP::Post.new(path, @header)
48
+ req.body = Util.to_params_str(params)
49
+ req.basic_auth @login, @password if require_auth
50
+
51
+ http_request(req, require_auth)
52
+ end
53
+
54
+ def delete(path, params = {}, require_auth = true)
55
+ path << '.json'
56
+
57
+ params_str = Util.to_params_str(params)
58
+ path << "?#{params_str}" if params_str
59
+
60
+ req = Net::HTTP::Post.new(path, @header)
61
+ req.basic_auth @login, @password if require_auth
62
+
63
+ http_request(req, require_auth)
64
+ end
65
+
66
+ private
67
+ def http_request(req, require_auth)
68
+ if require_auth
69
+ res = @https.request(req)
70
+ else
71
+ res = @http.request(req)
72
+ end
73
+ data = JSON.parse(res.body)
74
+
75
+ case res.code
76
+ when '200'
77
+ Util.unescapeHTML(data)
78
+ else
79
+ raise Twitter::ResponseError.new(data['error'], res)
80
+ end
81
+ end
82
+ end
83
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hot-coffee-twitter-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
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: twitter-ruby 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/twitter-ruby
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
55
+ test_files: []
56
+