A platform for community discussion. Free, open, simple.
FIX: Replace Twitter handles one at a time (#15870)
Dan Ungureanu1fb97f8bbae0
diff --git a/lib/twitter_api.rb b/lib/twitter_api.rbindex 5664bb2a263..85155dd2e3a 100644--- a/lib/twitter_api.rb+++ b/lib/twitter_api.rb@@ -95,28 +95,17 @@ class TwitterApiprotecteddef link_handles_in(text)- text.scan(/(?:^|\s)@(\w+)/).flatten.uniq.each do |handle|- text.gsub!(/(?:^|\s)@#{handle}/, [- " <a href='https://twitter.com/#{handle}' target='_blank'>",- "@#{handle}",- "</a>"- ].join)- end-- text.strip+ text.gsub(/(?:^|\s)@\w+/) do |match|+ handle = match.strip[1..]+ "<a href='https://twitter.com/#{handle}' target='_blank'>@#{handle}</a>"+ end.stripenddef link_hashtags_in(text)- text.scan(/(?:^|\s)#(\w+)/).flatten.uniq.each do |hashtag|- text.gsub!(/(?:^|\s)##{hashtag}/, [- " <a href='https://twitter.com/search?q=%23#{hashtag}' ",- "target='_blank'>",- "##{hashtag}",- "</a>"- ].join)- end-- text.strip+ text.gsub(/(?:^|\s)#\w+/) do |match|+ hashtag = match.strip[1..]+ "<a href='https://twitter.com/search?q=%23#{hashtag}' target='_blank'>##{hashtag}</a>"+ end.stripenddef user_timeline_uri_for(screen_name)diff --git a/spec/lib/twitter_api_spec.rb b/spec/lib/twitter_api_spec.rbnew file mode 100644index 00000000000..7af55b842db--- /dev/null+++ b/spec/lib/twitter_api_spec.rb@@ -0,0 +1,21 @@+# frozen_string_literal: true++require 'rails_helper'++describe TwitterApi do+ context '.link_handles_in' do+ it 'correctly replaces handles' do+ expect(TwitterApi.send(:link_handles_in, "@foo @foobar")).to match_html <<~HTML+ <a href='https://twitter.com/foo' target='_blank'>@foo</a> <a href='https://twitter.com/foobar' target='_blank'>@foobar</a>+ HTML+ end+ end++ context '.link_hashtags_in' do+ it 'correctly replaces hashtags' do+ expect(TwitterApi.send(:link_hashtags_in, "#foo #foobar")).to match_html <<~HTML+ <a href='https://twitter.com/search?q=%23foo' target='_blank'>#foo</a> <a href='https://twitter.com/search?q=%23foobar' target='_blank'>#foobar</a>+ HTML+ end+ end+end