hayesdavis-grackle 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +24 -19
- data/grackle.gemspec +1 -1
- data/lib/grackle.rb +1 -1
- data/lib/grackle/client.rb +1 -1
- data/lib/grackle/transport.rb +3 -1
- data/test/test_client.rb +4 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
=grackle
|
2
2
|
by Hayes Davis
|
3
|
+
- http://twitter.com/hayesdavis
|
3
4
|
- hayes [at] appozite.com
|
4
5
|
- http://cheaptweet.com
|
5
6
|
- http://www.appozite.com
|
@@ -25,14 +26,14 @@ Before you do anything else, you'll need to
|
|
25
26
|
|
26
27
|
===Creating a Grackle::Client
|
27
28
|
====Using Basic Auth
|
28
|
-
client = Grackle::Client.new(:auth=>{:type
|
29
|
+
client = Grackle::Client.new(:auth=>{:type=>:basic,:username=>'your_user',:password=>'yourpass'})
|
29
30
|
|
30
31
|
====Using OAuth
|
31
32
|
client = Grackle::Client.new(:auth=>{
|
32
33
|
:type=>:oauth,
|
33
34
|
:consumer_key=>'SOMECONSUMERKEYFROMTWITTER', :consumer_token=>'SOMECONSUMERTOKENFROMTWITTER',
|
34
35
|
:token=>'ACCESSTOKENACQUIREDONUSERSBEHALF', :token_secret=>'SUPERSECRETACCESSTOKENSECRET'
|
35
|
-
}
|
36
|
+
})
|
36
37
|
|
37
38
|
====Using No Auth
|
38
39
|
client = Grackle::Client.new
|
@@ -51,32 +52,34 @@ request to be execute include:
|
|
51
52
|
- A format method can also include a ? or ! to determine GET or POST in that format respectively
|
52
53
|
|
53
54
|
===GETting Data
|
54
|
-
|
55
|
-
|
55
|
+
The preferred and simplest way of executing a GET is to use the "?" method notation. This will use the default client
|
56
|
+
format (usually JSON, but see Formats section below):
|
57
|
+
client.users.show? :screen_name=>'some_user' #http://twitter.com/users/show.json?screen_name=some_user
|
56
58
|
|
57
|
-
|
58
|
-
client.users.show.
|
59
|
-
|
60
|
-
|
61
|
-
client.users.show.
|
59
|
+
You can force XML format by doing:
|
60
|
+
client.users.show.xml? :screen_name=>'some_user' #http://twitter.com/users/show.xml?scren_name=some_user
|
61
|
+
|
62
|
+
You can force JSON:
|
63
|
+
client.users.show.json? :screen_name=>'some_user' #http://twitter.com/users/show.json?screen_name=some_user
|
62
64
|
|
63
|
-
|
65
|
+
Or, since Twitter also allows certain ids/screen_names to be part of their URLs, this works:
|
64
66
|
client.users.show.some_user? #http://twitter.com/users/show/some_user.json
|
65
67
|
|
66
|
-
If you
|
68
|
+
If you use an explicit format, you can leave off the "?" like so:
|
69
|
+
client.users.show.xml :screen_name=>'some_user' #http://twitter.com/users/show.xml?scren_name=some_user
|
67
70
|
|
68
71
|
===POSTing data
|
69
72
|
To use Twitter API methods that require an HTTP POST, you need to end your method chain with a bang (!)
|
70
73
|
|
71
|
-
|
74
|
+
The preferred way is to use the Client's default format (usually JSON, but see Formats section below):
|
75
|
+
client.statuses.update! :status=>'this status is from grackle' #POST to http://twitter.com/statuses/update.json
|
76
|
+
|
77
|
+
You can force a format. To update the authenticated user's status using the XML format:
|
72
78
|
client.statuses.update.xml! :status=>'this status is from grackle' #POST to http://twitter.com/statuses/update.xml
|
73
79
|
|
74
80
|
Or, with JSON
|
75
81
|
client.statuses.update.json! :status=>'this status is from grackle' #POST to http://twitter.com/statuses/update.json
|
76
82
|
|
77
|
-
Or, using the default format
|
78
|
-
client.statuses.update! :status=>'this status is from grackle' #POST to http://twitter.com/statuses/update.json
|
79
|
-
|
80
83
|
===Toggling APIs
|
81
84
|
By default, the Grackle::Client sends all requests to the Twitter REST API. If you want to send requests to the Twitter Search API, just
|
82
85
|
set Grackle::Client.api to :search. To toggle back, set it to be :rest. All requests made after setting this
|
@@ -84,7 +87,7 @@ attribute will go to that API.
|
|
84
87
|
|
85
88
|
If you want to make a specific request to one API and not change the Client's overall api setting beyond that request, you can use the
|
86
89
|
bracket syntax like so:
|
87
|
-
client[:search].trends.daily? :
|
90
|
+
client[:search].trends.daily? :exclude=>'hashtags'
|
88
91
|
client[:rest].users.show? :id=>'hayesdavis'
|
89
92
|
|
90
93
|
Search and REST requests are all built using the same method chaining and termination conventions.
|
@@ -106,9 +109,11 @@ returned by Twitter. It's a good idea to wrap your API calls with rescue clauses
|
|
106
109
|
If there is an unexpected connection error or Twitter returns data in the wrong format (which it can do), you'll still get a TwitterError.
|
107
110
|
|
108
111
|
===Formats
|
109
|
-
Twitter allows you to request data in particular formats. Grackle
|
110
|
-
|
111
|
-
|
112
|
+
Twitter allows you to request data in particular formats. Grackle automatically parses JSON and XML formatted responses
|
113
|
+
and returns an OpenStruct. If you specify a format that Grackle doesn't parse for you, you'll receive a string containing
|
114
|
+
the raw response body. The Grackle::Client has a default_format you can specify. By default, the default_format is :json.
|
115
|
+
If you don't include a named format in your method chain as described above, but use a "?" or "!" then the
|
116
|
+
Grackle::Client.default_format is used.
|
112
117
|
|
113
118
|
== REQUIREMENTS:
|
114
119
|
|
data/grackle.gemspec
CHANGED
data/lib/grackle.rb
CHANGED
data/lib/grackle/client.rb
CHANGED
@@ -41,7 +41,7 @@ module Grackle
|
|
41
41
|
# :type=>:oauth,
|
42
42
|
# :consumer_key=>'SOMECONSUMERKEYFROMTWITTER, :consumer_token=>'SOMECONSUMERTOKENFROMTWITTER',
|
43
43
|
# :token=>'ACCESSTOKENACQUIREDONUSERSBEHALF', :token_secret=>'SUPERSECRETACCESSTOKENSECRET'
|
44
|
-
# }
|
44
|
+
# })
|
45
45
|
class Client
|
46
46
|
|
47
47
|
class Request #:nodoc:
|
data/lib/grackle/transport.rb
CHANGED
@@ -45,7 +45,9 @@ module Grackle
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def execute_request(method,url,options={})
|
48
|
-
Net::HTTP.new(url.host, url.port)
|
48
|
+
conn = Net::HTTP.new(url.host, url.port)
|
49
|
+
conn.use_ssl = (url.scheme == 'https')
|
50
|
+
conn.start do |http|
|
49
51
|
req = req_class(method).new(url.request_uri)
|
50
52
|
add_headers(req,options[:headers])
|
51
53
|
if file_param?(options[:params])
|
data/test/test_client.rb
CHANGED
@@ -5,10 +5,11 @@ class TestClient < Test::Unit::TestCase
|
|
5
5
|
#Used for mocking HTTP requests
|
6
6
|
class Net::HTTP
|
7
7
|
class << self
|
8
|
-
attr_accessor :response, :request
|
8
|
+
attr_accessor :response, :request, :last_instance
|
9
9
|
end
|
10
10
|
|
11
11
|
def request(req)
|
12
|
+
self.class.last_instance = self
|
12
13
|
self.class.request = req
|
13
14
|
self.class.response
|
14
15
|
end
|
@@ -60,6 +61,7 @@ class TestClient < Test::Unit::TestCase
|
|
60
61
|
value = client.users.show.json? :screen_name=>'test_user'
|
61
62
|
assert_equal(:get,client.transport.method)
|
62
63
|
assert_equal('http',client.transport.url.scheme)
|
64
|
+
assert(!Net::HTTP.last_instance.use_ssl?,'Net::HTTP instance should not be set to use SSL')
|
63
65
|
assert_equal('twitter.com',client.transport.url.host)
|
64
66
|
assert_equal('/users/show.json',client.transport.url.path)
|
65
67
|
assert_equal('test_user',client.transport.options[:params][:screen_name])
|
@@ -89,6 +91,7 @@ class TestClient < Test::Unit::TestCase
|
|
89
91
|
client = new_client(200,'[{"id":1,"text":"test 1"}]',:ssl=>true)
|
90
92
|
client.statuses.public_timeline?
|
91
93
|
assert_equal("https",client.transport.url.scheme)
|
94
|
+
assert(Net::HTTP.last_instance.use_ssl?,'Net::HTTP instance should be set to use SSL')
|
92
95
|
end
|
93
96
|
|
94
97
|
def test_default_format
|