minor refactoring

minor refactoring

Since ‘sanitize’ and ‘strip’ methods already called inside ‘truncate’ method removed the duplicates

diff --git a/lib/onebox/engine/google_docs_onebox.rb b/lib/onebox/engine/google_docs_onebox.rb
index 80e8fa8..3579a16 100644
--- a/lib/onebox/engine/google_docs_onebox.rb
+++ b/lib/onebox/engine/google_docs_onebox.rb
@@ -26,7 +26,7 @@ module Onebox
         og_data = get_og_data
         result = { link: link,
                    title: og_data[:title] || "Google #{shorttype.to_s.capitalize}",
-                   description: Sanitize.fragment(Onebox::Helpers.truncate(og_data[:description], 250)) || "This #{shorttype.to_s.chop.capitalize} is private",
+                   description: Onebox::Helpers.truncate(og_data[:description], 250) || "This #{shorttype.to_s.chop.capitalize} is private",
                    type: shorttype
                  }
         result
diff --git a/lib/onebox/engine/imgur_onebox.rb b/lib/onebox/engine/imgur_onebox.rb
index d087243..1561d9b 100644
--- a/lib/onebox/engine/imgur_onebox.rb
+++ b/lib/onebox/engine/imgur_onebox.rb
@@ -31,7 +31,7 @@ module Onebox
       def album_html(og)
         escaped_url = ::Onebox::Helpers.normalize_url_for_output(url)
         escaped_src = ::Onebox::Helpers.normalize_url_for_output(get_secure_link(og[:image]))
-        album_title = "[Album] #{Onebox::Helpers.sanitize(og[:title], 80)}"
+        album_title = "[Album] #{Onebox::Helpers.truncate(og[:title], 80)}"
 
         <<-HTML
             <div class='onebox imgur-album'>
diff --git a/lib/onebox/engine/instagram_onebox.rb b/lib/onebox/engine/instagram_onebox.rb
index c043925..a558963 100644
--- a/lib/onebox/engine/instagram_onebox.rb
+++ b/lib/onebox/engine/instagram_onebox.rb
@@ -20,7 +20,7 @@ module Onebox
 
         result = { link: og[:url],
                    title: html_entities.decode(Onebox::Helpers.truncate(title, 80)),
-                   description: html_entities.decode(Sanitize.fragment(Onebox::Helpers.truncate(og[:description].strip, 250)))
+                   description: html_entities.decode(Onebox::Helpers.truncate(og[:description], 250))
                   }
 
         if !Onebox::Helpers.blank?(og[:image])
diff --git a/lib/onebox/engine/whitelisted_generic_onebox.rb b/lib/onebox/engine/whitelisted_generic_onebox.rb
index cb4d5c7..70a06b4 100644
--- a/lib/onebox/engine/whitelisted_generic_onebox.rb
+++ b/lib/onebox/engine/whitelisted_generic_onebox.rb
@@ -202,16 +202,16 @@ module Onebox
           d = { link: link }.merge(raw)
 
           if !Onebox::Helpers.blank?(d[:title])
-            d[:title] = html_entities.decode(Onebox::Helpers.truncate(d[:title].strip, 80))
+            d[:title] = html_entities.decode(Onebox::Helpers.truncate(d[:title], 80))
           end
 
           d[:description] ||= d[:summary]
           if !Onebox::Helpers.blank?(d[:description])
-            d[:description] = html_entities.decode(Sanitize.fragment(Onebox::Helpers.truncate(d[:description].strip, 250)))
+            d[:description] = html_entities.decode(Onebox::Helpers.truncate(d[:description], 250))
           end
 
           if !Onebox::Helpers.blank?(d[:site_name])
-            d[:domain] = html_entities.decode(Onebox::Helpers.truncate(d[:site_name].strip, 80))
+            d[:domain] = html_entities.decode(Onebox::Helpers.truncate(d[:site_name], 80))
           elsif !Onebox::Helpers.blank?(d[:domain])
             d[:domain] = "http://#{d[:domain]}" unless d[:domain] =~ /^https?:\/\//
             d[:domain] = URI(d[:domain]).host.to_s.sub(/^www\./, '') rescue nil
