analyticsPierce
analyticsPierce

Reputation: 3025

How to prepare JSON for batch update

I am using Ruby 1.9.3 without Rails and am trying to prepare email addresses for sending to an email service providers API. I have to break up the sends into batches. I'm using 3 here as an example. First, I get the referrals that we will send from a database query.

referrals = @db.query("SELECT * FROM referrals")

I am taking then taking referrals and using each_slice to create the batches. However, when I run the following code the log entry for prepared_batch has no content while the log entry for referral['client_email'] contains the correct values.

referrals.each_slice(3) do |batch|
  prepared_batch = batch.map do |referral|
    { 
      :EMAIL => referral['client_email'], 
      :EMAIL_TYPE => 'html' 
    }
    @log.info("referral in prepared_batch: #{referral['client_email']}")
    @log.info("prepared_batch : #{prepared_batch}")
  end
end

What should I update here so that prepared_batch has the correct three entries?

I appreciate all advice. Thanks.

Upvotes: 0

Views: 140

Answers (2)

Jokester
Jokester

Reputation: 5617

prepared_batch is assigned only after the map do .. end is done. I believe that's why you didn't get a value in the block.

Upvotes: 1

mechanicalfish
mechanicalfish

Reputation: 12826

You are actually putting the result of a @log.info call in the prepared batch. Try this:

referrals.each_slice(3) do |batch|
  prepared_batch = batch.map do |referral|
    @log.info("referral in prepared_batch: #{referral['client_email']}")
    { 
      :EMAIL => referral['client_email'], 
      :EMAIL_TYPE => 'html' 
    }
  end
  @log.info("prepared_batch : #{prepared_batch}")
end

Upvotes: 1

Related Questions