Skip to content

Commit

Permalink
F2: Test coverage for the post publisher (#597)
Browse files Browse the repository at this point in the history
* Update gems (includes Rails 8.0.0.rc2)

* Remove redundant requires

* Test post publisher happy path

* Disable Rubocop standard temporarily

* Simplify downloader errors handling

* Define default HTTP client

* Fix class name

* Bypass exceptions

* Add a comment

* Test coverage

* Remove logging from the service

* Test comments publication

* Test errors handling

* Rubocop

* Fix a test
  • Loading branch information
dreikanter authored Nov 2, 2024
1 parent e784045 commit 064756e
Show file tree
Hide file tree
Showing 18 changed files with 330 additions and 116 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require:
- standard
# - standard
- rubocop-performance
- rubocop-rails
- rubocop-rspec
Expand Down
147 changes: 73 additions & 74 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,65 @@ GEM
specs:
aasm (5.5.0)
concurrent-ruby (~> 1.0)
actioncable (8.0.0.beta1)
actionpack (= 8.0.0.beta1)
activesupport (= 8.0.0.beta1)
actioncable (8.0.0.rc2)
actionpack (= 8.0.0.rc2)
activesupport (= 8.0.0.rc2)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
zeitwerk (~> 2.6)
actionmailbox (8.0.0.beta1)
actionpack (= 8.0.0.beta1)
activejob (= 8.0.0.beta1)
activerecord (= 8.0.0.beta1)
activestorage (= 8.0.0.beta1)
activesupport (= 8.0.0.beta1)
actionmailbox (8.0.0.rc2)
actionpack (= 8.0.0.rc2)
activejob (= 8.0.0.rc2)
activerecord (= 8.0.0.rc2)
activestorage (= 8.0.0.rc2)
activesupport (= 8.0.0.rc2)
mail (>= 2.8.0)
actionmailer (8.0.0.beta1)
actionpack (= 8.0.0.beta1)
actionview (= 8.0.0.beta1)
activejob (= 8.0.0.beta1)
activesupport (= 8.0.0.beta1)
actionmailer (8.0.0.rc2)
actionpack (= 8.0.0.rc2)
actionview (= 8.0.0.rc2)
activejob (= 8.0.0.rc2)
activesupport (= 8.0.0.rc2)
mail (>= 2.8.0)
rails-dom-testing (~> 2.2)
actionpack (8.0.0.beta1)
actionview (= 8.0.0.beta1)
activesupport (= 8.0.0.beta1)
actionpack (8.0.0.rc2)
actionview (= 8.0.0.rc2)
activesupport (= 8.0.0.rc2)
nokogiri (>= 1.8.5)
rack (>= 2.2.4)
rack-session (>= 1.0.1)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
useragent (~> 0.16)
actiontext (8.0.0.beta1)
actionpack (= 8.0.0.beta1)
activerecord (= 8.0.0.beta1)
activestorage (= 8.0.0.beta1)
activesupport (= 8.0.0.beta1)
actiontext (8.0.0.rc2)
actionpack (= 8.0.0.rc2)
activerecord (= 8.0.0.rc2)
activestorage (= 8.0.0.rc2)
activesupport (= 8.0.0.rc2)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
actionview (8.0.0.beta1)
activesupport (= 8.0.0.beta1)
actionview (8.0.0.rc2)
activesupport (= 8.0.0.rc2)
builder (~> 3.1)
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
activejob (8.0.0.beta1)
activesupport (= 8.0.0.beta1)
activejob (8.0.0.rc2)
activesupport (= 8.0.0.rc2)
globalid (>= 0.3.6)
activemodel (8.0.0.beta1)
activesupport (= 8.0.0.beta1)
activerecord (8.0.0.beta1)
activemodel (= 8.0.0.beta1)
activesupport (= 8.0.0.beta1)
activemodel (8.0.0.rc2)
activesupport (= 8.0.0.rc2)
activerecord (8.0.0.rc2)
activemodel (= 8.0.0.rc2)
activesupport (= 8.0.0.rc2)
timeout (>= 0.4.0)
activestorage (8.0.0.beta1)
actionpack (= 8.0.0.beta1)
activejob (= 8.0.0.beta1)
activerecord (= 8.0.0.beta1)
activesupport (= 8.0.0.beta1)
activestorage (8.0.0.rc2)
actionpack (= 8.0.0.rc2)
activejob (= 8.0.0.rc2)
activerecord (= 8.0.0.rc2)
activesupport (= 8.0.0.rc2)
marcel (~> 1.0)
activesupport (8.0.0.beta1)
activesupport (8.0.0.rc2)
base64
benchmark (>= 0.3)
bigdecimal
Expand All @@ -85,7 +85,7 @@ GEM
bigdecimal (3.1.8)
bootsnap (1.18.4)
msgpack (~> 1.2)
brakeman (6.2.1)
brakeman (6.2.2)
racc
builder (3.3.0)
bundler-audit (0.9.2)
Expand All @@ -99,16 +99,17 @@ GEM
bigdecimal
rexml
crass (1.0.6)
date (3.3.4)
date (3.4.0)
diff-lcs (1.5.1)
docile (1.4.1)
domain_name (0.6.20240107)
drb (2.2.1)
dry-configurable (1.2.0)
dry-core (~> 1.0, < 2)
zeitwerk (~> 2.6)
dry-core (1.0.1)
dry-core (1.0.2)
concurrent-ruby (~> 1.0)
logger
zeitwerk (~> 2.6)
dry-inflector (1.1.0)
dry-initializer (3.1.1)
Expand All @@ -134,8 +135,8 @@ GEM
erubi (1.13.0)
factory_bot (6.5.0)
activesupport (>= 5.0.0)
factory_bot_rails (6.4.3)
factory_bot (~> 6.4)
factory_bot_rails (6.4.4)
factory_bot (~> 6.5)
railties (>= 5.0.0)
ffi (1.17.0-aarch64-linux-gnu)
ffi (1.17.0-arm-linux-gnu)
Expand All @@ -149,7 +150,7 @@ GEM
globalid (1.2.1)
activesupport (>= 6.1)
hashdiff (1.1.1)
honeybadger (5.16.0)
honeybadger (5.19.1)
logger
http (5.2.0)
addressable (~> 2.8)
Expand All @@ -168,14 +169,14 @@ GEM
reline (>= 0.4.2)
jsbundling-rails (1.3.1)
railties (>= 6.0.0)
json (2.7.2)
json (2.7.5)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
llhttp-ffi (0.5.0)
ffi-compiler (~> 1.0)
rake (~> 13.0)
logger (1.6.1)
loofah (2.22.0)
loofah (2.23.1)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
mail (2.8.1)
Expand All @@ -201,7 +202,7 @@ GEM
timeout
net-smtp (0.5.0)
net-protocol
nio4r (2.7.3)
nio4r (2.7.4)
nokogiri (1.16.7-aarch64-linux)
racc (~> 1.4)
nokogiri (1.16.7-arm-linux)
Expand All @@ -217,12 +218,12 @@ GEM
optimist (3.1.0)
ostruct (0.6.0)
parallel (1.26.3)
parser (3.3.5.0)
parser (3.3.5.1)
ast (~> 2.4.1)
racc
patience_diff (1.2.0)
optimist (~> 3.0)
pg (1.5.8)
pg (1.5.9)
propshaft (1.1.0)
actionpack (>= 7.0.0)
activesupport (>= 7.0.0)
Expand All @@ -247,33 +248,32 @@ GEM
rack (>= 3.0.0)
rack-test (2.1.0)
rack (>= 1.3)
rackup (2.1.0)
rackup (2.2.0)
rack (>= 3)
webrick (~> 1.8)
rails (8.0.0.beta1)
actioncable (= 8.0.0.beta1)
actionmailbox (= 8.0.0.beta1)
actionmailer (= 8.0.0.beta1)
actionpack (= 8.0.0.beta1)
actiontext (= 8.0.0.beta1)
actionview (= 8.0.0.beta1)
activejob (= 8.0.0.beta1)
activemodel (= 8.0.0.beta1)
activerecord (= 8.0.0.beta1)
activestorage (= 8.0.0.beta1)
activesupport (= 8.0.0.beta1)
rails (8.0.0.rc2)
actioncable (= 8.0.0.rc2)
actionmailbox (= 8.0.0.rc2)
actionmailer (= 8.0.0.rc2)
actionpack (= 8.0.0.rc2)
actiontext (= 8.0.0.rc2)
actionview (= 8.0.0.rc2)
activejob (= 8.0.0.rc2)
activemodel (= 8.0.0.rc2)
activerecord (= 8.0.0.rc2)
activestorage (= 8.0.0.rc2)
activesupport (= 8.0.0.rc2)
bundler (>= 1.15.0)
railties (= 8.0.0.beta1)
railties (= 8.0.0.rc2)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (8.0.0.beta1)
actionpack (= 8.0.0.beta1)
activesupport (= 8.0.0.beta1)
railties (8.0.0.rc2)
actionpack (= 8.0.0.rc2)
activesupport (= 8.0.0.rc2)
irb (~> 1.13)
rackup (>= 1.0.0)
rake (>= 12.2)
Expand All @@ -295,8 +295,8 @@ GEM
regexp_parser (2.9.2)
reline (0.5.10)
io-console (~> 0.5)
rexml (3.3.8)
rspec-core (3.13.1)
rexml (3.3.9)
rspec-core (3.13.2)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.3)
diff-lcs (>= 1.2.0, < 2.0)
Expand Down Expand Up @@ -325,19 +325,19 @@ GEM
rubocop-ast (>= 1.32.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.32.3)
rubocop-ast (1.33.0)
parser (>= 3.3.1.0)
rubocop-factory_bot (2.26.1)
rubocop (~> 1.61)
rubocop-performance (1.22.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rails (2.26.2)
rubocop-rails (2.27.0)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.52.0, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rspec (3.1.0)
rubocop-rspec (3.2.0)
rubocop (~> 1.61)
rubocop-rspec_rails (2.30.0)
rubocop (~> 1.61)
Expand All @@ -352,7 +352,7 @@ GEM
simplecov_json_formatter (~> 0.1)
simplecov-html (0.13.1)
simplecov_json_formatter (0.1.4)
standard (1.41.0)
standard (1.41.1)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.66.0)
Expand Down Expand Up @@ -383,12 +383,11 @@ GEM
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
webrick (1.8.2)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
yaml-lint (0.1.2)
zeitwerk (2.7.0)
zeitwerk (2.7.1)

