Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert twitter embed change, add thenextweb to the whitelist, bump versi... #228

Merged
merged 1 commit into from Aug 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
89 changes: 81 additions & 8 deletions lib/onebox/engine/twitter_status_onebox.rb
Expand Up @@ -2,22 +2,95 @@ module Onebox
module Engine
class TwitterStatusOnebox
include Engine
include JSON
include LayoutSupport
include HTML

matches_regexp Regexp.new("^http(?:s)?://(?:www\\.)?(?:(?:\\w)+\\.)?(twitter)\\.com(?:/)?(?:.)*/status(es)?/")

def url
"https://api.twitter.com/1/statuses/oembed.json?id=#{match[:id]}"
private

def match
@match ||= @url.match(%r{twitter\.com/.+?/status(es)?/(?<id>\d+)})
end

def to_html
raw['html']
def client
Onebox.options.twitter_client
end

private
def raw
if client
@raw ||= OpenStruct.new(client.status(match[:id]).to_hash)
else
super
end
end

def match
@match ||= @url.match(%r{twitter\.com/.+?/status(es)?/(?<id>\d+)})
def access(*keys)
keys.reduce(raw) do |memo, key|
memo[key] || memo[key.to_s]
end
end

def tweet
if raw.html?
raw.css(".tweet-text")[0].inner_text
else
access(:text)
end
end

def timestamp
if raw.html?
raw.css(".metadata span")[0].inner_text
else
created_at = access(:created_at)
date = DateTime.strptime(created_at, "%a %b %d %H:%M:%S %z %Y")
user_offset = access(:user, :utc_offset).to_i
offset = (user_offset >= 0 ? "+" : "-") + Time.at(user_offset.abs).gmtime.strftime("%H%M")
date.new_offset(offset).strftime("%l:%M %p - %e %b %Y")
end
end

def title
if raw.html?
raw.css(".stream-item-header .username").inner_text
else
access(:user, :screen_name)
end
end

def avatar
if raw.html?
raw.css(".avatar")[2]["src"]
else
access(:user, :profile_image_url)
end
end

def favorites
if raw.html?
raw.css(".stats li .request-favorited-popup").inner_text
else
access(:favorite_count)
end
end

def retweets
if raw.html?
raw.css(".stats li .request-retweeted-popup").inner_text
else
access(:retweet_count)
end
end

def data
{ link: link,
tweet: tweet,
timestamp: timestamp,
title: title,
avatar: avatar,
favorites: favorites,
retweets: retweets }
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/onebox/engine/whitelisted_generic_onebox.rb
Expand Up @@ -96,6 +96,7 @@ def self.default_whitelist
ted.com
thefreedictionary.com
theglobeandmail.com
thenextweb.com
theonion.com
thestar.com
thesun.co.uk
Expand Down
2 changes: 1 addition & 1 deletion lib/onebox/version.rb
@@ -1,3 +1,3 @@
module Onebox
VERSION = "1.4.7"
VERSION = "1.4.8"
end