From 51334abf32b80fe1851df38b8764074a5242c4e0 Mon Sep 17 00:00:00 2001 From: wagurano Date: Wed, 30 Jun 2021 23:28:10 +0900 Subject: [PATCH 1/5] =?UTF-8?q?=EC=98=A4=ED=94=88=20=EA=B7=B8=EB=9E=98?= =?UTF-8?q?=ED=94=84=20=EC=A0=AC(ogp)=EB=A5=BC=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=ED=95=A9=EB=8B=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 3 +++ Gemfile.lock | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 908d41e..a726e1e 100644 --- a/Gemfile +++ b/Gemfile @@ -54,6 +54,9 @@ end #gem 'wicked_pdf' #gem 'wkhtmltopdf-binary' +# open graph +gem 'ogp', '~> 0.4.0' + # excel gem 'axlsx', '2.1.0.pre' gem 'axlsx_rails' diff --git a/Gemfile.lock b/Gemfile.lock index 6c57bd1..4c7fefa 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -51,9 +51,11 @@ GEM sshkit (>= 1.6.1, != 1.7.0) ancestry (3.0.7) activerecord (>= 3.2.0) + ansi (1.5.0) archive-zip (0.12.0) io-like (~> 0.3.0) arel (9.0.0) + ast (2.4.2) autoprefixer-rails (9.7.2) execjs aws-eventstream (1.1.0) @@ -1125,6 +1127,11 @@ GEM nokogiri (1.11.2) mini_portile2 (~> 2.5.0) racc (~> 1.4) + oga (2.15) + ast + ruby-ll (~> 2.1) + ogp (0.4.0) + oga (~> 2.15) orm_adapter (0.5.0) pg (1.1.4) popper_js (1.14.5) @@ -1180,6 +1187,9 @@ GEM responders (3.0.0) actionpack (>= 5.0) railties (>= 5.0) + ruby-ll (2.1.2) + ansi + ast ruby-vips (2.0.16) ffi (~> 1.9) ruby_dep (1.5.0) @@ -1308,6 +1318,7 @@ DEPENDENCIES letter_opener_web listen (>= 3.0.5, < 3.2) mini_magick (~> 4.8) + ogp (~> 0.4.0) pg puma (~> 3.11) rails (~> 5.2.5) @@ -1337,4 +1348,4 @@ RUBY VERSION ruby 2.6.6p146 BUNDLED WITH - 2.2.6 + 2.2.14 From bfd046973f94322ab69aa1c25b4ba3722d42b423 Mon Sep 17 00:00:00 2001 From: wagurano Date: Wed, 30 Jun 2021 23:28:55 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=EB=B8=8C=EB=9E=9C=EC=B9=98=EC=97=90=20?= =?UTF-8?q?=EB=94=B0=EB=9D=BC=20=EB=8D=B0=EC=9D=B4=ED=84=B0=EB=B2=A0?= =?UTF-8?q?=EC=9D=B4=EC=8A=A4=EB=A5=BC=20=EC=82=AC=EC=9A=A9=ED=95=A9?= =?UTF-8?q?=EB=8B=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/database.yml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/config/database.yml b/config/database.yml index 0ca0072..f52267e 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,9 +1,3 @@ -# SQLite version 3.x -# gem install sqlite3 -# -# Ensure the SQLite 3 gem is defined in your Gemfile -# gem 'sqlite3' -# default: &default adapter: sqlite3 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> @@ -11,7 +5,7 @@ default: &default development: adapter: postgresql - database: datapublic + database: datapublic_development_<%= %x[git rev-parse --abbrev-ref HEAD].strip %> encoding: unicode host: <%= ENV.fetch("DATABASE_HOST") {"localhost"} %> <% if(ENV["DATABASE_USERNAME"] != nil) %> From 6f9dd634296b13db69806791c63a4f5dd28c12f7 Mon Sep 17 00:00:00 2001 From: wagurano Date: Wed, 30 Jun 2021 23:29:49 +0900 Subject: [PATCH 3/5] =?UTF-8?q?=EC=86=8C=EC=8B=9D(post)=20=EC=BB=AC?= =?UTF-8?q?=EB=9F=BC=EC=9C=BC=EB=A1=9C=20=EB=AF=B8=EB=A6=AC=EB=B3=B4?= =?UTF-8?q?=EA=B8=B0=20=EB=A7=81=ED=81=AC=EB=A5=BC=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=ED=95=A9=EB=8B=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/migrate/20210630134855_add_preview_link_to_posts.rb | 5 +++++ db/schema.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20210630134855_add_preview_link_to_posts.rb diff --git a/db/migrate/20210630134855_add_preview_link_to_posts.rb b/db/migrate/20210630134855_add_preview_link_to_posts.rb new file mode 100644 index 0000000..16365c0 --- /dev/null +++ b/db/migrate/20210630134855_add_preview_link_to_posts.rb @@ -0,0 +1,5 @@ +class AddPreviewLinkToPosts < ActiveRecord::Migration[5.2] + def change + add_column :posts, :preview_link, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index fb5dea2..5f9884c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_11_16_105613) do +ActiveRecord::Schema.define(version: 2021_06_30_134855) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -92,6 +92,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.bigint "archive_id" + t.string "preview_link" t.index ["archive_id"], name: "index_posts_on_archive_id" t.index ["user_id"], name: "index_posts_on_user_id" end From 787fd685b5ac087357542d7e3e90e050f741e07e Mon Sep 17 00:00:00 2001 From: wagurano Date: Wed, 30 Jun 2021 23:30:16 +0900 Subject: [PATCH 4/5] =?UTF-8?q?=EB=AF=B8=EB=A6=AC=EB=B3=B4=EA=B8=B0=20?= =?UTF-8?q?=EB=A7=81=ED=81=AC=EB=A5=BC=20=EC=B6=94=EA=B0=80=ED=95=A9?= =?UTF-8?q?=EB=8B=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/posts_controller.rb | 3 ++- app/views/posts/_form.html.haml | 1 + app/views/posts/show.html.haml | 7 +++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 85759be..e8818ea 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -10,6 +10,7 @@ def index # GET /posts/1 # GET /posts/1.json def show + @post_og = OGP::OpenGraph.new(Net::HTTP.get_response(URI(@post.preview_link))&.body)&.data if @post.preview_link end # GET /posts/new @@ -75,6 +76,6 @@ def set_post # Never trust parameters from the scary internet, only allow the white list through. def post_params - params.require(:post).permit(:archive_id, :title, :cover, :body) + params.require(:post).permit(:archive_id, :title, :cover, :body, :preview_link) end end diff --git a/app/views/posts/_form.html.haml b/app/views/posts/_form.html.haml index 614356a..8ffc2df 100644 --- a/app/views/posts/_form.html.haml +++ b/app/views/posts/_form.html.haml @@ -6,4 +6,5 @@ = f.input :title = f.input :cover = f.input :body, as: "summernote" + = f.input :preview_link = f.submit data: { disable_with: "Please wait..." }, class: 'btn btn-primary btn-full py-3' diff --git a/app/views/posts/show.html.haml b/app/views/posts/show.html.haml index 8b3a56a..5ab935d 100644 --- a/app/views/posts/show.html.haml +++ b/app/views/posts/show.html.haml @@ -13,6 +13,13 @@ .my-4 .body-content = raw @post.body + - if @post_og + .row + = @post_og['title'] + .row + = @post_og['description'] + .row + = image_tag @post_og['image'] - if @post.archive.present? %section From 091bac31a9c72a2e0186f09296bf0d2e3a9d2426 Mon Sep 17 00:00:00 2001 From: wagurano Date: Thu, 22 Jul 2021 00:56:15 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=EB=AF=B8=EB=A6=AC=EB=B3=B4=EA=B8=B0=20?= =?UTF-8?q?=EB=A7=81=ED=81=AC=EB=A5=BC=20=EA=B0=80=EC=A0=B8=EC=98=A4?= =?UTF-8?q?=EB=8A=94=20=EC=9E=91=EC=97=85=EC=9D=84=20=EB=B0=B1=EA=B7=B8?= =?UTF-8?q?=EB=9D=BC=EC=9A=B4=EB=93=9C=EB=A1=9C=20=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=ED=95=A9=EB=8B=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 1 + Gemfile.lock | 4 ++++ app/controllers/posts_controller.rb | 4 +++- app/jobs/crawling_job.rb | 12 ++++++++++++ app/models/preview_link.rb | 2 ++ app/views/posts/show.html.haml | 16 +++++++++------- .../20210717054410_create_preview_links.rb | 12 ++++++++++++ db/schema.rb | 11 ++++++++++- test/fixtures/preview_links.yml | 9 +++++++++ test/jobs/crawling_job_job_test.rb | 7 +++++++ test/models/preview_link_test.rb | 7 +++++++ 11 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 app/jobs/crawling_job.rb create mode 100644 app/models/preview_link.rb create mode 100644 db/migrate/20210717054410_create_preview_links.rb create mode 100644 test/fixtures/preview_links.yml create mode 100644 test/jobs/crawling_job_job_test.rb create mode 100644 test/models/preview_link_test.rb diff --git a/Gemfile b/Gemfile index a726e1e..861ca94 100644 --- a/Gemfile +++ b/Gemfile @@ -56,6 +56,7 @@ end # open graph gem 'ogp', '~> 0.4.0' +gem 'faraday', '~> 0.9.2' # excel gem 'axlsx', '2.1.0.pre' diff --git a/Gemfile.lock b/Gemfile.lock index 4c7fefa..df34c65 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1044,6 +1044,8 @@ GEM execjs (2.7.0) faker (1.7.3) i18n (~> 0.5) + faraday (0.9.2) + multipart-post (>= 1.2, < 3) ffi (1.11.3) fugit (1.3.3) et-orbi (~> 1.1, >= 1.1.8) @@ -1120,6 +1122,7 @@ GEM mini_portile2 (2.5.0) minitest (5.14.4) msgpack (1.3.1) + multipart-post (2.1.1) net-scp (2.0.0) net-ssh (>= 2.6.5, < 6.0.0) net-ssh (5.2.0) @@ -1307,6 +1310,7 @@ DEPENDENCIES devise dotenv-rails faker (~> 1.7.3) + faraday (~> 0.9.2) haml-rails (~> 1.0) health_check http_accept_language diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index e8818ea..8d0fc9f 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -10,7 +10,7 @@ def index # GET /posts/1 # GET /posts/1.json def show - @post_og = OGP::OpenGraph.new(Net::HTTP.get_response(URI(@post.preview_link))&.body)&.data if @post.preview_link + @preview_link = PreviewLink.find_by_url(@post.preview_link) end # GET /posts/new @@ -47,6 +47,8 @@ def create # PATCH/PUT /posts/1 # PATCH/PUT /posts/1.json def update + CrawlingJob.perform_later(@post.preview_link) if @post.preview_link + respond_to do |format| if @post.update(post_params) format.html { redirect_to @post, notice: 'Post was successfully updated.' } diff --git a/app/jobs/crawling_job.rb b/app/jobs/crawling_job.rb new file mode 100644 index 0000000..3a75ee0 --- /dev/null +++ b/app/jobs/crawling_job.rb @@ -0,0 +1,12 @@ +class CrawlingJob < ApplicationJob + queue_as :default + + def perform(url) + link = PreviewLink.find_by_url(url) + unless link.present? + ogp = OGP::OpenGraph.new(Faraday.get(url)&.body) + link = PreviewLink.create(url: url, title: ogp.title, description: ogp.description, image: ogp.image.url) + end + logger.info "#{link.inspect}" + end +end diff --git a/app/models/preview_link.rb b/app/models/preview_link.rb new file mode 100644 index 0000000..174a45a --- /dev/null +++ b/app/models/preview_link.rb @@ -0,0 +1,2 @@ +class PreviewLink < ApplicationRecord +end diff --git a/app/views/posts/show.html.haml b/app/views/posts/show.html.haml index 5ab935d..8ac81c2 100644 --- a/app/views/posts/show.html.haml +++ b/app/views/posts/show.html.haml @@ -13,13 +13,15 @@ .my-4 .body-content = raw @post.body - - if @post_og - .row - = @post_og['title'] - .row - = @post_og['description'] - .row - = image_tag @post_og['image'] + - if @preview_link.present? + = link_to @preview_link.url do + .preview-link-card{ style: "display: block; width: 438px; max-width: 100%; border-radius: .85714em; border: 1px solid #e1e8ed; overflow: hidden; line-height: 1.3em;" } + .card-cover-image{ style: "background-image: url(#{@preview_link.image}); background-size: cover; height: 220px; border-bottom: 1px solid #e1e8ed;" } + .card-meta{ style: "box-sizing: border-box; padding: .75em; text-decoration: none;"} + .card-meta-title{ style: "display: block; border-collapse: separate; text-align: left; word-wrap: break-word;" } + = @preview_link.title + .card-meta-description{ style: "max-height: 2.6em; margin-top: .32333em; overflow: hidden" } + = @preview_link.description - if @post.archive.present? %section diff --git a/db/migrate/20210717054410_create_preview_links.rb b/db/migrate/20210717054410_create_preview_links.rb new file mode 100644 index 0000000..a0f2016 --- /dev/null +++ b/db/migrate/20210717054410_create_preview_links.rb @@ -0,0 +1,12 @@ +class CreatePreviewLinks < ActiveRecord::Migration[5.2] + def change + create_table :preview_links do |t| + t.string :url + t.string :title + t.text :description + t.string :image + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 5f9884c..8a2c60b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_06_30_134855) do +ActiveRecord::Schema.define(version: 2021_07_17_054410) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -97,6 +97,15 @@ t.index ["user_id"], name: "index_posts_on_user_id" end + create_table "preview_links", force: :cascade do |t| + t.string "url" + t.string "title" + t.text "description" + t.string "image" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "rumors", force: :cascade do |t| t.string "title" t.text "body" diff --git a/test/fixtures/preview_links.yml b/test/fixtures/preview_links.yml new file mode 100644 index 0000000..97fbfc4 --- /dev/null +++ b/test/fixtures/preview_links.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + description: MyText + +two: + title: MyString + description: MyText diff --git a/test/jobs/crawling_job_job_test.rb b/test/jobs/crawling_job_job_test.rb new file mode 100644 index 0000000..fe9b9fd --- /dev/null +++ b/test/jobs/crawling_job_job_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class CrawlingJobJobTest < ActiveJob::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/preview_link_test.rb b/test/models/preview_link_test.rb new file mode 100644 index 0000000..b95f597 --- /dev/null +++ b/test/models/preview_link_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class PreviewLinkTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end