Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug fix] Stop manual URI parsing/escaping and use CGI::parse #119

Merged
merged 6 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
bundler_args: --without development
dist: xenial
language: ruby
rvm:
- 1.9.3
- 2.0.0
- jruby-head
- rbx-2
- ruby-head

before_install:
- bundle config without 'development'

branches:
only:
- master

matrix:
fast_finish: true
allow_failures:
- rvm: jruby-head
- rvm: ruby-head
fast_finish: true
sudo: false
- rvm: rbx-2
include:
- rvm: 2.6.5
- rvm: 2.5.7
- rvm: 2.4.9
- rvm: jruby-head
- rvm: ruby-head
- rvm: rbx-2
19 changes: 11 additions & 8 deletions lib/twurl/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def parse_options(args)
arguments = args.dup

Twurl.options = Options.new
Twurl.options.args = arguments
Twurl.options.trace = false
Twurl.options.data = {}
Twurl.options.headers = {}
Expand Down Expand Up @@ -159,11 +160,9 @@ def extract_path!(arguments)
end

def escape_params(params)
split_params = params.split("&").map do |param|
key, value = param.split('=', 2)
CGI::escape(key) + '=' + CGI::escape(value)
end
split_params.join("&")
CGI::parse(params).map do |key, value|
"#{CGI.escape key}=#{CGI.escape value.first}"
end.join("&")
end
end

Expand Down Expand Up @@ -230,9 +229,13 @@ def trace

def data
on('-d', '--data [data]', 'Sends the specified data in a POST request to the HTTP server.') do |data|
data.split('&').each do |pair|
key, value = pair.split('=', 2)
options.data[key] = value
if options.args.count { |item| /content-type: (.*)/i.match(item) } > 0
options.data[data] = nil
else
data.split('&').each do |pair|
key, value = pair.split('=', 2)
options.data[key] = value
end
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion lib/twurl/oauth_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ def perform_request_from_options(options, &block)
elsif request.content_type && options.data
request.body = options.data.keys.first
elsif options.data
request.set_form_data(options.data)
request.content_type = "application/x-www-form-urlencoded"
if options.data.length == 1 && options.data.values.first == nil
request.body = options.data.keys.first
else
request.body = options.data.map do |key, value|
"#{key}=#{CGI.escape value}"
end.join("&")
end
end

request.oauth!(consumer.http, consumer, access_token)
Expand Down
3 changes: 1 addition & 2 deletions twurl.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ require 'twurl/version'

Gem::Specification.new do |spec|
spec.add_dependency 'oauth', '~> 0.4'
spec.add_development_dependency 'bundler', '~> 1.0'
spec.authors = ["Marcel Molina", "Erik Michaels-Ober"]
spec.description = %q{Curl for the Twitter API}
spec.email = ['marcel@twitter.com']
Expand All @@ -17,7 +16,7 @@ Gem::Specification.new do |spec|
spec.name = 'twurl'
spec.rdoc_options = ['--title', 'twurl -- OAuth-enabled curl for the Twitter API', '--main', 'README', '--line-numbers', '--inline-source']
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 1.9.3'
spec.required_ruby_version = '>= 2.4.0'
spec.rubyforge_project = 'twurl'
spec.summary = spec.description
spec.version = Twurl::Version
Expand Down