Skip to content

Commit

Permalink
Give Twitter module class-like interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Aug 18, 2013
1 parent 7284394 commit 34d46e9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
35 changes: 18 additions & 17 deletions lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,27 @@
require 'uri'

module Twitter
class << self
include Twitter::Configurable
extend Twitter::Configurable

# Delegate to a Twitter::Client
#
# @return [Twitter::Client]
def client
return @client if instance_variable_defined?(:@client) && @client.hash == options.hash
@client = Twitter::Client.new(options)
end

def method_missing(method_name, *args, &block)
return super unless respond_to_missing?(method_name)
client.send(method_name, *args, &block)
end
# Delegate to a Twitter::Client
#
# @return [Twitter::Client]
def new
return @client if instance_variable_defined?(:@client) && @client.hash == options.hash
@client = Twitter::Client.new(options)
end
module_function :new

def respond_to_missing?(method_name, include_private=false)
client.respond_to?(method_name, include_private)
end
def method_missing(method_name, *args, &block)
return super unless respond_to_missing?(method_name)
new.send(method_name, *args, &block)
end
module_function :method_missing

def respond_to_missing?(method_name, include_private=false)
new.respond_to?(method_name, include_private)
end
module_function :respond_to_missing?

setup
end
10 changes: 5 additions & 5 deletions spec/twitter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@
expect(Twitter.respond_to?(:user)).to be_true
end
it "takes an optional argument" do
expect(Twitter.respond_to?(:client, true)).to be_true
expect(Twitter.respond_to?(:new, true)).to be_true
end
end

describe ".client" do
it "returns a Twitter::Client" do
expect(Twitter.client).to be_a Twitter::Client
expect(Twitter.new).to be_a Twitter::Client
end

context "when the options don't change" do
it "caches the client" do
expect(Twitter.client).to eq Twitter.client
expect(Twitter.new).to eq Twitter.new
end
end
context "when the options change" do
it "busts the cache" do
client1 = Twitter.client
client1 = Twitter.new
Twitter.configure do |config|
config.consumer_key = 'abc'
config.consumer_secret = '123'
end
client2 = Twitter.client
client2 = Twitter.new
expect(client1).not_to eq client2
end
end
Expand Down

0 comments on commit 34d46e9

Please sign in to comment.