Link hashtags in Twitter onebox

This commit is contained in:
Chris Hunt 2013-06-13 21:09:37 -07:00
parent b84ee29c43
commit 4491b1f06f
2 changed files with 29 additions and 3 deletions

View File

@ -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}", [
"<a href='https://twitter.com/search?q=%23#{hashtag}' ",
"target='_blank'>",
"##{hashtag}",
"</a>"
].join)
end
text
end
def tweet_for(id)
request = Net::HTTP::Get.new(tweet_uri_for id)

View File

@ -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. ",
"<a href='https://twitter.com/search?q=%23NSA' target='_blank'>",
"#NSA",
"</a>"
].join)
end
end
end
end