Skip to content

Commit

Permalink
F2: Test configurator (#579)
Browse files Browse the repository at this point in the history
* Test feeds configuration

* Add `shoulda-matchers` gem

* Use FactoryBot

* Integrate `shoulda`

* Fix validation definition

* Test `Feed` validations

* Rubocop

* Change integration branch name in CI config

* Ruby 3.3.5
  • Loading branch information
dreikanter authored Sep 8, 2024
1 parent 5b144f2 commit 9a698ee
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 46 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ on:
push:
branches:
- master
- feeder2
- f2
pull_request:
branches:
- master
- feeder2
- f2

jobs:
rubocop:
Expand Down
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Rails/SkipsModelValidations:
#

RSpec/ExampleLength:
Max: 10
Max: 50

RSpec/MultipleMemoizedHelpers:
Max: 10
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-3.3.4
ruby-3.3.5
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"

ruby "3.3.4"
ruby "3.3.5"

gem "aasm", "~> 5.5"
gem "amazing_print"
Expand Down Expand Up @@ -43,6 +43,7 @@ group :development, :test do
gem "factory_bot_rails", "~> 6.4"
gem "marginalia", "~> 1.11"
gem "rspec-rails", "~> 6.1"
gem "shoulda-matchers", "~> 6.0"
gem "simplecov", "~> 0.22"
gem "webmock", "~> 3.23"
end
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ GEM
rubocop-rspec (~> 3, >= 3.0.1)
ruby-progressbar (1.13.0)
securerandom (0.3.1)
shoulda-matchers (6.4.0)
activesupport (>= 5.2.0)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand Down Expand Up @@ -428,6 +430,7 @@ DEPENDENCIES
rubocop-rails
rubocop-rspec
rubocop-rspec_rails (~> 2.30)
shoulda-matchers (~> 6.0)
simplecov (~> 0.22)
standard (>= 1.0)
standard-performance
Expand Down
2 changes: 1 addition & 1 deletion app/models/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Feed < ApplicationRecord
normalizes :name, with: ->(name) { name.to_s.strip.downcase }

validates :import_limit, numericality: {less_than_or_equal_to: MAX_LIMIT_LIMIT}
validates :refresh_interval, presence: true, numericality: {greater_or_equal_to: 0}
validates :refresh_interval, presence: true, numericality: {greater_than_or_equal_to: 0}
validates :loader, :normalizer, :processor, presence: true, format: /\A\w+\z/
validates :url, length: {maximum: MAX_URL_LENGTH}, allow_nil: true
validates :source_url, length: {maximum: MAX_URL_LENGTH}, allow_blank: true
Expand Down
2 changes: 2 additions & 0 deletions app/models/feed_entity.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Data object representating a raw feed entity (like a blog post or RSS item).
#
# @see BaseProcessor
#
class FeedEntity
attr_reader :feed, :uid, :content

Expand Down
32 changes: 25 additions & 7 deletions app/services/feeds_configurator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,37 @@ def initialize(feeds_configuration: Rails.configuration.feeds)

def import
feeds_configuration.each do |configuration|
existing_feed = existing_feeds[configuration[:name]]
attributes = feed_attributes(configuration)
feed = existing_feeds_index[configuration[:name]]
attributes = feed_attributes(**configuration)

if existing_feed
existing_feed.update!(**attributes) if existing_feed.configurable?
if feed
update_existing_feed(feed, **attributes) if feed.configurable?
else
Feed.create!(**attributes)
create_new_feed(attributes)
end
end
end

private

def update_existing_feed(feed, attributes = {})
if feed.update(**attributes)
logger.info("feed updated: #{attributes[:name]}")
else
logger.error("error updating feed; attributes: #{attributes.to_json}; errors: #{feed.errors}")
end
end

def create_new_feed(attributes)
feed = Feed.new(**attributes)

if feed.save
logger.info("new feed created: #{attributes[:name]}")
else
logger.error("error creating new feed; attributes: #{attributes.to_json}; errors: #{feed.errors.to_json}")
end
end

def feed_attributes(**configuration)
{
**configuration,
Expand All @@ -35,8 +53,8 @@ def configured_at
@configured_at ||= Time.current
end

def existing_feeds
@existing_feeds ||= Feed.where(name: feed_names).index_with(&:name)
def existing_feeds_index
@existing_feeds_index ||= Feed.where(name: feed_names).index_by(&:name)
end

def feed_names
Expand Down
Loading

0 comments on commit 9a698ee

Please sign in to comment.