From 28a5856d99064ae331ab4f12a871bc82cdacbfc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolo=CC=80=20Rebughini?= Date: Fri, 8 Jan 2021 17:04:52 +0100 Subject: [PATCH] Add a static file publisher --- README.md | 18 ++++++++++- lib/solidus_feeds.rb | 1 + lib/solidus_feeds/publishers/static_file.rb | 19 ++++++++++++ .../publishers/static_file_spec.rb | 31 +++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 lib/solidus_feeds/publishers/static_file.rb create mode 100644 spec/lib/solidus_feeds/publishers/static_file_spec.rb diff --git a/README.md b/README.md index 76bfb97..5a7ea7f 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ need custom configuration on a per-publisher basis. SolidusFeeds.config.register :all_products do |feed| feed.generator = SolidusFeeds::Generators::GoogleMerchant.new(Spree::Product.all) - feed.publisher = SolidusFeeds::Puslishers::S3.new( + feed.publisher = SolidusFeeds::Publishers::S3.new( bucket: "foo", object_key: "bar/my_feed.xml", client: Aws::S3::Client.new(…), # This is optional - use only if a custom config is needed @@ -131,7 +131,23 @@ end ### ActiveStorage ### Rails cache + ### Static file + +To publish the feed directly from an app directory (e.g. the `public` directory), you can use the +Static File Publisher as such: + +```ruby +# config/initializers/solidus_feeds.rb + +SolidusFeeds.config.register :all_products do |feed| + feed.generator = SolidusFeeds::Generators::GoogleMerchant.new(Spree::Product.all) + feed.publisher = SolidusFeeds::Publishers::StaticFile.new( + path: Rails.root.join('public/products.xml') + ) +end +``` + ### FTP ## Builtin Marketplace format generators diff --git a/lib/solidus_feeds.rb b/lib/solidus_feeds.rb index c3dae44..d6d05b6 100644 --- a/lib/solidus_feeds.rb +++ b/lib/solidus_feeds.rb @@ -8,5 +8,6 @@ require 'solidus_feeds/engine' require 'solidus_feeds/publishers/s3' +require 'solidus_feeds/publishers/static_file' require 'solidus_feeds/generators/google_merchant' diff --git a/lib/solidus_feeds/publishers/static_file.rb b/lib/solidus_feeds/publishers/static_file.rb new file mode 100644 index 0000000..67ba743 --- /dev/null +++ b/lib/solidus_feeds/publishers/static_file.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module SolidusFeeds + module Publishers + class StaticFile + attr_reader :path + + def initialize(path:) + @path = path + end + + def call + File.open(path, 'w') do |file| + yield file + end + end + end + end +end diff --git a/spec/lib/solidus_feeds/publishers/static_file_spec.rb b/spec/lib/solidus_feeds/publishers/static_file_spec.rb new file mode 100644 index 0000000..6031d5b --- /dev/null +++ b/spec/lib/solidus_feeds/publishers/static_file_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'csv' + +RSpec.describe SolidusFeeds::Publishers::StaticFile do + let(:filename) { 'my_feed.csv' } + let(:io) { StringIO.new } + let(:generator) { + ->(io) { + csv = CSV.new(io) + csv << ["some", "data"] + csv << ["another", "line"] + } + } + + describe '#call' do + it 'saves the generated content to the specified file' do + buffer = StringIO.new + allow(File).to receive(:open).with(filename, 'w').and_yield(buffer) + static_file_publisher = described_class.new(path: filename) + + static_file_publisher.call do |io| + generator.call(io) + end + + expect(buffer.string).to eq( + "some,data\nanother,line\n" + ) + end + end +end