Skip to content

Commit

Permalink
Stick Options on Twurl since Twurl can be used as a library and not j…
Browse files Browse the repository at this point in the history
…ust via the CLI.
  • Loading branch information
Marcel Molina committed Nov 18, 2009
1 parent 30c1926 commit b05a1da
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 56 deletions.
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,10 @@ namespace :dist do
raise
end
end

desc "Unpack current version of library into the twitter.com vendor directory"
task :unpack_to_vendor => :repackage do
cd 'pkg'
system("gem unpack '#{spec.name}-#{spec.version}.gem' --target=$TWITTER/vendor/gems")
end
end
5 changes: 5 additions & 0 deletions lib/twurl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
end

module Twurl
@options ||= Options.new
class << self
attr_accessor :options
end

class Exception < ::Exception
end
end
48 changes: 24 additions & 24 deletions lib/twurl/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CLI
@output ||= STDOUT

class << self
attr_accessor :options, :output
attr_accessor :output

def run(args)
options = parse_options(args)
Expand Down Expand Up @@ -39,9 +39,9 @@ def dispatch(options)
def parse_options(args)
arguments = args.dup

@options = Options.new
options.trace = false
options.data = {}
Twurl.options = Options.new
Twurl.options.trace = false
Twurl.options.data = {}

option_parser = OptionParser.new do |o|
o.extend AvailableOptions
Expand Down Expand Up @@ -76,13 +76,13 @@ def parse_options(args)
end

arguments = option_parser.parse!(args)
options.request_method ||= options.data.empty? ? DEFAULT_REQUEST_METHOD : 'post'
options.protocol ||= DEFAULT_PROTOCOL
options.host ||= DEFAULT_HOST
options.command = extract_command!(arguments)
options.path = extract_path!(arguments)
options.subcommands = arguments
options
Twurl.options.request_method ||= Twurl.options.data.empty? ? DEFAULT_REQUEST_METHOD : 'post'
Twurl.options.protocol ||= DEFAULT_PROTOCOL
Twurl.options.host ||= DEFAULT_HOST
Twurl.options.command = extract_command!(arguments)
Twurl.options.path = extract_path!(arguments)
Twurl.options.subcommands = arguments
Twurl.options
end

def puts(*args, &block)
Expand Down Expand Up @@ -112,7 +112,7 @@ def extract_path!(arguments)

module AvailableOptions
def options
CLI.options
Twurl.options
end

def section(heading, &block)
Expand Down Expand Up @@ -223,22 +223,22 @@ def prompt_for(label)
system "stty echo"
end
end
end

class Options < OpenStruct
def oauth_client_options
OAuthClient::OAUTH_CLIENT_OPTIONS.inject({}) do |options, option|
options[option] = send(option)
options
end
class Options < OpenStruct
def oauth_client_options
OAuthClient::OAUTH_CLIENT_OPTIONS.inject({}) do |options, option|
options[option] = send(option)
options
end
end

def base_url
"#{protocol}://#{host}"
end
def base_url
"#{protocol}://#{host}"
end

def ssl?
protocol == 'https'
end
def ssl?
protocol == 'https'
end
end
end
6 changes: 3 additions & 3 deletions lib/twurl/oauth_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ def consumer
consumer = OAuth::Consumer.new(
consumer_key,
consumer_secret,
:site => CLI.options.base_url
:site => Twurl.options.base_url
)
consumer.http.set_debug_output(STDERR) if CLI.options.trace
if CLI.options.ssl?
consumer.http.set_debug_output(STDERR) if Twurl.options.trace
if Twurl.options.ssl?
consumer.http.use_ssl = true
consumer.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
Expand Down
8 changes: 4 additions & 4 deletions test/account_information_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Twurl::AccountInformationController::DispatchWithNoAuthorizedAccountsTest < Test::Unit::TestCase
attr_reader :options, :client, :controller
def setup
@options = Twurl::CLI::Options.new
@options = Twurl::Options.new
@client = Twurl::OAuthClient.load_new_client_from_options(options)
@controller = Twurl::AccountInformationController.new(client, options)
mock(Twurl::OAuthClient.rcfile).empty? { true }
Expand All @@ -19,7 +19,7 @@ def test_message_indicates_when_no_accounts_are_authorized
class Twurl::AccountInformationController::DispatchWithOneAuthorizedAccountTest < Test::Unit::TestCase
attr_reader :options, :client, :controller
def setup
@options = Twurl::CLI::Options.test_exemplar
@options = Twurl::Options.test_exemplar
@client = Twurl::OAuthClient.load_new_client_from_options(options)
mock(Twurl::OAuthClient.rcfile).save.times(1)
Twurl::OAuthClient.rcfile << client
Expand All @@ -37,10 +37,10 @@ def test_authorized_account_is_displayed_and_marked_as_the_default
class Twurl::AccountInformationController::DispatchWithOneUsernameThatHasAuthorizedMultipleAccountsTest < Test::Unit::TestCase
attr_reader :default_client_options, :default_client, :other_client_options, :other_client, :controller
def setup
@default_client_options = Twurl::CLI::Options.test_exemplar
@default_client_options = Twurl::Options.test_exemplar
@default_client = Twurl::OAuthClient.load_new_client_from_options(default_client_options)

