Skip to content

Commit

Permalink
Wrap google document in Document class
Browse files Browse the repository at this point in the history
This avoids having to have decoration logic (like formatting the date)
in multiple places.
  • Loading branch information
nicolas-fricke committed Nov 24, 2017
1 parent 2cbc315 commit d9d4151
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 53 deletions.
40 changes: 40 additions & 0 deletions app/document.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Document
def initialize(google_document)
@google_document = google_document
end

def id
@google_document.id
end

def title
@google_document.title
end

def modified_time
@google_document.modified_time
end

def modified_time_display
modified_time.strftime('%d %b %Y')
end

def last_author_name
@google_document.last_modifying_user.display_name
end

def html_body
article_json.to_html
end

private

def article_json
@article_json ||=
ArticleJSON::Article.from_google_doc_html(google_doc_html)
end

def google_doc_html
@google_document.export_as_string('html')
end
end
4 changes: 3 additions & 1 deletion app/google_drive_connector.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'google_drive'
require_relative 'config'
require_relative 'document'

class GoogleDriveConnector
attr_reader :session
Expand All @@ -15,13 +16,14 @@ def docs
.file_by_id(Config[:folder_id])
.files
.select { |file| file.resource_type == 'document' }
.map { |doc| Document.new(doc) }
.sort_by(&:modified_time)
.reverse
end

def doc(document_id)
document = session.file_by_id(document_id)
document if document.parents.include?(Config[:folder_id])
Document.new(document) if document.parents.include?(Config[:folder_id])
rescue Google::Apis::ClientError
nil
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% docs.each do |doc| %>
<li>
<a href="/<%= doc.id %>">
<%= doc.title %> (<%= doc.modified_time.strftime('%d %b %Y') %>)
<%= doc.title %> (<%= doc.modified_time_display %>)
</a>
</li>
<% end %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/show.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<h1><%= doc.title %></h1>
<div class="meta-info">
<cite><%= doc.last_modifying_user.display_name %></cite>
<cite><%= doc.last_author_name %></cite>
//
<time><%= doc.modified_time.strftime('%d %b %Y') %></time>
<time><%= doc.modified_time_display %></time>
</div>
<article>
<%= ArticleJSON::Article.from_google_doc_html(doc.export_as_string('html')).to_html %>
<%= doc.html_body %>
</article>
52 changes: 52 additions & 0 deletions spec/document_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'spec_helper'
require_relative '../app/document'

