@@ -0,0 +1,50 @@ |
||
| 1 |
+module Agents |
|
| 2 |
+ class ReadFileAgent < Agent |
|
| 3 |
+ include FormConfigurable |
|
| 4 |
+ include FileHandling |
|
| 5 |
+ |
|
| 6 |
+ cannot_be_scheduled! |
|
| 7 |
+ consumes_file_pointer! |
|
| 8 |
+ |
|
| 9 |
+ def default_options |
|
| 10 |
+ {
|
|
| 11 |
+ 'data_key' => 'data' |
|
| 12 |
+ } |
|
| 13 |
+ end |
|
| 14 |
+ |
|
| 15 |
+ description do |
|
| 16 |
+ <<-MD |
|
| 17 |
+ The ReadFileAgent takes events from `FileHandling` agents, reads the file, and emits the contents as a string. |
|
| 18 |
+ |
|
| 19 |
+ `data_key` specifies the key of the emitted event which contains the file contents. |
|
| 20 |
+ |
|
| 21 |
+ #{receiving_file_handling_agent_description}
|
|
| 22 |
+ MD |
|
| 23 |
+ end |
|
| 24 |
+ |
|
| 25 |
+ event_description <<-MD |
|
| 26 |
+ {
|
|
| 27 |
+ "data" => '...' |
|
| 28 |
+ } |
|
| 29 |
+ MD |
|
| 30 |
+ |
|
| 31 |
+ form_configurable :data_key, type: :string |
|
| 32 |
+ |
|
| 33 |
+ def validate_options |
|
| 34 |
+ if options['data_key'].blank? |
|
| 35 |
+ errors.add(:base, "The 'data_key' options is required.") |
|
| 36 |
+ end |
|
| 37 |
+ end |
|
| 38 |
+ |
|
| 39 |
+ def working? |
|
| 40 |
+ received_event_without_error? |
|
| 41 |
+ end |
|
| 42 |
+ |
|
| 43 |
+ def receive(incoming_events) |
|
| 44 |
+ incoming_events.each do |event| |
|
| 45 |
+ next unless io = get_io(event) |
|
| 46 |
+ create_event payload: { interpolated['data_key'] => io.read }
|
|
| 47 |
+ end |
|
| 48 |
+ end |
|
| 49 |
+ end |
|
| 50 |
+end |
@@ -0,0 +1,47 @@ |
||
| 1 |
+require 'rails_helper' |
|
| 2 |
+ |
|
| 3 |
+describe Agents::ReadFileAgent do |
|
| 4 |
+ before(:each) do |
|
| 5 |
+ @valid_params = {
|
|
| 6 |
+ 'data_key' => 'data', |
|
| 7 |
+ } |
|
| 8 |
+ |
|
| 9 |
+ @checker = Agents::ReadFileAgent.new(:name => 'somename', :options => @valid_params) |
|
| 10 |
+ @checker.user = users(:jane) |
|
| 11 |
+ @checker.save! |
|
| 12 |
+ end |
|
| 13 |
+ |
|
| 14 |
+ it_behaves_like 'FileHandlingConsumer' |
|
| 15 |
+ |
|
| 16 |
+ context '#validate_options' do |
|
| 17 |
+ it 'is valid with the given options' do |
|
| 18 |
+ expect(@checker).to be_valid |
|
| 19 |
+ end |
|
| 20 |
+ |
|
| 21 |
+ it "requires data_key to be present" do |
|
| 22 |
+ @checker.options['data_key'] = '' |
|
| 23 |
+ expect(@checker).not_to be_valid |
|
| 24 |
+ end |
|
| 25 |
+ end |
|
| 26 |
+ |
|
| 27 |
+ context '#working' do |
|
| 28 |
+ it 'is not working without having received an event' do |
|
| 29 |
+ expect(@checker).not_to be_working |
|
| 30 |
+ end |
|
| 31 |
+ |
|
| 32 |
+ it 'is working after receiving an event without error' do |
|
| 33 |
+ @checker.last_receive_at = Time.now |
|
| 34 |
+ expect(@checker).to be_working |
|
| 35 |
+ end |
|
| 36 |
+ end |
|
| 37 |
+ |
|
| 38 |
+ context '#receive' do |
|
| 39 |
+ it "emits an event with the contents of the receives files" do |
|
| 40 |
+ event = Event.new(payload: {file_pointer: {agent_id: 111, file: 'test'}})
|
|
| 41 |
+ io_mock = mock() |
|
| 42 |
+ mock(@checker).get_io(event) { StringIO.new("testdata") }
|
|
| 43 |
+ expect { @checker.receive([event]) }.to change(Event, :count).by(1)
|
|
| 44 |
+ expect(Event.last.payload).to eq('data' => 'testdata')
|
|
| 45 |
+ end |
|
| 46 |
+ end |
|
| 47 |
+end |