@other_client_options = Twurl::CLI::Options.test_exemplar
@other_client_options = Twurl::Options.test_exemplar
other_client_options.consumer_key = default_client_options.consumer_key.reverse
@other_client = Twurl::OAuthClient.load_new_client_from_options(other_client_options)
mock(Twurl::OAuthClient.rcfile).save.times(2)
Expand Down
2 changes: 1 addition & 1 deletion test/alias_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Twurl::AliasesController::DispatchTest < Test::Unit::TestCase
attr_reader :options, :client
def setup
@options = Twurl::CLI::Options.test_exemplar
@options = Twurl::Options.test_exemplar
@client = Twurl::OAuthClient.test_exemplar

# Clean slate
Expand Down
2 changes: 1 addition & 1 deletion test/authorization_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Twurl::AuthorizationController::DispatchTest < Test::Unit::TestCase
attr_reader :options, :client, :controller
def setup
@options = Twurl::CLI::Options.new
@options = Twurl::Options.new
@client = Twurl::OAuthClient.load_new_client_from_options(options)
@controller = Twurl::AuthorizationController.new(client, options)
end
Expand Down
4 changes: 2 additions & 2 deletions test/cli_options_test.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require File.dirname(__FILE__) + '/test_helper'

class Twurl::CLI::Options::Test < Test::Unit::TestCase
class Twurl::Options::Test < Test::Unit::TestCase
attr_reader :options
def setup
@options = Twurl::CLI::Options.new
@options = Twurl::Options.new
end

def test_base_url_is_built_from_protocol_and_host
Expand Down
6 changes: 3 additions & 3 deletions test/configuration_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Twurl::ConfigurationController::DispatchTest < Test::Unit::TestCase
def test_error_message_is_displayed_if_setting_is_unrecognized
options = Twurl::CLI::Options.test_exemplar
options = Twurl::Options.test_exemplar
client = Twurl::OAuthClient.test_exemplar

options.subcommands = ['unrecognized', 'value']
Expand All @@ -17,7 +17,7 @@ def test_error_message_is_displayed_if_setting_is_unrecognized

class Twurl::ConfigurationController::DispatchDefaultSettingTest < Test::Unit::TestCase
def test_setting_default_profile_just_by_username
options = Twurl::CLI::Options.test_exemplar
options = Twurl::Options.test_exemplar
client = Twurl::OAuthClient.test_exemplar

options.subcommands = ['default', client.username]
Expand All @@ -30,7 +30,7 @@ def test_setting_default_profile_just_by_username
end

def test_setting_default_profile_by_username_and_consumer_key
options = Twurl::CLI::Options.test_exemplar
options = Twurl::Options.test_exemplar
client = Twurl::OAuthClient.test_exemplar

options.subcommands = ['default', client.username, client.consumer_key]
Expand Down
6 changes: 3 additions & 3 deletions test/oauth_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ class Twurl::OAuthClient::AbstractOAuthClientTest < Test::Unit::TestCase
def setup
Twurl::OAuthClient.instance_variable_set(:@rcfile, nil)

@options = Twurl::CLI::Options.test_exemplar
@options = Twurl::Options.test_exemplar
@client = Twurl::OAuthClient.test_exemplar
options.base_url = 'api.twitter.com'
options.request_method = 'get'
options.path = '/path/does/not/matter.xml'
options.data = {}

Twurl::CLI.options = options
Twurl.options = options
end

def teardown
super
Twurl::CLI.options = nil
Twurl.options = Twurl::Options.new
# Make sure we don't do any disk IO in these tests
assert !File.exists?(Twurl::RCFile.file_path)
end
Expand Down
2 changes: 1 addition & 1 deletion test/rcfile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_rcfile_is_considered_empty_at_first
end

def test_setting_default_profile
options = Twurl::CLI::Options.test_exemplar
options = Twurl::Options.test_exemplar

client = Twurl::OAuthClient.load_new_client_from_options(options)
rcfile.default_profile = client
Expand Down
2 changes: 1 addition & 1 deletion test/request_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Twurl::RequestController::AbstractTestCase < Test::Unit::TestCase
attr_reader :options, :client, :controller
def setup
Twurl::CLI.output = StringIO.new
@options = Twurl::CLI::Options.test_exemplar
@options = Twurl::Options.test_exemplar
@client = Twurl::OAuthClient.test_exemplar
@controller = Twurl::RequestController.new(client, options)
end
Expand Down
24 changes: 11 additions & 13 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,24 @@ class Test::Unit::TestCase
Twurl::RCFile.directory = ENV['TMPDIR']

module Twurl
class CLI
class Options
class << self
def test_exemplar
options = new
options.username = 'exemplar_user_name'
options.password = 'secret'
options.consumer_key = '123456789'
options.consumer_secret = '987654321'
options.subcommands = []
options
end
class Options
class << self
def test_exemplar
options = new
options.username = 'exemplar_user_name'
options.password = 'secret'
options.consumer_key = '123456789'
options.consumer_secret = '987654321'
options.subcommands = []
options
end
end
end

class OAuthClient
class << self
def test_exemplar(overrides = {})
options = Twurl::CLI::Options.test_exemplar
options = Twurl::Options.test_exemplar

overrides.each do |attribute, value|
options.send("#{attribute}=", value)
Expand Down

0 comments on commit b05a1da

Please sign in to comment.