mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-24 08:09:13 -05:00
Link Twitter handles in Twitter onebox
This commit is contained in:
parent
bd1b4d3130
commit
b84ee29c43
2 changed files with 68 additions and 3 deletions
|
@ -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, "<a href='#{url}' target='_blank'>#{url}</a>")
|
||||
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, "<a href='#{url}' target='_blank'>#{url}</a>")
|
||||
end
|
||||
|
||||
text
|
||||
end
|
||||
|
||||
def link_handles_in(text)
|
||||
text.scan(/\s@(\w+)/).flatten.uniq.each do |handle|
|
||||
text.gsub!("@#{handle}", [
|
||||
"<a href='https://twitter.com/#{handle}' target='_blank'>",
|
||||
"@#{handle}",
|
||||
"</a>"
|
||||
].join)
|
||||
end
|
||||
|
||||
text
|
||||
end
|
||||
|
||||
def tweet_for(id)
|
||||
request = Net::HTTP::Get.new(tweet_uri_for id)
|
||||
|
||||
|
|
43
spec/components/oneboxer/twitter_onebox_spec.rb
Normal file
43
spec/components/oneboxer/twitter_onebox_spec.rb
Normal file
|
@ -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 ",
|
||||
"<a href='http://twitter.com' target='_blank'>",
|
||||
"http://twitter.com",
|
||||
"</a>"
|
||||
].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 ",
|
||||
"<a href='https://twitter.com/chrishunt' target='_blank'>",
|
||||
"@chrishunt",
|
||||
"</a>"
|
||||
].join)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in a new issue