From b84ee29c435c46de41d813b073535a949de60fb2 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 13 Jun 2013 21:00:47 -0700 Subject: [PATCH 1/2] Link Twitter handles in Twitter onebox --- lib/oneboxer/twitter_onebox.rb | 28 ++++++++++-- .../oneboxer/twitter_onebox_spec.rb | 43 +++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 spec/components/oneboxer/twitter_onebox_spec.rb 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 + From 4491b1f06fd6bafcf05ed43d9b2cdd16fa809519 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 13 Jun 2013 21:09:37 -0700 Subject: [PATCH 2/2] Link hashtags in Twitter onebox --- lib/oneboxer/twitter_onebox.rb | 19 ++++++++++++++++--- .../oneboxer/twitter_onebox_spec.rb | 13 +++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/oneboxer/twitter_onebox.rb b/lib/oneboxer/twitter_onebox.rb index 9a8ee8c2fc7..6da44703585 100644 --- a/lib/oneboxer/twitter_onebox.rb +++ b/lib/oneboxer/twitter_onebox.rb @@ -24,15 +24,15 @@ module Oneboxer result['created_at'] = Time.parse(result['created_at']).strftime("%I:%M%p - %d %b %y") - result['text'] = link_urls_and_handles_in result['text'] + result['text'] = link_all_the_things_in result['text'] result end private - def link_urls_and_handles_in(text) - link_handles_in link_urls_in(text) + def link_all_the_things_in(text) + link_hashtags_in link_handles_in link_urls_in(text) end def link_urls_in(text) @@ -55,6 +55,19 @@ module Oneboxer text end + def link_hashtags_in(text) + text.scan(/\s#(\w+)/).flatten.uniq.each do |hashtag| + text.gsub!("##{hashtag}", [ + "", + "##{hashtag}", + "" + ].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 index 5636f99fa78..e0e8d1276d0 100644 --- a/spec/components/oneboxer/twitter_onebox_spec.rb +++ b/spec/components/oneboxer/twitter_onebox_spec.rb @@ -38,6 +38,19 @@ describe Oneboxer::TwitterOnebox do ].join) end end + + context 'when the text contains a hashtag' do + let(:text) { 'No secrets. #NSA' } + + it 'wraps each hashtag in a link' do + expect(subject.parse(data)['text']).to eq([ + "No secrets. ", + "", + "#NSA", + "" + ].join) + end + end end end