PLATFORMS
aarch64-linux
Expand Down
7 changes: 3 additions & 4 deletions app/services/post_publisher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class PostPublisher
include Logging

attr_reader :post, :freefeed_client

def initialize(post:, freefeed_client:)
Expand All @@ -15,9 +13,9 @@ def publish
post.update(freefeed_post_id: post_id)
create_comments(post_id)
post.success!
logger.info("---> new post URL: #{post.permalink}")
rescue StandardError
post.fail!
raise
# TBD: Report error
end

Expand Down Expand Up @@ -52,8 +50,9 @@ def create_attachments
post.attachments.map { |url| create_attachment(url) }
end

# TBD: Move download process to the Freefeed client namespace
def create_attachment(url)
Downloader.call(url) do |io, content_type|
Freefeed::Downloader.new(url: url).call do |io, content_type|
response = freefeed_client.create_attachment(io, content_type: content_type)
response.parse.fetch("attachments").fetch("id")
end
Expand Down
6 changes: 1 addition & 5 deletions lib/freefeed/downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ module Freefeed
class Downloader
attr_reader :url, :http_client

def initialize(url:, http_client:)
def initialize(url:, http_client: HTTP)
@url = url
@http_client = http_client
end

def call
# TBD: Honeybadger.context(downloader: {url: url})
response = fetch_url
return unless response&.status&.success?
yield build_io_from(response), response.content_type.mime_type
Expand All @@ -26,9 +25,6 @@ def build_io_from(response)

def fetch_url
http_client.get(url)
rescue StandardError
# TBD: Report download error
nil
end
end
end
2 changes: 0 additions & 2 deletions spec/lib/freefeed/authenticated_request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "rails_helper"

RSpec.describe Freefeed::AuthenticatedRequest do
let(:client) do
Freefeed::Client.new(
Expand Down
2 changes: 0 additions & 2 deletions spec/lib/freefeed/client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "rails_helper"

RSpec.describe Freefeed::Client do
let(:token) { "TEST_TOKEN" }
let(:base_url) { "https://example.com" }
Expand Down
Loading

0 comments on commit 064756e

Please sign in to comment.