Skip to content

Commit

Permalink
Merge pull request #1 from nebulab/elia/innitial-registry
Browse files Browse the repository at this point in the history
Initial feed registry implementation
  • Loading branch information
elia authored Sep 11, 2020
2 parents 402208e + fab44eb commit b6f3418
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Define and publish your own feeds
```ruby
# initializer/spree.rb

SolidusFeeds.register_feed :google_merchant_shoes do |feed|
SolidusFeeds.register :google_merchant_shoes do |feed|
taxon = Spree::Taxon.find_by(name: "Shoes")
products = Spree::Product.available.in_taxon(taxon)

Expand All @@ -38,6 +38,12 @@ end
Both the generator and the publisher are expected to respond to `#call`.
The publisher's `#call` method is expected to yield an IO-like object that responds to `#<<`.

At this point the feed can be generated and published using:

```ruby
SolidusFeeds.find(:google_merchant_shoes).publish
```

### Serving the feed from the products controller (legacy)

Support the legacy behavior of `solidus_product_feed` by prepending the product controller decorator.
Expand Down
2 changes: 2 additions & 0 deletions lib/solidus_feeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
require 'solidus_support'

require 'solidus_feeds/version'
require 'solidus_feeds/feed'
require 'solidus_feeds/feeds'
require 'solidus_feeds/engine'
13 changes: 13 additions & 0 deletions lib/solidus_feeds/feed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class SolidusFeeds::Feed
attr_accessor :generator, :publisher

def generate(output)
generator.call(output)
end

def publish
publisher.call do |output|
generate(output)
end
end
end
15 changes: 15 additions & 0 deletions lib/solidus_feeds/feeds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module SolidusFeeds::Feeds
def feeds
@feeds ||= {}
end

def register name, &block
feeds[name] = block
end

def find(name)
SolidusFeeds::Feed.new.tap { |feed| feeds[name].call(feed) }
end

SolidusFeeds.extend self
end
31 changes: 31 additions & 0 deletions spec/solidus_feeds/feeds_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'spec_helper'
require 'stringio'
require 'csv'

RSpec.describe SolidusFeeds::Feeds do
subject(:solidus_feeds) do
Class.new.tap { |klass| klass.extend described_class }
end
let(:io) { StringIO.new }
let(:publisher) { -> &block { block.call(io) } }
let(:generator) {
-> io {
csv = CSV.new(io)
csv << ["some", "data"]
csv << ["another", "line"]
}
}

it 'allows to register, generate, and publish feeds' do
solidus_feeds.register :foo do |feed|
feed.publisher = publisher
feed.generator = generator
end

feed = solidus_feeds.find(:foo)
expect(feed).to be_a SolidusFeeds::Feed

feed.publish
expect(io.string).to eq("some,data\nanother,line\n")
end
end

0 comments on commit b6f3418

Please sign in to comment.