From 642e5ab45b1a011ac0ad6ff63f4786e2c2d51d30 Mon Sep 17 00:00:00 2001 From: jaideng123 Date: Sat, 6 Sep 2014 14:44:54 -0500 Subject: [PATCH 1/2] Fixed Open Graph db insertion --- app/models/open_graph_cache.rb | 2 +- db/migrate/20140906192846_fix_open_graph_data.rb | 10 ++++++++++ db/schema.rb | 8 ++++---- 3 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20140906192846_fix_open_graph_data.rb diff --git a/app/models/open_graph_cache.rb b/app/models/open_graph_cache.rb index 8c6e32841..d0c94362e 100644 --- a/app/models/open_graph_cache.rb +++ b/app/models/open_graph_cache.rb @@ -26,7 +26,7 @@ class OpenGraphCache < ActiveRecord::Base return if response.blank? || response.type.blank? - self.title = response.title + self.title = response.title.truncate(255) self.ob_type = response.type self.image = response.images[0] self.url = response.url diff --git a/db/migrate/20140906192846_fix_open_graph_data.rb b/db/migrate/20140906192846_fix_open_graph_data.rb new file mode 100644 index 000000000..54959931d --- /dev/null +++ b/db/migrate/20140906192846_fix_open_graph_data.rb @@ -0,0 +1,10 @@ +class FixOpenGraphData < ActiveRecord::Migration + def self.up + change_column :open_graph_caches, :url, :text + change_column :open_graph_caches, :image, :text + end + def self.down + change_column :open_graph_caches, :url, :string + change_column :open_graph_caches, :image, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index aa8406d86..80202933c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140826165533) do +ActiveRecord::Schema.define(version: 20140906192846) do create_table "account_deletions", force: true do |t| t.string "diaspora_handle" @@ -219,8 +219,8 @@ ActiveRecord::Schema.define(version: 20140826165533) do create_table "open_graph_caches", force: true do |t| t.string "title" t.string "ob_type" - t.string "image" - t.string "url" + t.text "image" + t.text "url" t.text "description" end @@ -570,4 +570,4 @@ ActiveRecord::Schema.define(version: 20140826165533) do add_foreign_key "share_visibilities", "contacts", name: "post_visibilities_contact_id_fk", dependent: :delete -end +end \ No newline at end of file From d16eabae21f98ba570bf652cffadb3e82c6955e8 Mon Sep 17 00:00:00 2001 From: jaideng123 Date: Sun, 7 Sep 2014 20:28:02 -0500 Subject: [PATCH 2/2] Added test for truncation to gather OG data spec --- spec/workers/gather_open_graph_data_spec.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/spec/workers/gather_open_graph_data_spec.rb b/spec/workers/gather_open_graph_data_spec.rb index 7c2397e4f..3416047c4 100644 --- a/spec/workers/gather_open_graph_data_spec.rb +++ b/spec/workers/gather_open_graph_data_spec.rb @@ -16,12 +16,25 @@ describe Workers::GatherOpenGraphData do " + @oglong_title = "D" * 256 + @oglong_url = 'http://www.we-are-too-long.com' + @oglong_body = + "#{@oglong_title} + + + + + + " + @no_open_graph_url = 'http://www.we-do-not-support-open-graph.com/index.html' @status_message = FactoryGirl.create(:status_message) stub_request(:get, @ogsite_url).to_return(:status => 200, :body => @ogsite_body) stub_request(:get, @no_open_graph_url).to_return(:status => 200, :body => 'hello there') + stub_request(:get, @oglong_url).to_return(:status => 200, :body => @oglong_body) + end describe '.perform' do @@ -65,5 +78,11 @@ describe Workers::GatherOpenGraphData do Workers::GatherOpenGraphData.new.perform(0, @ogsite_url) }.to_not raise_error end + it 'truncates + inserts titles that are too long' do + Workers::GatherOpenGraphData.new.perform(@status_message.id, @oglong_url) + ogc = OpenGraphCache.find_by_url(@oglong_url) + expect(ogc).to be_truthy + expect(ogc.title.length).to be <= 255 + end end end