Skip to content

Commit

Permalink
fix s3_event and change method to s3_event_payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
tongueroo committed Dec 24, 2023
1 parent d952915 commit ef0efb3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 34 deletions.
22 changes: 12 additions & 10 deletions lib/jets/job/helpers/s3_event_helper.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
module Jets::Job::Helpers
module S3EventHelper
def s3_events
def s3_event_payloads
messages = event["Records"].map do |record|
record["Sns"]["Message"]
end
message.map do |message|
messages.map do |message|
h = JSON.load(message)
ActiveSupport::HashWithIndifferentAccess.new(h)
end
end

def s3_events?
def s3_event_payloads?
event["Records"]&.any? { |r| r.dig("Sns", "Message") }
end

def s3_objects
records = s3_event["Records"]
records.map do |record|
record["s3"]["object"]
end
s3_event_payloads.map do |payload|
records = payload["Records"]
records.map do |record|
record["s3"]["object"]
end
end.flatten
end

def s3_objects?
s3_event["Records"]&.any? { |r| r.dig("s3", "object") }
s3_event_payloads["Records"]&.any? { |r| r.dig("s3", "object") }
end

# Deprecated methods below
def s3_event
puts "WARN: s3_event is deprecated".color(:yellow)
puts "It can possibly drop events when come in extremely fast."
puts "Use s3_events instead"
s3_events.first
puts "Use s3_event_payloads instead"
s3_event_payloads.first
end

def s3_object
Expand Down
33 changes: 9 additions & 24 deletions spec/lib/jets/job/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,43 +48,28 @@
it "s3_event" do
event = json_file("spec/fixtures/dumps/sns/s3_upload.json")
job = HardJob.new(event, {}, :dig)
# uncomment to debug
# puts JSON.pretty_generate(job.event)
# puts JSON.pretty_generate(job.s3_event)
# puts JSON.pretty_generate(job.s3_object)

expect(job.s3_event.key?("Records")).to be true
expect(job.s3_event_payloads.first).to include("Records")

expect(job.s3_object.key?("key")).to be true
expect(job.s3_object[:key]).to eq "myfolder/subfolder/test.txt"
expect(job.s3_objects.first.key?("key")).to be true
expect(job.s3_objects.first[:key]).to eq "myfolder/subfolder/test.txt"
end
end

context 'sns_event' do
it 'sns_event_payload' do
it 'sns_event_payloads' do
event = json_file("spec/fixtures/dumps/sns/sns_event.json")
job = HardJob.new(event, {}, :dig)
# uncomment to debug
# puts JSON.pretty_generate(job.event)
# puts JSON.pretty_generate(job.sns_event_payload)


expect(job.sns_event_payload.key?("body")).to be true
expect(job.sns_event_payload[:body]).to eq "This is a sns hard job"
expect(job.sns_event_payloads.first.key?("body")).to be true
expect(job.sns_event_payloads.first[:body]).to eq "This is a sns hard job"
end
end

context 'sqs_event' do
it 'sns_event_payload' do
it 'sqs_event_payloads' do
event = json_file("spec/fixtures/dumps/sqs/sqs_event.json")
job = HardJob.new(event, {}, :dig)
# uncomment to debug
# puts JSON.pretty_generate(job.event)
# puts JSON.pretty_generate(job.sqs_event_payload)


expect(job.sqs_event_payload.key?("message")).to be true
expect(job.sqs_event_payload[:message]).to eq "This is a hard job"
expect(job.sqs_event_payloads.first.key?("message")).to be true
expect(job.sqs_event_payloads.first[:message]).to eq "This is a hard job"
end
end

Expand Down

0 comments on commit ef0efb3

Please sign in to comment.