Skip to content

Instantly share code, notes, and snippets.

@crova
Last active December 11, 2017 18:00
Show Gist options
  • Select an option

  • Save crova/a53bf460e3beacd200930c545f3b1a36 to your computer and use it in GitHub Desktop.

Select an option

Save crova/a53bf460e3beacd200930c545f3b1a36 to your computer and use it in GitHub Desktop.
Problem with table name "invalid"! How to save "invalid" from API response to the column "invalid_sent" from database
{"requests"=>5851, "delivered"=>5656, "bounces"=>194, "invalid"=>11, "deferred"=>46, "opens"=>2705, "unique_opens"=>1500, "clicks"=>158, "unique_clicks"=>140, "date"=>"2017-11-25", "tag"=>"WM-ABD-2018-GMAIL-E-CIA"}
# Fetchs information for TAG WM-ABD-2018-GMAIL-E-CIA
def sib
@sib ||= Mailin.new(APIURL,APIKEY)
end
# Define the data to collect
def shoot_criteria_gmail_e_cia
{ "aggregate" => 0, "tag" => "WM-ABD-2018-GMAIL-E-CIA", "end_date" => "2017-12-10", "start_date" => "2017-11-01" }
end
# Map the data to collect
def shoots_gmail_e_cia
@shoots_gmail_e_cia ||= sib.get_statistics(shoot_criteria_gmail_e_cia)
end
def needed_records_gmail_e_cia
first_cut = @shoots_gmail_e_cia.dig('data')
end
def campaign_records_gmail_e_cia
@campaign_records_gmail_e_cia ||= needed_records_gmail_e_cia.map
end
def transac_gmail_e_cia_fetch
def self.find_or_create(attributes)
SibTransac.where(attributes).first || SibTransac.create(attributes)
end
# update/create
campaign_records_gmail_e_cia.each do |record|
shoot = SibTransac.find_or_create_by(record)
unless shoot.update(record)
end
end
end
class CreateSibTransacs < ActiveRecord::Migration[5.0]
def change
create_table :sib_transacs do |t|
t.integer :requests
t.integer :delivered
t.integer :bounces
t.integer :spamreports
t.integer :blocked
t.integer :invalid_sent
t.integer :deferred
t.integer :opens
t.integer :unique_opens
t.integer :clicks
t.integer :unique_clicks
t.string :tag
t.float :cost
t.float :turnover
t.float :margin
t.float :cpm
t.float :ecpm
t.string :partner
t.string :name
t.string :date
t.timestamps
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment