changeset 103:1251b3d6ec5d

Use included url data instead of expanding using embiggen
author nanaya <me@myconan.net>
date Mon, 26 Sep 2016 02:39:40 +0900
parents f4805ac41656
children f2398d2a6859
files Gemfile Gemfile.lock app/helpers/application_helper.rb app/views/tweets/_tweet.html.erb
diffstat 4 files changed, 21 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/Gemfile	Fri Sep 23 23:10:54 2016 +0900
+++ b/Gemfile	Mon Sep 26 02:39:40 2016 +0900
@@ -14,5 +14,3 @@
 
 gem "puma", :require => false
 gem "unicorn", :require => false, :platforms => :ruby
-
-gem "embiggen"
--- a/Gemfile.lock	Fri Sep 23 23:10:54 2016 +0900
+++ b/Gemfile.lock	Mon Sep 26 02:39:40 2016 +0900
@@ -27,8 +27,6 @@
     dalli (2.7.6)
     domain_name (0.5.20160826)
       unf (>= 0.0.5, < 1.0.0)
-    embiggen (1.2.1)
-      addressable (~> 2.3)
     equalizer (0.0.10)
     erubis (2.7.0)
     faraday (0.9.2)
@@ -120,7 +118,6 @@
   actionpack (~> 5.0.0)
   actionview (~> 5.0.0)
   dalli
-  embiggen
   newrelic_rpm
   puma
   railties (~> 5.0.0)
--- a/app/helpers/application_helper.rb	Fri Sep 23 23:10:54 2016 +0900
+++ b/app/helpers/application_helper.rb	Mon Sep 26 02:39:40 2016 +0900
@@ -13,11 +13,20 @@
     "tag:#{request.host_with_port},2005:#{id}"
   end
 
-  def expand_url(text)
+  def expand_url(text, *urls)
+    urls.flatten!
+
+    urls = urls.reduce({}) do |result, u|
+      if u.try(:[], :url)
+        result[u[:url]] = u[:expanded_url]
+        @any = 1
+      end
+
+      result
+    end
+
     text.gsub /https?:\/\/t\.co\/[A-Za-z0-9]+/ do |url|
-      Rails.cache.fetch url, :expires_in => 1.hour do
-        " #{Embiggen::URI(url).expand} "
-      end
+      urls[url] || url
     end
   end
 end
--- a/app/views/tweets/_tweet.html.erb	Fri Sep 23 23:10:54 2016 +0900
+++ b/app/views/tweets/_tweet.html.erb	Mon Sep 26 02:39:40 2016 +0900
@@ -7,7 +7,14 @@
 <% else%>
   <p>
     <%# FIXME: Twitter gem doesn't support extended mode when writing this %>
-    <%= auto_link(expand_url(tweet.to_hash[:full_text])).gsub("\n", "<br>").html_safe %>
+    <%= auto_link(expand_url(
+          tweet.attrs[:full_text],
+          tweet.attrs[:entities][:urls],
+          tweet.attrs[:entities][:media]
+        ))
+        .gsub("\n", "<br>")
+        .html_safe
+      %>
   </p>
 
   <p>