diff --git a/lib/oneboxer/twitter_onebox.rb b/lib/oneboxer/twitter_onebox.rb index b242d976fc4..9a8ee8c2fc7 100644 --- a/lib/oneboxer/twitter_onebox.rb +++ b/lib/oneboxer/twitter_onebox.rb @@ -24,15 +24,37 @@ module Oneboxer result['created_at'] = Time.parse(result['created_at']).strftime("%I:%M%p - %d %b %y") - URI.extract(result['text'], %w(http https)).each do |url| - result['text'].gsub!(url, "#{url}") - end + result['text'] = link_urls_and_handles_in result['text'] result end private + def link_urls_and_handles_in(text) + link_handles_in link_urls_in(text) + end + + def link_urls_in(text) + URI.extract(text, %w(http https)).each do |url| + text.gsub!(url, "#{url}") + end + + text + end + + def link_handles_in(text) + text.scan(/\s@(\w+)/).flatten.uniq.each do |handle| + text.gsub!("@#{handle}", [ + "", + "@#{handle}", + "" + ].join) + end + + text + end + def tweet_for(id) request = Net::HTTP::Get.new(tweet_uri_for id) diff --git a/spec/components/oneboxer/twitter_onebox_spec.rb b/spec/components/oneboxer/twitter_onebox_spec.rb new file mode 100644 index 00000000000..5636f99fa78 --- /dev/null +++ b/spec/components/oneboxer/twitter_onebox_spec.rb @@ -0,0 +1,43 @@ +require 'spec_helper' + +describe Oneboxer::TwitterOnebox do + subject { described_class.new(nil, nil) } + + let(:data) { %({ "text":"#{text}", "created_at":"#{created_at}" }) } + + let(:text) { '' } + let(:created_at) { '2013-06-13T22:37:05Z' } + + describe '#parse' do + it 'formats the timestamp' do + expect(subject.parse(data)['created_at']).to eq '10:37PM - 13 Jun 13' + end + + context 'when text contains a url' do + let(:text) { 'Twitter http://twitter.com' } + + it 'wraps eack url in a link' do + expect(subject.parse(data)['text']).to eq([ + "Twitter ", + "", + "http://twitter.com", + "" + ].join) + end + end + + context 'when the text contains a twitter handle' do + let(:text) { 'I like @chrishunt' } + + it 'wraps each handle in a link' do + expect(subject.parse(data)['text']).to eq([ + "I like ", + "", + "@chrishunt", + "" + ].join) + end + end + end +end +