Fix #3984: Twitter: undefined method `first' for nil:NilClass.

Fix Sources::Strategies::Twitter#image_urls to return an empty array
instead of nil when the tweet doesn't contain any images.
This commit is contained in:
evazion
2018-11-11 17:41:32 -06:00
parent a20eba9ef7
commit 6a7cd6ce8e
4 changed files with 17 additions and 10 deletions

View File

@@ -41,15 +41,11 @@ module Sources::Strategies
def image_urls def image_urls
if url =~ /(#{ASSET}[^:]+)/ if url =~ /(#{ASSET}[^:]+)/
return [$1 + ":orig" ] [$1 + ":orig" ]
elsif api_response.blank? elsif api_response.present?
return [url] service.image_urls(api_response)
end else
[url]
[url, referer_url].each do |x|
if x =~ PAGE
return service.image_urls(api_response)
end
end end
end end
memoize :image_urls memoize :image_urls

View File

@@ -67,6 +67,8 @@ class TwitterService
extract_urls_for_status(tweet) extract_urls_for_status(tweet)
elsif tweet.urls.any? elsif tweet.urls.any?
extract_urls_for_card(tweet) extract_urls_for_card(tweet)
else
[]
end end
end end
end end

View File

@@ -1,7 +1,7 @@
<% if params[:url] %> <% if params[:url] %>
<% if ImageProxy.needs_proxy?(@source.image_url) %> <% if ImageProxy.needs_proxy?(@source.image_url) %>
<%= image_tag(image_proxy_uploads_path(:url => @source.image_url), :title => "Preview", :id => "image") %> <%= image_tag(image_proxy_uploads_path(:url => @source.image_url), :title => "Preview", :id => "image") %>
<% else %> <% elsif @source.image_url.present? %>
<%= image_tag(@source.image_url, :title => "Preview", :id => "image") %> <%= image_tag(@source.image_url, :title => "Preview", :id => "image") %>
<% end %> <% end %>

View File

@@ -97,6 +97,15 @@ module Sources
end end
end end
context "A tweet without any images" do
should "not fail" do
@site = Sources::Strategies.find("https://twitter.com/teruyo/status/1058452066060853248")
assert_nil(@site.image_url)
assert_nothing_raised { @site.to_h }
end
end
context "The source site for twitter" do context "The source site for twitter" do
setup do setup do
@site = Sources::Strategies.find("https://mobile.twitter.com/nounproject/status/540944400767922176") @site = Sources::Strategies.find("https://mobile.twitter.com/nounproject/status/540944400767922176")