Skip to content

Commit

Permalink
added nautilus component
Browse files Browse the repository at this point in the history
  • Loading branch information
Ch4s3 committed Oct 17, 2017
1 parent 21b49b0 commit 98c43bc
Show file tree
Hide file tree
Showing 12 changed files with 602 additions and 16 deletions.
8 changes: 2 additions & 6 deletions app/components/generic_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
class GenericClient
attr_reader :code, :response_body
include HTTParty
base_uri 'https://test.test'

# intialize method for the client that sets the article's path
#
# @param path [String] a uri path
# @param article_id [Integer] id of Article if one was already instanciated
def initialize(path, article_id = nil)
base_uri # this call ensures that base_uri is implemented
@path = path
@article_id = article_id
end

# This method gets the article and calls the builder, returning
# an article if one is built
# an article if one is built. It will fail if base_uri is not set
#
# @return [Article]
def article
Expand All @@ -30,10 +30,6 @@ def article

private

def base_uri
raise NotImplementedError, 'You must implement the base_uri method'
end

def call_builder
raise NotImplementedError, 'You must implement the call_builder method'
end
Expand Down
6 changes: 2 additions & 4 deletions app/components/long_reads/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ module LongReads
# HTTParty client for LongReads that takes an article path
# and get's the text of the article
class Client < GenericClient
private
base_uri 'longreads.com'

def base_uri
'longreads.com'
end
private

def call_builder
LongReads::Builder.new(@response_body, @path, @article_id).article
Expand Down
29 changes: 29 additions & 0 deletions app/components/nautilus/builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module Nautilus
# A builder for articles that come from Nautilus
class Builder < GenericBuilder
private

def publication
Publication.nautilus.first
end

def publication_url
'http://http://mitp.nautil.us/'
end

def article_title
@xml.css('.LongformA_textbody h2').text
end

def article_full_text
@xml.css('article').text
end

def article_published_on
# Inexact since Nautilus uses monthly publication
DateTime.parse(@xml.css('.general_datetype').text)
end
end
end
15 changes: 15 additions & 0 deletions app/components/nautilus/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Nautilus
# HTTParty client for Nautilus that takes an article path
# and get's the text of the article
class Client < GenericClient
base_uri 'mitp.nautil.us'

private

def call_builder
Nautilus::Builder.new(@response_body, @path, @article_id).article
end
end
end
6 changes: 2 additions & 4 deletions app/components/ny_times/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ module NyTimes
# HTTParty client for the NewYork Times that takes an article path
# and get's the text of the article
class Client < GenericClient
private
base_uri 'nytimes.com'

def base_uri
'nytimes.com'
end
private

def call_builder
NyTimes::Builder.new(@response_body, @path, @article_id).article
Expand Down
1 change: 1 addition & 0 deletions app/models/publication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ class Publication < ApplicationRecord
validates_uniqueness_of :name, :site
scope :new_york_times, -> { find_by(name: 'New York Times') }
scope :long_reads, -> { find_by(name: 'Long Reads') }
scope :nautilus, -> { find_by(name: 'Nautilus') }
end
5 changes: 5 additions & 0 deletions app/views/layouts/_nav_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
<li class='side-nav-link'>
<%= link_to('new article', new_article_path, 'arial-label': 'new article')%>
</li>
<%- if current_user.admin? %>
<li class='side-nav-link'>
<%= link_to('new publication', new_publication_path, 'arial-label': 'new publication')%>
</li>
<% end %>
<% end %>
<li class='side-nav-link'>
<%- if user_signed_in? %>
Expand Down
8 changes: 7 additions & 1 deletion app/workers/article_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
class ArticleWorker
include Sidekiq::Worker

# kicks off the `ArticleWorker`
#
# @param url [String] a url
# @param article_id [Integer|nil] id of Article if one was already instanciated
def perform(url, article_id = nil)
uri = URI(url)
client =
if uri.host =~ /nytimes.com/
NyTimes::Client.new(uri.path, article_id)
elsif uri.host =~ /longreads.com/
LongReads::Client.new(uri.path, article_id)
elsif uri.host =~ /nautil.us/
Nautilus::Client.new(uri.path, article_id)
end
client.article
client.try(:article)
end
end
15 changes: 15 additions & 0 deletions spec/components/nautilus/builder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

RSpec.describe Nautilus::Builder, type: :builder do
let!(:publication) { Publication.create(name: 'Nautilus') }

describe 'building an article' do
let(:response_body) { File.open('spec/fixtures/nautilus_article.html').read }
let(:path) { '/feature/257/how-to-obfuscate' }
let(:subject) { Nautilus::Builder.new(response_body, path) }

it 'creates a article in the database' do
expect(subject.article).to eq(Article.first)
end
end
end
30 changes: 30 additions & 0 deletions spec/components/nautilus/client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'rails_helper'

RSpec.describe Nautilus::Builder, type: :client do
let!(:publication) { Publication.create(name: 'New York Times') }

describe 'building an article' do
let(:response_body) { File.open('spec/fixtures/nautilus_article.html').read }
let(:url) { 'http://mitp.nautil.us/feature/257/how-to-obfuscate' }
let(:article_id) { nil }
let(:subject) { Nautilus::Client.new(url, article_id) }

context 'good response' do
let!(:stub) { stub_request(:any, /nautil/).to_return(body: response_body) }

it 'creates a article in the database' do
expect(subject.article.title).to eq(Article.first.title)
expect(subject.article.date).to eq(Article.first.date)
end
end

context '404 response' do
let!(:stub) { stub_request(:any, /nautil/).to_return(status: 404) }

it 'captures the code and does not create an article' do
expect(subject.article).to eq(nil)
expect(subject.code).to eq(404)
end
end
end
end

0 comments on commit 98c43bc

Please sign in to comment.