From e030a717b2e96744b47df8484a43bb0247c5fce8 Mon Sep 17 00:00:00 2001 From: Pat Walls Date: Fri, 28 Dec 2018 16:59:37 +0700 Subject: [PATCH] add embed functionality --- Gemfile | 2 ++ Gemfile.lock | 2 ++ app/views/i/posts/show.html.haml | 21 +++++++++++++++++++++ config/application.rb | 7 +++++++ 4 files changed, 32 insertions(+) diff --git a/Gemfile b/Gemfile index e159419..b132dc5 100644 --- a/Gemfile +++ b/Gemfile @@ -66,6 +66,8 @@ gem "paperclip", "~> 6.0.0" gem 'aws-sdk', '~> 3' +gem 'rack-cors', :require => 'rack/cors' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] diff --git a/Gemfile.lock b/Gemfile.lock index 67629f1..fc7657f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -804,6 +804,7 @@ GEM public_suffix (3.0.3) puma (3.12.0) rack (2.0.5) + rack-cors (1.0.2) rack-protection (2.0.4) rack rack-test (1.1.0) @@ -955,6 +956,7 @@ DEPENDENCIES paperclip (~> 6.0.0) pg puma (~> 3.11) + rack-cors rails (~> 5.2.0) rest-client sass-rails (~> 5.0) diff --git a/app/views/i/posts/show.html.haml b/app/views/i/posts/show.html.haml index 33a75d5..3889c09 100644 --- a/app/views/i/posts/show.html.haml +++ b/app/views/i/posts/show.html.haml @@ -12,3 +12,24 @@ %time.lighter{ datetime: @post.published }= @post.published&.strftime("%B #{@post.published.day.ordinalize}, %Y") %h1= @post.title = raw @parsed.html_safe + +:javascript + $(".article a").each(function() { + var node = this; + + if (this.text === "embed:tweet") { + $.ajax({ + url: `https://publish.twitter.com/oembed?url=${node.href}`, + type: "GET", + dataType: "jsonp", + success: function (response) { + var nodeToReplace = $(node) + nodeToReplace.parent().append($(response.html)) + nodeToReplace.remove() + }, + error: function (xhr, status) { + console.log("error"); + } + }); + } + }); diff --git a/config/application.rb b/config/application.rb index 885c2ac..01c1f4c 100644 --- a/config/application.rb +++ b/config/application.rb @@ -16,6 +16,13 @@ class Application < Rails::Application # -- all .rb files in that directory are automatically loaded after loading # the framework and any gems in your application. + config.middleware.insert_before 0, Rack::Cors do + allow do + origins '*' + resource '*', headers: :any, methods: [:get, :post, :options] + end + end + Raven.configure do |config| config.dsn = 'https://3286d54592144330be6af85fd527f76b:59774be85b094b60a21f2b98f3dd2e2a@sentry.io/1286413' end