Skip to content

Commit

Permalink
Add the -A/--header command line option to support adding a header to…
Browse files Browse the repository at this point in the history
… the request.
  • Loading branch information
Marcel Molina committed Jan 5, 2011
1 parent 4f38cd6 commit 2118f97
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
9 changes: 9 additions & 0 deletions lib/twurl/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def parse_options(args)
Twurl.options = Options.new
Twurl.options.trace = false
Twurl.options.data = {}
Twurl.options.headers = {}

option_parser = OptionParser.new do |o|
o.extend AvailableOptions
Expand All @@ -64,6 +65,7 @@ def parse_options(args)
o.section "Common options:" do
trace
data
headers
host
quiet
disable_ssl
Expand Down Expand Up @@ -190,6 +192,13 @@ def data
end
end

def headers
on('-A', '--header [header]', 'Adds the specified header to the request to the HTTP server.') do |header|
key, value = header.split(': ')
options.headers[key] = value
end
end

def host
on('-H', '--host [host]', 'Specify host to make requests to (default: api.twitter.com)') do |host|
options.host = host
Expand Down
12 changes: 8 additions & 4 deletions lib/twurl/oauth_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,19 @@ def initialize(options = {})

[:get, :post, :put, :delete, :options, :head, :copy].each do |request_method|
class_eval(<<-EVAL, __FILE__, __LINE__)
def #{request_method}(url, options = {})
def #{request_method}(url, *options)
# configure_http!
access_token.#{request_method}(url, options)
access_token.#{request_method}(url, *options)
end
EVAL
end

def perform_request_from_options(options)
send(options.request_method, options.path, options.data)
if [:post, :put].include?(options.request_method.to_sym)
send(options.request_method, options.path, options.data, options.headers)
else
send(options.request_method, options.path, options.headers)
end
end

def exchange_credentials_for_access_token
Expand Down Expand Up @@ -167,4 +171,4 @@ def access_token
@access_token ||= OAuth::AccessToken.new(consumer, token, secret)
end
end
end
end
18 changes: 17 additions & 1 deletion test/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ def test_multiple_pairs_separated_by_ampersand_are_all_captured
end
include DataParsingTests

module HeaderParsingTests
def test_extracting_a_single_header
options = Twurl::CLI.parse_options(['-A', 'Key: Value'])
assert_equal({'Key' => 'Value'}, options.headers)

options = Twurl::CLI.parse_options(['--header', 'Key: Value'])
assert_equal({'Key' => 'Value'}, options.headers)
end

def test_multiple_headers_when_option_is_specified_multiple_times_on_command_line_collects_all
options = Twurl::CLI.parse_options(['-A', 'Key: Value', '-A', 'Another: Pair'])
assert_equal({'Key' => 'Value', 'Another' => 'Pair'}, options.headers)
end
end
include HeaderParsingTests

module SSLDisablingTests
def test_ssl_is_on_by_default
options = Twurl::CLI.parse_options([])
Expand Down Expand Up @@ -126,4 +142,4 @@ def test_setting_host_updates_to_requested_value
end
end
include HostOptionTests
end
end
3 changes: 2 additions & 1 deletion test/oauth_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def setup
options.request_method = 'get'
options.path = '/path/does/not/matter.xml'
options.data = {}
options.headers = {}

Twurl.options = options
end
Expand Down Expand Up @@ -159,4 +160,4 @@ def test_successful_exchange_parses_token_and_secret_from_response_body
client.exchange_credentials_for_access_token
assert !client.needs_to_authorize?
end
end
end

0 comments on commit 2118f97

Please sign in to comment.