Skip to content

Instantly share code, notes, and snippets.

@Gordin
Created February 15, 2020 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gordin/d4f00b18a5c3d0addb850a1704dc77da to your computer and use it in GitHub Desktop.
Save Gordin/d4f00b18a5c3d0addb850a1704dc77da to your computer and use it in GitHub Desktop.
Fix for the twitter gem that allows Tweets with * in them
require "uri"
# The encoder method of the http gem needs to be overriden because of the twitter gem.
# Without that, there's an incompatibility between the simple_oauth gem which encodes asterisks and the http one which does not.
# Cf. https://github.com/httprb/form_data/issues/22 and https://github.com/sferik/twitter/issues/6# 77
# I added a check to see if this method has been called from inside the twitter gem, so that other libraries can still use the default behavior
HTTP::FormData::Urlencoded.encoder = lambda do |enum, enc = nil|
call_regex = Regexp.new(
'gems/twitter-\d+.\d+.\d+/lib/twitter/rest/request.rb:\d+:in `public_send\'')
if caller.any? call_regex
unescaped_chars = /[^a-z0-9\-\.\_\~]/i
enum.map 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|
str = ::URI::DEFAULT_PARSER.escape(k.to_s, unescaped_chars)
unless w.nil?
str << '='
str << ::URI::DEFAULT_PARSER.escape(w.to_s, unescaped_chars)
end
end.join('&')
else
str = ::URI::DEFAULT_PARSER.escape(k.to_s, unescaped_chars)
str << '='
str << ::URI::DEFAULT_PARSER.escape(v.to_s, unescaped_chars)
end
end.join('&')
else
::URI.encode_www_form(enum, enc)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment