Skip to content

Commit

Permalink
Add AMP view under /:document_id/amp
Browse files Browse the repository at this point in the history
This renders the article as an AMP friendly format
  • Loading branch information
nicolas-fricke committed Nov 24, 2017
1 parent d9d4151 commit 43a02c8
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 2 deletions.
15 changes: 14 additions & 1 deletion app/document.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'article_json'

class Document
def initialize(google_document)
@google_document = google_document
Expand Down Expand Up @@ -27,11 +29,22 @@ def html_body
article_json.to_html
end

def amp_body
article_json.to_amp
end

def amp_libraries
[
'<script async="" src="https://cdn.ampproject.org/v0.js"></script>',
*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
Expand Down
13 changes: 12 additions & 1 deletion app/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions app/views/layout.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><%= Config[:blog_title] %></title>
<% if local_variables.include?(:header_tags) %>
<%= header_tags %>
<% end %>
</head>
<body>
<div class="content">
Expand Down
9 changes: 9 additions & 0 deletions app/views/show.amp.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1><%= doc.title %></h1>
<div class="meta-info">
<cite><%= doc.last_author_name %></cite>
//
<time><%= doc.modified_time_display %></time>
</div>
<article>
<%= doc.amp_body %>
</article>
File renamed without changes.
14 changes: 14 additions & 0 deletions spec/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<script' }
it { should all end_with '</script>' }
end
end
45 changes: 45 additions & 0 deletions spec/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 43a02c8

Please sign in to comment.