describe Document do
let(:user_double) { double 'user', display_name: 'Us Er' }
let(:modified_time) { DateTime.parse('2017-11-24T12:39:41') }
let(:google_doc_double) do
double 'document',
id: 'd0c_1d',
modified_time: modified_time,
title: 'My first document',
last_modifying_user: user_double
end
let(:instance) { described_class.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

describe '#id' do
subject { instance.id }
it { should eq google_doc_double.id }
end

describe '#title' do
subject { instance.title }
it { should eq google_doc_double.title }
end

describe '#modified_time' do
subject { instance.modified_time }
it { should eq google_doc_double.modified_time }
end

describe '#modified_time_display' do
subject { instance.modified_time_display }
it { should eq '24 Nov 2017' }
end

describe '#last_author_name' do
subject { instance.last_author_name }
it { should eq user_double.display_name }
end

describe '#html_body' do
subject { instance.html_body }
it { should be_a String }
it { should include "It's nice when it works!" }
end
end
20 changes: 20 additions & 0 deletions spec/fixtures/google_doc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<style type="text/css"></style>
</head>
<body style="background-color:#ffffff;padding:72pt 72pt 72pt 72pt;max-width:468pt">
<h1 id="h.rdp4m3ei7nk6" style="padding-top:20pt;margin:0;color:#000000;padding-left:0;font-size:20pt;padding-bottom:6pt;line-height:1.15;page-break-after:avoid;font-family:&quot;Arial&quot;;orphans:2;widows:2;text-align:left;padding-right:0">
<span style="color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:20pt;font-family:&quot;Arial&quot;;font-style:normal">Simple article</span>
</h1>
<p style="padding:0;margin:0;color:#000000;font-size:11pt;font-family:&quot;Arial&quot;;line-height:1.15;orphans:2;widows:2;height:11pt;text-align:left">
<span style="color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal"></span>
</p>
<p style="padding:0;margin:0;color:#000000;font-size:11pt;font-family:&quot;Arial&quot;;line-height:1.15;orphans:2;widows:2;text-align:left">
<span style="color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal">It's nice when it works!</span>
</p>
<p style="padding:0;margin:0;color:#000000;font-size:11pt;font-family:&quot;Arial&quot;;line-height:1.15;orphans:2;widows:2;height:11pt;text-align:left">
<span style="color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal"></span>
</p>
</body>
</html>
23 changes: 18 additions & 5 deletions spec/google_drive_connector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

let(:older_document_double) do
double 'older_document',
title: 'Older Document',
resource_type: 'document',
modified_time: DateTime.now - 3 # 3 days ago
end
let(:newer_document_double) do
double 'newer_document',
title: 'Newer Document',
resource_type: 'document',
modified_time: DateTime.now - 1 # 1 day ago
end
Expand All @@ -45,9 +47,15 @@
allow(folder_double).to receive(:files).and_return(file_doubles)
end

it { should be_an Array }
it { should contain_exactly older_document_double, newer_document_double }
it { should eq [newer_document_double, older_document_double] }
it 'should return the right documents' do
is_expected.to be_an Array
is_expected.to all be_a Document
titles = subject.map(&:title)
expect(titles).to contain_exactly older_document_double.title,
newer_document_double.title
expect(titles).to eq [newer_document_double.title,
older_document_double.title]
end
end

describe '#doc' do
Expand All @@ -56,7 +64,9 @@

context 'when the `doc_id` exists' do
let(:folder_id) { '123abc' }
let(:document_double) { double('document', parents: document_parents) }
let(:document_double) do
double('document', id: doc_id, parents: document_parents)
end

before do
allow(Config).to receive(:[]).with(:folder_id).and_return(folder_id)
Expand All @@ -67,7 +77,10 @@

context 'and it is in the right folder' do
let(:document_parents) { [folder_id] }
it { should eq document_double }
it 'should return the wrapped document' do
is_expected.to be_a Document
expect(subject.id).to eq doc_id
end
end

context 'but it is not in the right folder' do
Expand Down
68 changes: 25 additions & 43 deletions spec/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,32 @@ def app
end

describe 'GET /' do
let(:document_double_1) do
double 'document_1',
id: 'd0cum3nt-1',
modified_time: DateTime.now - 3, # 3 days ago
title: 'My first document'
let(:document_1) do
Document.new(
double 'document_1',
id: 'd0cum3nt-1',
modified_time: DateTime.now - 3, # 3 days ago
title: 'My first document'
)
end
let(:document_double_2) do
double 'document_2',
id: 'd0cum3nt-2',
modified_time: DateTime.now - 1, # 1 day ago
title: 'New stuff'
let(:document_2) do
Document.new(
double 'document_2',
id: 'd0cum3nt-2',
modified_time: DateTime.now - 1, # 1 day ago
title: 'New stuff'
)
end
let(:docs) { [document_double_1, document_double_2] }
let(:docs) { [document_1, document_2] }

before { allow(drive_connector_double).to receive(:docs).and_return(docs) }

it 'should return the blog index page' do
get '/'
expect(last_response).to be_successful
expect(last_response.body).to include blog_title
expect(last_response.body).to include document_double_1.title,
document_double_2.title
expect(last_response.body).to include document_1.title,
document_2.title
end
end

Expand All @@ -52,58 +56,36 @@ def app

before do
allow(drive_connector_double)
.to receive(:doc).with(doc_id).and_return(document_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(:document_double) do
double 'document_1',
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(:html) do
<<~HTML
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<style type="text/css"></style>
</head>
<body style="background-color:#ffffff;padding:72pt 72pt 72pt 72pt;max-width:468pt">
<h1 id="h.rdp4m3ei7nk6" style="padding-top:20pt;margin:0;color:#000000;padding-left:0;font-size:20pt;padding-bottom:6pt;line-height:1.15;page-break-after:avoid;font-family:&quot;Arial&quot;;orphans:2;widows:2;text-align:left;padding-right:0">
<span style="color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:20pt;font-family:&quot;Arial&quot;;font-style:normal">Simple article</span>
</h1>
<p style="padding:0;margin:0;color:#000000;font-size:11pt;font-family:&quot;Arial&quot;;line-height:1.15;orphans:2;widows:2;height:11pt;text-align:left">
<span style="color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal"></span>
</p>
<p style="padding:0;margin:0;color:#000000;font-size:11pt;font-family:&quot;Arial&quot;;line-height:1.15;orphans:2;widows:2;text-align:left">
<span style="color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal">It's nice when it works!</span>
</p>
<p style="padding:0;margin:0;color:#000000;font-size:11pt;font-family:&quot;Arial&quot;;line-height:1.15;orphans:2;widows:2;height:11pt;text-align:left">
<span style="color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:&quot;Arial&quot;;font-style:normal"></span>
</p>
</body>
</html>
HTML
end
let(:document) { Document.new(google_doc_double) }
let(:html) { File.read('spec/fixtures/google_doc.html') }

before do
allow(document_double)
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}"
expect(last_response).to be_successful
expect(last_response.body).to include document_double.title
expect(last_response.body).to include document.title
expect(last_response.body).to include "It's nice when it works!"
end
end

context 'when there is no document for the given ID' do
let(:document_double) { nil }
let(:document) { nil }

it 'should return a 404 error' do
get "/#{doc_id}"
Expand Down

0 comments on commit d9d4151

Please sign in to comment.