Created
February 15, 2020 02:42
-
-
Save Gordin/d4f00b18a5c3d0addb850a1704dc77da to your computer and use it in GitHub Desktop.
Fix for the twitter gem that allows Tweets with * in them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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