curt-wren 0.0.0
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 +143 -0
- data/lib/wren.rb +151 -0
- metadata +63 -0
data/README.rdoc
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
= Wren
|
2
|
+
|
3
|
+
A very lightweight Ruby wrapper for the Twitter REST API
|
4
|
+
|
5
|
+
== Author
|
6
|
+
|
7
|
+
Curt Gilman, Reductive Reason LLC
|
8
|
+
|
9
|
+
curt.gilman [at] reductivereason.com
|
10
|
+
|
11
|
+
- http://twitter.com/curtg72 - Twitter ego
|
12
|
+
- http://reductivereason.com - Corporate identity
|
13
|
+
- http://pearlsofnonsense.com - Personal soapbox
|
14
|
+
- http://w8track.com - Online weight tracking
|
15
|
+
|
16
|
+
== Description
|
17
|
+
|
18
|
+
Wren is a very lightweight Ruby wrapper for the Twitter REST API.
|
19
|
+
It implements only basic authentication and JSON parsing. SSL is
|
20
|
+
used automatically whenever credentials are sent and for all post
|
21
|
+
requests. OAuth authentication, search API, and XML parsing are
|
22
|
+
not implemented. PUT and DELETE methods are also not implemented.
|
23
|
+
|
24
|
+
== Installation
|
25
|
+
|
26
|
+
Add GitHub to your gem sources, if you haven't already:
|
27
|
+
gem sources -a http://gems.github.com
|
28
|
+
|
29
|
+
Install the gem.
|
30
|
+
sudo gem install curt-wren
|
31
|
+
|
32
|
+
== Usage
|
33
|
+
|
34
|
+
The first thing you'll need to do is require Wren.
|
35
|
+
require 'wren'
|
36
|
+
|
37
|
+
=== Configuration
|
38
|
+
|
39
|
+
Before you make any Twitter API calls, you'll want to set up
|
40
|
+
the configuration.
|
41
|
+
|
42
|
+
==== Using no authentication
|
43
|
+
|
44
|
+
If you're not using any parts of the Twitter API requiring
|
45
|
+
authentication, you can use the default configuration. Congratulations,
|
46
|
+
you're done!
|
47
|
+
|
48
|
+
==== Using basic authentication
|
49
|
+
|
50
|
+
The simplest configuration for basic authentication is just your
|
51
|
+
Twitter username and password.
|
52
|
+
config = Wren::Configuration.new do |c|
|
53
|
+
c.user = 't123'
|
54
|
+
c.pwd = 'fido' # <= not recommended
|
55
|
+
end
|
56
|
+
|
57
|
+
You could also do this as a one-liner.
|
58
|
+
|
59
|
+
config = Wren::Configuration(:user => 't123', :pwd => 'fido')
|
60
|
+
|
61
|
+
==== Other settings
|
62
|
+
|
63
|
+
If you've created a configuration, you can change the logger, which
|
64
|
+
is STDOUT by default.
|
65
|
+
config.logger = my_logger
|
66
|
+
|
67
|
+
=== Calling the API
|
68
|
+
|
69
|
+
You'll first need to "new" up an instance of Client.
|
70
|
+
client = Wren::Client.new(config)
|
71
|
+
|
72
|
+
If you call the initializer with a hash, it'll create a Configuration
|
73
|
+
object for you.
|
74
|
+
client = Wren::Client.new(:user => 't123', :pwd => 'fido')
|
75
|
+
|
76
|
+
If you're just using the default configuration, that's fine too.
|
77
|
+
client = Wren::Client.new
|
78
|
+
|
79
|
+
==== GET requests
|
80
|
+
|
81
|
+
To call the GET method on a resource, simply chain method calls together
|
82
|
+
like they were parts of the URI. The request will replace the periods
|
83
|
+
with slashes. At the end of the chain, call the "get" method.
|
84
|
+
client.account.rate_limit_status.get
|
85
|
+
# => returns a Hash object
|
86
|
+
|
87
|
+
If your request requires authentication, call the "auth" method as part of
|
88
|
+
the chain.
|
89
|
+
client.direct_messages.auth.get
|
90
|
+
# => returns an Array object
|
91
|
+
|
92
|
+
If your request requires query parameters, pass them in as a hash using the
|
93
|
+
"options" method as part of the query chain.
|
94
|
+
client.friends.ids.options(:screen_name => 't123').get
|
95
|
+
# => returns an Array object
|
96
|
+
|
97
|
+
And, of course, you can use both "auth" and "options" methods.
|
98
|
+
client.favorites.auth.options(:id => 't123', :page => 3)
|
99
|
+
# => returns an Array object
|
100
|
+
|
101
|
+
==== POST requests
|
102
|
+
|
103
|
+
Calling a POST method on a resource behaves much like a GET resource, except
|
104
|
+
that you call the "post" method. The "post" method takes a hash of data to
|
105
|
+
send. Authentication is assumed, so you don't need to call the "auth" method.
|
106
|
+
client.statuses.update.post(:status => 'Is this thing on?')
|
107
|
+
# => returns a Hash object
|
108
|
+
|
109
|
+
Some POST methods require query parameters, and that's fine.
|
110
|
+
client.friendships.destroy.options(:screen_name => 't123').post
|
111
|
+
# => returns a Hash object
|
112
|
+
|
113
|
+
==== One-liner?
|
114
|
+
|
115
|
+
It's certainly possible to combine much of this into a one-liner, if you
|
116
|
+
have a screen that's wide enough.
|
117
|
+
Wren::Client.new(:user => 't123', :pwd => 'fido').statuses.update.post(:status => 'Wow!')
|
118
|
+
# => returns a Hash object
|
119
|
+
|
120
|
+
== License
|
121
|
+
|
122
|
+
(MIT License)
|
123
|
+
|
124
|
+
Copyright (c) 2009 Reductive Reason LLC
|
125
|
+
|
126
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
127
|
+
a copy of this software and associated documentation files (the
|
128
|
+
'Software'), to deal in the Software without restriction, including
|
129
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
130
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
131
|
+
permit persons to whom the Software is furnished to do so, subject to
|
132
|
+
the following conditions:
|
133
|
+
|
134
|
+
The above copyright notice and this permission notice shall be
|
135
|
+
included in all copies or substantial portions of the Software.
|
136
|
+
|
137
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
138
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
139
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
140
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
141
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
142
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
143
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/wren.rb
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'logger'
|
3
|
+
require 'rubygems'
|
4
|
+
require 'net/http'
|
5
|
+
require 'net/https'
|
6
|
+
require 'json'
|
7
|
+
require 'cgi'
|
8
|
+
|
9
|
+
module Wren
|
10
|
+
class HTTP < Net::HTTP
|
11
|
+
alias_method :old_wren_initialize, :initialize
|
12
|
+
|
13
|
+
def initialize(*args)
|
14
|
+
old_wren_initialize(*args)
|
15
|
+
@ssl_context = OpenSSL::SSL::SSLContext.new
|
16
|
+
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Configuration
|
21
|
+
attr_accessor :user, :pwd, :logger, :transport
|
22
|
+
|
23
|
+
def initialize(params = nil)
|
24
|
+
if params.is_a?(Hash)
|
25
|
+
params.each{ |k, v| self.send("#{k.to_sym}=", v) }
|
26
|
+
end
|
27
|
+
|
28
|
+
@logger = Logger.new(STDOUT)
|
29
|
+
@logger.level = Logger::DEBUG
|
30
|
+
@transport = Wren::HTTP
|
31
|
+
|
32
|
+
yield self if block_given?
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class TwitterError < StandardError
|
37
|
+
attr_accessor :method, :request_uri, :status, :response_body, :response_object
|
38
|
+
|
39
|
+
def initialize(method, request_uri, status, response_body, msg = nil)
|
40
|
+
self.method = method
|
41
|
+
self.request_uri = request_uri
|
42
|
+
self.status = status
|
43
|
+
self.response_body = response_body
|
44
|
+
super(msg || "#{self.method} #{self.request_uri} => #{self.status}: #{self.response_body}")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class Client
|
49
|
+
attr_accessor :config
|
50
|
+
|
51
|
+
def initialize(arg = Configuration.new)
|
52
|
+
if arg.is_a?(Hash)
|
53
|
+
@config = Configuration.new
|
54
|
+
arg.each{ |k, v| @config.send("#{k.to_sym}=", v) }
|
55
|
+
elsif arg.is_a?(Configuration)
|
56
|
+
@config = arg
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def method_missing(name, *args)
|
63
|
+
req = Request.new(self)
|
64
|
+
req.send(name, *args)
|
65
|
+
req
|
66
|
+
end
|
67
|
+
|
68
|
+
class Request
|
69
|
+
TWITTER_ROOT_URL = "http://twitter.com"
|
70
|
+
|
71
|
+
def initialize(client)
|
72
|
+
@client = client
|
73
|
+
@auth = false
|
74
|
+
@options = {}
|
75
|
+
@data = {}
|
76
|
+
@path = ''
|
77
|
+
end
|
78
|
+
|
79
|
+
def auth
|
80
|
+
@auth = true
|
81
|
+
self
|
82
|
+
end
|
83
|
+
|
84
|
+
def options(params)
|
85
|
+
@options.merge!(params)
|
86
|
+
self
|
87
|
+
end
|
88
|
+
|
89
|
+
def get
|
90
|
+
get_json
|
91
|
+
end
|
92
|
+
|
93
|
+
def post(params = nil)
|
94
|
+
if params.is_a?(Hash)
|
95
|
+
@data.merge!(params)
|
96
|
+
end
|
97
|
+
post_json
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
def method_missing(name, *args)
|
103
|
+
@path << "/#{name}"
|
104
|
+
self
|
105
|
+
end
|
106
|
+
|
107
|
+
def url_with_options
|
108
|
+
"#{TWITTER_ROOT_URL}#{@path}.json#{query_string}"
|
109
|
+
end
|
110
|
+
|
111
|
+
def query_string
|
112
|
+
@options.empty? ? '' : '?' + @options.collect{ |k, v| "#{CGI::escape(k.to_s)}=#{CGI::escape(v.to_s)}" }.join('&')
|
113
|
+
end
|
114
|
+
|
115
|
+
def get_json
|
116
|
+
request_json(Net::HTTP::Get)
|
117
|
+
end
|
118
|
+
|
119
|
+
def post_json
|
120
|
+
@auth = true
|
121
|
+
request_json(Net::HTTP::Post)
|
122
|
+
end
|
123
|
+
|
124
|
+
def request_json(method)
|
125
|
+
begin
|
126
|
+
url = URI.parse(url_with_options)
|
127
|
+
method_name = method.class_eval('METHOD').downcase
|
128
|
+
url.scheme = @auth ? 'https' : 'http'
|
129
|
+
req = method.new(url.request_uri)
|
130
|
+
if @auth; req.basic_auth(@client.config.user, @client.config.pwd); end
|
131
|
+
if @data; req.set_form_data(@data); end
|
132
|
+
http = @client.config.transport.new(url.host, url.scheme == 'https' ? 443 : 80)
|
133
|
+
http.use_ssl = (url.scheme == 'https')
|
134
|
+
res = http.request(req)
|
135
|
+
case res
|
136
|
+
when Net::HTTPSuccess
|
137
|
+
JSON.parse(res.body)
|
138
|
+
else
|
139
|
+
raise TwitterError.new(method_name, url.request_uri, res.status, res.body)
|
140
|
+
end
|
141
|
+
rescue TwitterError => e
|
142
|
+
raise e
|
143
|
+
rescue JSON::ParserError => e
|
144
|
+
raise TwitterError.new(method_name, url.request_uri, res.status, res.body, "Unable to parse JSON: #{e}")
|
145
|
+
rescue Timeout::Error
|
146
|
+
raise "Timeout while #{method_name}ing to #{url.to_s}"
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: curt-wren
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Curt Gilman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-23 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: json
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.1.6
|
24
|
+
version:
|
25
|
+
description: Wren is a very lightweight Ruby wrapper for the Twitter REST API.
|
26
|
+
email: curt.gilman@reductivereason.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.rdoc
|
33
|
+
files:
|
34
|
+
- lib/wren.rb
|
35
|
+
- README.rdoc
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/curt/wren
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options:
|
40
|
+
- --charset=UTF-8
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.2.0
|
59
|
+
signing_key:
|
60
|
+
specification_version: 2
|
61
|
+
summary: Wren is a very lightweight Ruby wrapper for the Twitter REST API.
|
62
|
+
test_files: []
|
63
|
+
|