From 43a02c828ab84b110724ad1cfcf14b281a2cd4de Mon Sep 17 00:00:00 2001 From: Nicolas Fricke Date: Fri, 24 Nov 2017 14:54:24 +0100 Subject: [PATCH] Add AMP view under `/:document_id/amp` This renders the article as an AMP friendly format --- app/document.rb | 15 ++++++++- app/server.rb | 13 +++++++- app/views/layout.erb | 3 ++ app/views/show.amp.erb | 9 ++++++ app/views/{show.erb => show.html.erb} | 0 spec/document_spec.rb | 14 +++++++++ spec/server_spec.rb | 45 +++++++++++++++++++++++++++ 7 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 app/views/show.amp.erb rename app/views/{show.erb => show.html.erb} (100%) diff --git a/app/document.rb b/app/document.rb index 3bac225..e7895f0 100644 --- a/app/document.rb +++ b/app/document.rb @@ -1,3 +1,5 @@ +require 'article_json' + class Document def initialize(google_document) @google_document = google_document @@ -27,11 +29,22 @@ def html_body article_json.to_html end + def amp_body + article_json.to_amp + end + + def amp_libraries + [ + '', + *article_json.amp_exporter.amp_libraries + ] + end + private def article_json @article_json ||= - ArticleJSON::Article.from_google_doc_html(google_doc_html) + ::ArticleJSON::Article.from_google_doc_html(google_doc_html) end def google_doc_html diff --git a/app/server.rb b/app/server.rb index 13f47c2..62447af 100644 --- a/app/server.rb +++ b/app/server.rb @@ -15,9 +15,20 @@ get '/:document_id' do doc = GoogleDriveConnector.new.doc(params[:document_id]) halt 404 unless doc - erb :show, + erb :'show.html', locals: { blog_title: Config[:blog_title], doc: doc, } end + +get '/:document_id/amp' do + doc = GoogleDriveConnector.new.doc(params[:document_id]) + halt 404 unless doc + erb :'show.amp', + locals: { + blog_title: Config[:blog_title], + doc: doc, + header_tags: doc.amp_libraries.join(''), + } +end diff --git a/app/views/layout.erb b/app/views/layout.erb index 2a30845..ff19923 100644 --- a/app/views/layout.erb +++ b/app/views/layout.erb @@ -5,6 +5,9 @@ <%= Config[:blog_title] %> + <% if local_variables.include?(:header_tags) %> + <%= header_tags %> + <% end %>
diff --git a/app/views/show.amp.erb b/app/views/show.amp.erb new file mode 100644 index 0000000..e12e8c8 --- /dev/null +++ b/app/views/show.amp.erb @@ -0,0 +1,9 @@ +

<%= doc.title %>

+
+ <%= doc.last_author_name %> + // + +
+
+ <%= doc.amp_body %> +
diff --git a/app/views/show.erb b/app/views/show.html.erb similarity index 100% rename from app/views/show.erb rename to app/views/show.html.erb diff --git a/spec/document_spec.rb b/spec/document_spec.rb index db42fb4..76070a3 100644 --- a/spec/document_spec.rb +++ b/spec/document_spec.rb @@ -49,4 +49,18 @@ it { should be_a String } it { should include "It's nice when it works!" } end + + describe '#amp_body' do + subject { instance.amp_body } + it { should be_a String } + it { should include "It's nice when it works!" } + end + + describe '#amp_libraries' do + subject { instance.amp_libraries } + it { should be_an Array } + it { should all be_a String } + it { should all start_with '' } + end end diff --git a/spec/server_spec.rb b/spec/server_spec.rb index d6affb5..cedc5e8 100644 --- a/spec/server_spec.rb +++ b/spec/server_spec.rb @@ -94,4 +94,49 @@ def app end end end + + describe 'GET /:document_id/amp' do + let(:doc_id) { 'd0cum3nt' } + + before do + allow(drive_connector_double) + .to receive(:doc).with(doc_id).and_return(document) + end + + context 'when there is a document for the given ID' do + let(:user_double) { double 'user', display_name: 'Us Er' } + let(:google_doc_double) do + double 'document', + id: doc_id, + modified_time: DateTime.now - 3, # 3 days ago + title: 'My first document', + last_modifying_user: user_double + end + let(:document) { Document.new(google_doc_double) } + let(:html) { File.read('spec/fixtures/google_doc.html') } + + before do + allow(google_doc_double) + .to receive(:export_as_string).with('html').and_return(html) + end + + it 'should return the article page for the given document' do + get "/#{doc_id}/amp" + expect(last_response).to be_successful + expect(last_response.body).to include document.title + expect(last_response.body).to include "It's nice when it works!" + expect(last_response.body).to include 'https://cdn.ampproject.org/v0.js' + end + end + + context 'when there is no document for the given ID' do + let(:document) { nil } + + it 'should return a 404 error' do + get "/#{doc_id}" + expect(last_response).to_not be_successful + expect(last_response).to be_not_found + end + end + end end