Skip to content

Commit

Permalink
Merge pull request #17 from nebulab/nirebu/file-publisher
Browse files Browse the repository at this point in the history
Add a static file publisher
  • Loading branch information
nirebu authored Jan 15, 2021
2 parents 1ab1ff7 + 28a5856 commit d2cb7db
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/solidus_feeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
19 changes: 19 additions & 0 deletions lib/solidus_feeds/publishers/static_file.rb
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions spec/lib/solidus_feeds/publishers/static_file_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d2cb7db

Please sign in to comment.