-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from nebulab/nirebu/file-publisher
Add a static file publisher
- Loading branch information
Showing
4 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |