Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'master' of github.com:jnunemaker/twitter

  • Loading branch information...
commit e40624c8badcf15ce13694888301940bdbe7becb 2 parents f6420b5 + d96dea9
Erik Michaels-Ober sferik authored
1  lib/twitter.rb
... ... @@ -1,5 +1,6 @@
1 1 require File.expand_path('../twitter/error', __FILE__)
2 2 require File.expand_path('../twitter/configuration', __FILE__)
  3 +require File.expand_path('../twitter/api', __FILE__)
3 4 require File.expand_path('../twitter/client', __FILE__)
4 5 require File.expand_path('../twitter/search', __FILE__)
5 6 require File.expand_path('../twitter/base', __FILE__)
20 lib/twitter/api.rb
... ... @@ -0,0 +1,20 @@
  1 +require File.expand_path('../connection', __FILE__)
  2 +require File.expand_path('../request', __FILE__)
  3 +require File.expand_path('../authentication', __FILE__)
  4 +
  5 +module Twitter
  6 + class API
  7 + attr_accessor *Configuration::VALID_OPTIONS_KEYS
  8 +
  9 + def initialize(options={})
  10 + options = Twitter.options.merge(options)
  11 + Configuration::VALID_OPTIONS_KEYS.each do |key|
  12 + send("#{key}=", options[key])
  13 + end
  14 + end
  15 +
  16 + include Connection
  17 + include Request
  18 + include Authentication
  19 + end
  20 +end
18 lib/twitter/authentication.rb
... ... @@ -0,0 +1,18 @@
  1 +module Twitter
  2 + module Authentication
  3 + private
  4 +
  5 + def authentication
  6 + {
  7 + :consumer_key => consumer_key,
  8 + :consumer_secret => consumer_secret,
  9 + :token => oauth_token,
  10 + :token_secret => oauth_token_secret
  11 + }
  12 + end
  13 +
  14 + def authenticated?
  15 + authentication.values.all?
  16 + end
  17 + end
  18 +end
19 lib/twitter/client.rb
... ... @@ -1,19 +1,12 @@
1   -Dir[File.expand_path('../client/*.rb', __FILE__)].each{|f| require f}
2   -
3 1 module Twitter
4   - class Client
5   - attr_accessor *Configuration::VALID_OPTIONS_KEYS
  2 + class Client < API
  3 + # Require client method modules after initializing the Client class in
  4 + # order to avoid a superclass mismatch error, allowing those modules to be
  5 + # Client-namespaced.
  6 + Dir[File.expand_path('../client/*.rb', __FILE__)].each{|f| require f}
6 7
7   - def initialize(options={})
8   - options = Twitter.options.merge(options)
9   - Configuration::VALID_OPTIONS_KEYS.each do |key|
10   - send("#{key}=", options[key])
11   - end
12   - end
  8 + alias :api_endpoint :endpoint
13 9
14   - include Connection
15   - include Request
16   - include Authentication
17 10 include Utils
18 11
19 12 include Timeline
19 lib/twitter/client/authentication.rb
... ... @@ -1,19 +0,0 @@
1   -module Twitter
2   - class Client
3   - module Authentication
4   - private
5   - def authentication
6   - {
7   - :consumer_key => consumer_key,
8   - :consumer_secret => consumer_secret,
9   - :token => oauth_token,
10   - :token_secret => oauth_token_secret
11   - }
12   - end
13   -
14   - def authenticated?
15   - authentication.values.all?
16   - end
17   - end
18   - end
19   -end
28 lib/twitter/client/connection.rb
... ... @@ -1,28 +0,0 @@
1   -require 'faraday_middleware'
2   -Dir[File.expand_path('../../../faraday/*.rb', __FILE__)].each{|f| require f}
3   -
4   -module Twitter
5   - class Client
6   - module Connection
7   - private
8   -
9   - def connection(raw=false)
10   - options = {
11   - :headers => {:user_agent => user_agent},
12   - :ssl => {:verify => false},
13   - :url => endpoint
14   - }
15   -
16   - Faraday::Connection.new(options) do |builder|
17   - builder.use Faraday::Request::Multipart
18   - builder.use Faraday::Request::OAuth, authentication if authenticated?
19   - builder.adapter(adapter)
20   - builder.use Faraday::Response::RaiseHttp5xx
21   - builder.use Faraday::Response::Parse unless raw
22   - builder.use Faraday::Response::RaiseHttp4xx
23   - builder.use Faraday::Response::Mashify unless raw
24   - end
25   - end
26   - end
27   - end
28   -end
40 lib/twitter/client/request.rb
... ... @@ -1,40 +0,0 @@
1   -module Twitter
2   - class Client
3   - module Request
4   - def get(path, options={}, raw=false)
5   - request(:get, path, options, raw)
6   - end
7   -
8   - def post(path, options={}, raw=false)
9   - request(:post, path, options, raw)
10   - end
11   -
12   - def put(path, options={}, raw=false)
13   - request(:put, path, options, raw)
14   - end
15   -
16   - def delete(path, options={}, raw=false)
17   - request(:delete, path, options, raw)
18   - end
19   -
20   - private
21   -
22   - def request(method, path, options, raw)
23   - response = connection(raw).send(method) do |request|
24   - case method
25   - when :get, :delete
26   - request.url(formatted_path(path), options)
27   - when :post, :put
28   - request.path = formatted_path(path)
29   - request.body = options
30   - end
31   - end
32   - raw ? response : response.body
33   - end
34   -
35   - def formatted_path(path)
36   - [path, format].compact.join('.')
37   - end
38   - end
39   - end
40   -end
23 lib/twitter/configuration.rb
@@ -3,12 +3,14 @@
3 3
4 4 module Twitter
5 5 module Configuration
6   - VALID_OPTIONS_KEYS = [:consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret, :adapter, :endpoint, :format, :user_agent].freeze
7   - VALID_FORMATS = [:json, :xml].freeze
8   - DEFAULT_ADAPTER = Faraday.default_adapter.freeze
9   - DEFAULT_ENDPOINT = 'https://api.twitter.com/1/'.freeze
10   - DEFAULT_FORMAT = :json.freeze
11   - DEFAULT_USER_AGENT = "Twitter Ruby Gem #{Twitter::VERSION}".freeze
  6 + VALID_OPTIONS_KEYS = [:consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret, :adapter, :endpoint, :search_endpoint, :format, :user_agent].freeze
  7 + VALID_FORMATS = [:json, :xml].freeze
  8 +
  9 + DEFAULT_ADAPTER = Faraday.default_adapter.freeze
  10 + DEFAULT_ENDPOINT = 'https://api.twitter.com/1/'.freeze
  11 + DEFAULT_SEARCH_ENDPOINT = 'https://search.twitter.com/'.freeze
  12 + DEFAULT_FORMAT = :json.freeze
  13 + DEFAULT_USER_AGENT = "Twitter Ruby Gem #{Twitter::VERSION}".freeze
12 14
13 15 attr_accessor *VALID_OPTIONS_KEYS
14 16
@@ -25,10 +27,11 @@ def options
25 27 end
26 28
27 29 def reset
28   - self.adapter = DEFAULT_ADAPTER
29   - self.endpoint = DEFAULT_ENDPOINT
30   - self.format = DEFAULT_FORMAT
31   - self.user_agent = DEFAULT_USER_AGENT
  30 + self.adapter = DEFAULT_ADAPTER
  31 + self.endpoint = DEFAULT_ENDPOINT
  32 + self.search_endpoint = DEFAULT_SEARCH_ENDPOINT
  33 + self.format = DEFAULT_FORMAT
  34 + self.user_agent = DEFAULT_USER_AGENT
32 35 end
33 36 end
34 37 end
26 lib/twitter/connection.rb
... ... @@ -0,0 +1,26 @@
  1 +require 'faraday_middleware'
  2 +Dir[File.expand_path('../../faraday/*.rb', __FILE__)].each{|f| require f}
  3 +
  4 +module Twitter
  5 + module Connection
  6 + private
  7 +
  8 + def connection(raw=false)
  9 + options = {
  10 + :headers => {:user_agent => user_agent},
  11 + :ssl => {:verify => false},
  12 + :url => api_endpoint
  13 + }
  14 +
  15 + Faraday::Connection.new(options) do |builder|
  16 + builder.use Faraday::Request::Multipart
  17 + builder.use Faraday::Request::OAuth, authentication if authenticated?
  18 + builder.adapter(adapter)
  19 + builder.use Faraday::Response::RaiseHttp5xx
  20 + builder.use Faraday::Response::Parse unless raw
  21 + builder.use Faraday::Response::RaiseHttp4xx
  22 + builder.use Faraday::Response::Mashify unless raw
  23 + end
  24 + end
  25 + end
  26 +end
