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 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=:basic,:username=>'your_user',:password=>'yourpass'})
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
- Invoking the API method "/users/show" in XML format for the Twitter user "some_user" looks like
55
- client.users.show.xml :id=>'some_user' #http://twitter.com/users/show.xml?id=some_user
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
- Or using the JSON format:
58
- client.users.show.json :id=>'some_user' #http://twitter.com/users/show.json?id=some_user
59
-
60
- Or, since Twitter also allows certain ids to be part of their URLs, this works:
61
- client.users.show.some_user.json #http://twitter.com/users/show/some_user.json
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
- The client has a default format of :json so if you want to use the above call with the default format you can do:
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 include a "?" at the end of any method name, that signals to the Grackle client that you want to execute an HTTP GET request.
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
- For example, to update the authenticated user's status using the XML format:
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? :exclue=>'hashtags'
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 currently supports JSON and XML. The Grackle::Client has a
110
- default_format you can specify. By default, the default_format is :json. If you don't include a named format in your method
111
- chain as described above, but use a "?" or "!" then the Grackle::Client.default_format is used.
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{grackle}
5
- s.version = "0.1.0"
5
+ s.version = "0.1.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Hayes Davis"]
data/lib/grackle.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Grackle
2
2
 
3
3
  # :stopdoc:
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
6
6
  PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
7
7
  # :startdoc:
@@ -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:
@@ -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).start do |http|
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hayesdavis-grackle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hayes Davis