@@ -231,22 +231,22 @@ module Onebox
 
           # Twitter labels
           if !Onebox::Helpers.blank?(d[:label1]) && !Onebox::Helpers.blank?(d[:data1]) && !!WhitelistedGenericOnebox.twitter_label_whitelist.find { |l| d[:label1] =~ /#{l}/i }
-            d[:label_1] = Sanitize.fragment(Onebox::Helpers.truncate(d[:label1].strip))
-            d[:data_1]  = Sanitize.fragment(Onebox::Helpers.truncate(d[:data1].strip))
+            d[:label_1] = Onebox::Helpers.truncate(d[:label1])
+            d[:data_1]  = Onebox::Helpers.truncate(d[:data1])
           end
           if !Onebox::Helpers.blank?(d[:label2]) && !Onebox::Helpers.blank?(d[:data2]) && !!WhitelistedGenericOnebox.twitter_label_whitelist.find { |l| d[:label2] =~ /#{l}/i }
             unless Onebox::Helpers.blank?(d[:label_1])
-              d[:label_2] = Sanitize.fragment(Onebox::Helpers.truncate(d[:label2].strip))
-              d[:data_2]  = Sanitize.fragment(Onebox::Helpers.truncate(d[:data2].strip))
+              d[:label_2] = Onebox::Helpers.truncate(d[:label2])
+              d[:data_2]  = Onebox::Helpers.truncate(d[:data2])
             else
-              d[:label_1] = Sanitize.fragment(Onebox::Helpers.truncate(d[:label2].strip))
-              d[:data_1]  = Sanitize.fragment(Onebox::Helpers.truncate(d[:data2].strip))
+              d[:label_1] = Onebox::Helpers.truncate(d[:label2])
+              d[:data_1]  = Onebox::Helpers.truncate(d[:data2])
             end
           end
 
           if Onebox::Helpers.blank?(d[:label_1]) && !Onebox::Helpers.blank?(d[:price_amount]) && !Onebox::Helpers.blank?(d[:price_currency])
             d[:label_1] = "Price"
-            d[:data_1] = Sanitize.fragment(Onebox::Helpers.truncate("#{d[:price_currency].strip} #{d[:price_amount].strip}"))
+            d[:data_1] = Onebox::Helpers.truncate("#{d[:price_currency].strip} #{d[:price_amount].strip}")
           end
 
           d
diff --git a/lib/onebox/engine/youtube_onebox.rb b/lib/onebox/engine/youtube_onebox.rb
index 71f4dc3..9b8b60f 100644
--- a/lib/onebox/engine/youtube_onebox.rb
+++ b/lib/onebox/engine/youtube_onebox.rb
@@ -50,7 +50,7 @@ module Onebox
 
       def video_title
         @video_title ||= begin
-          Sanitize.fragment(Onebox::Helpers.truncate(video_oembed_data[:title].strip, 80))
+          Onebox::Helpers.truncate(video_oembed_data[:title], 80)
         rescue
           nil
         end
diff --git a/lib/onebox/helpers.rb b/lib/onebox/helpers.rb
index f515e84..c530a2e 100644
--- a/lib/onebox/helpers.rb
+++ b/lib/onebox/helpers.rb
@@ -169,6 +169,7 @@ module Onebox
     end
 
     def self.truncate(string, length = 50)
+      string = sanitize(string)
       string.size > length ? string[0...(string.rindex(" ", length) || length)] + "..." : string
     end
 
@@ -183,7 +184,7 @@ module Onebox
 
     def self.sanitize(value, length = 50)
       return nil if blank?(value)
-      truncate(Sanitize.fragment(value).strip, length)
+      Sanitize.fragment(value).strip
     end
 
     def self.normalize_url_for_output(url)
diff --git a/spec/lib/onebox/helpers_spec.rb b/spec/lib/onebox/helpers_spec.rb
index 56d8d1f..850a0ba 100644
--- a/spec/lib/onebox/helpers_spec.rb
+++ b/spec/lib/onebox/helpers_spec.rb
@@ -22,7 +22,7 @@ RSpec.describe Onebox::Helpers do
     it { expect(described_class.truncate(test_string, 9)).to eq("Chops off...") }
     it { expect(described_class.truncate(test_string, 10)).to eq("Chops off...") }
     it { expect(described_class.truncate(test_string, 100)).to eq("Chops off on spaces") }
-    it { expect(described_class.truncate(" #{test_string} ", 6)).to eq(" Chops...") }
+    it { expect(described_class.truncate(" #{test_string} ", 6)).to eq("Chops...") }
   end
 
   describe "fetch_response" do

GitHub sha: ced22829