38 lib/twitter/request.rb
... ... @@ -0,0 +1,38 @@
  1 +module Twitter
  2 + module Request
  3 + def get(path, options={}, raw=false)
  4 + request(:get, path, options, raw)
  5 + end
  6 +
  7 + def post(path, options={}, raw=false)
  8 + request(:post, path, options, raw)
  9 + end
  10 +
  11 + def put(path, options={}, raw=false)
  12 + request(:put, path, options, raw)
  13 + end
  14 +
  15 + def delete(path, options={}, raw=false)
  16 + request(:delete, path, options, raw)
  17 + end
  18 +
  19 + private
  20 +
  21 + def request(method, path, options, raw)
  22 + response = connection(raw).send(method) do |request|
  23 + case method
  24 + when :get, :delete
  25 + request.url(formatted_path(path), options)
  26 + when :post, :put
  27 + request.path = formatted_path(path)
  28 + request.body = options
  29 + end
  30 + end
  31 + raw ? response : response.body
  32 + end
  33 +
  34 + def formatted_path(path)
  35 + [path, format].compact.join('.')
  36 + end
  37 + end
  38 +end
16 lib/twitter/search.rb
... ... @@ -1,30 +1,22 @@
1 1 require 'cgi'
2   -require File.expand_path('../search/request', __FILE__)
3   -Dir[File.expand_path('../search/*.rb', __FILE__)].each{|f| require f}
4 2
5 3 module Twitter
6 4
7 5 # Handles the Twitter Search API
8 6 #
9 7 # @see http://dev.twitter.com/doc/get/search Twitter Search API docs
10   - class Search
11   - attr_accessor *Configuration::VALID_OPTIONS_KEYS
  8 + class Search < API
12 9 attr_reader :fetch, :query
13 10
14 11 # Creates a new instance of a search
15 12 #
16 13 # @param [String] query the optional keyword to search
17   - def initialize(options={})
18   - options = Twitter.options.merge(options)
  14 + def initialize(*)
19 15 clear
20   - Configuration::VALID_OPTIONS_KEYS.each do |key|
21   - send("#{key}=", options[key])
22   - end
  16 + super
23 17 end
24 18
25   - include Connection
26   - include Request
27   - include Authentication
  19 + alias :api_endpoint :search_endpoint
28 20
29 21 # Clears all the query filters to make a new search
30 22 def clear
19 lib/twitter/search/authentication.rb
... ... @@ -1,19 +0,0 @@
1   -module Twitter
2   - class Search
3   - module Authentication
4   - private
5   - def authentication
6   - {
7   - :consumer_key => consumer_key,
8   - :consumer_secret => consumer_secret,
9   - :token => oauth_token,
10   - :token_secret => oauth_token_secret
11   - }
12   - end
13   -
14   - def authenticated?
15   - authentication.values.all?
16   - end
17   - end
18   - end
19   -end
27 lib/twitter/search/connection.rb
... ... @@ -1,27 +0,0 @@
1   -require 'faraday_middleware'
2   -Dir[File.expand_path('../../../faraday/*.rb', __FILE__)].each{|f| require f}
3   -
4   -module Twitter
5   - class Search
6   - module Connection
7   - private
8   -
9   - def connection(raw=false)
10   - options = {
11   - :headers => {:user_agent => user_agent},
12   - :ssl => {:verify => false},
13   - :url => endpoint
14   - }
15   -
16   - Faraday::Connection.new(options) do |builder|
17   - builder.use Faraday::Request::OAuth, authentication if authenticated?
18   - builder.adapter(adapter)
19   - builder.use Faraday::Response::RaiseHttp5xx
20   - builder.use Faraday::Response::Parse unless raw
21   - builder.use Faraday::Response::RaiseHttp4xx
22   - builder.use Faraday::Response::Mashify unless raw
23   - end
24   - end
25   - end
26   - end
27   -end
22 lib/twitter/search/request.rb
... ... @@ -1,22 +0,0 @@
1   -module Twitter
2   - class Search
3   - module Request
4   - def get(path, options={}, raw=false)
5   - request(:get, path, options, raw)
6   - end
7   -
8   - private
9   -
10   - def request(method, path, options, raw)
11   - response = connection(raw).send(method) do |request|
12   - request.url(formatted_path(path), options)
13   - end
14   - raw ? response : response.body
15   - end
16   -
17   - def formatted_path(path)
18   - [path, format].compact.join('.')
19   - end
20   - end
21   - end
22   -end
68 spec/twitter/api_spec.rb
... ... @@ -0,0 +1,68 @@
  1 +require File.expand_path('../../spec_helper', __FILE__)
  2 +
  3 +describe "Twitter::API" do
  4 + before do
  5 + @keys = Twitter::Configuration::VALID_OPTIONS_KEYS
  6 + end
  7 +
  8 + context "with module configuration" do
  9 +
  10 + before do
  11 + Twitter.configure do |config|
  12 + @keys.each do |key|
  13 + config.send("#{key}=", key)
  14 + end
  15 + end
  16 + end
  17 +
  18 + after do
  19 + Twitter.reset
  20 + end
  21 +
  22 + it "should inherit module configuration" do
  23 + api = Twitter::API.new
  24 + @keys.each do |key|
  25 + api.send(key).should == key
  26 + end
  27 + end
  28 +
  29 + context "with class configuration" do
  30 +
  31 + before do
  32 + @configuration = {
  33 + :consumer_key => 'CK',
  34 + :consumer_secret => 'CS',
  35 + :oauth_token => 'OT',
  36 + :oauth_token_secret => 'OS',
  37 + :adapter => :typhoeus,
  38 + :endpoint => 'http://tumblr.com/',
  39 + :search_endpoint => 'http://google.com/',
  40 + :format => :xml,
  41 + :user_agent => 'Custom User Agent',
  42 + }
  43 + end
  44 +
  45 + context "during initialization"
  46 +
  47 + it "should override module configuration" do
  48 + api = Twitter::API.new(@configuration)
  49 + @keys.each do |key|
  50 + api.send(key).should == @configuration[key]
  51 + end
  52 + end
  53 +
  54 + context "after initilization" do
  55 +
  56 + it "should override module configuration after initialization" do
  57 + api = Twitter::API.new
  58 + @configuration.each do |key, value|
  59 + api.send("#{key}=", value)
  60 + end
  61 + @keys.each do |key|
  62 + api.send(key).should == @configuration[key]
  63 + end
  64 + end
  65 + end
  66 + end
  67 + end
  68 +end
