Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Feb 4, 2023
1 parent ee620fb commit 3b468dc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/twitter/rest/form_encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ class FormEncoder
UNESCAPED_CHARS = /[^a-z0-9\-\.\_\~]/i

def self.encode(data)
data.map do |k, v|
data.collect do |k, v|
if v.nil?
::URI::DEFAULT_PARSER.escape(k.to_s, UNESCAPED_CHARS)
elsif v.respond_to?(:to_ary)
v.to_ary.map do |w|
v.to_ary.collect do |w|
str = ::URI::DEFAULT_PARSER.escape(k.to_s, UNESCAPED_CHARS)
unless w.nil?
str << '='
Expand Down
10 changes: 5 additions & 5 deletions lib/twitter/rest/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ def perform
private

def request_options
if @options_key == :form
options = {form: HTTP::FormData.create(@options, encoder: Twitter::REST::FormEncoder.method(:encode))}
else
options = {@options_key => @options}
end
options = if @options_key == :form
{form: HTTP::FormData.create(@options, encoder: Twitter::REST::FormEncoder.method(:encode))}
else
{@options_key => @options}
end

if @params
if options[:params]
Expand Down
1 change: 1 addition & 0 deletions spec/twitter/rest/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made
expect(a_post('/1.1/statuses/update.json').with(body: {status: 'Update', media_ids: '470030289822314497'})).to have_been_made
end

it 'uses custom HTTP::FormData::Urlencoded instance for form requests' do
stub_post('/1.1/statuses/update.json').with(body: {status: 'Update'}).to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
expect_any_instance_of(HTTP::Client).to receive(:post).with('https://api.twitter.com/1.1/statuses/update.json', form: instance_of(HTTP::FormData::Urlencoded)).and_call_original
Expand Down

0 comments on commit 3b468dc

Please sign in to comment.