67 spec/twitter/client_spec.rb
... ... @@ -1,67 +1,10 @@
1 1 require File.expand_path('../../spec_helper', __FILE__)
2 2
3 3 describe "Twitter::Client" do
4   - before do
5   - @keys = Twitter::Configuration::VALID_OPTIONS_KEYS
6   - end
7   -
8   - context "with module configuration" do
9   -
10   - before do
11   - Twitter.configure do |config|
12   - @keys.each do |key|
13   - config.send("#{key}=", key)
14   - end
15   - end
16   - end
17   -
18   - after do
19   - Twitter.reset
20   - end
21   -
22   - it "should inherit module configuration" do
23   - client = Twitter::Client.new
24   - @keys.each do |key|
25   - client.send(key).should == key
26   - end
27   - end
28   -
29   - context "with class configuration" do
30   -
31   - before do
32   - @configuration = {
33   - :consumer_key => 'CK',
34   - :consumer_secret => 'CS',
35   - :oauth_token => 'OT',
36   - :oauth_token_secret => 'OS',
37   - :adapter => :typhoeus,
38   - :endpoint => 'http://tumblr.com/',
39   - :format => :xml,
40   - :user_agent => 'Custom User Agent',
41   - }
42   - end
43   -
44   - context "during initialization"
45   -
46   - it "should override module configuration" do
47   - client = Twitter::Client.new(@configuration)
48   - @keys.each do |key|
49   - client.send(key).should == @configuration[key]
50   - end
51   - end
52   -
53   - context "after initilization" do
54   -
55   - it "should override module configuration after initialization" do
56   - client = Twitter::Client.new
57   - @configuration.each do |key, value|
58   - client.send("#{key}=", value)
59   - end
60   - @keys.each do |key|
61   - client.send(key).should == @configuration[key]
62   - end
63   - end
64   - end
65   - end
  4 + it "should connect using the endpoint configuration" do
  5 + client = Twitter::Client.new
  6 + endpoint = URI.parse(client.api_endpoint)
  7 + connection = client.send(:connection).build_url(nil).to_s
  8 + connection.should == endpoint.to_s
66 9 end
67 10 end
8 spec/twitter/search_spec.rb
@@ -2,6 +2,13 @@
2 2
3 3 describe "Twitter::Search" do
4 4
  5 + it "should connect using the search_endpoint configuration" do
  6 + search = Twitter::Search.new
  7 + endpoint = URI.parse(search.api_endpoint)
  8 + connection = search.send(:connection).build_url(nil).to_s
  9 + connection.should == endpoint.to_s
  10 + end
  11 +
5 12 context "with module configuration" do
6 13
7 14 before do
@@ -34,6 +41,7 @@
34 41 :oauth_token_secret => 'OS',
35 42 :adapter => :typhoeus,
36 43 :endpoint => 'http://tumblr.com/',
  44 + :search_endpoint => 'http://google.com/',
37 45 :format => :xml,
38 46 :user_agent => 'Custom User Agent',
39 47 }

0 comments on commit e40624c

Please sign in to comment.
Something went wrong with that request. Please try again.