Skip to content

Instantly share code, notes, and snippets.

@crova
Last active June 10, 2016 21:28
Show Gist options
  • Save crova/12a3d47d0675aa3ae7a8d3e917478377 to your computer and use it in GitHub Desktop.
Save crova/12a3d47d0675aa3ae7a8d3e917478377 to your computer and use it in GitHub Desktop.
iterates through each row but keep updating on the same db row
irb(main):067:0> single
=> [{"id"=>200,
"campaign_name"=>"....",
"subject"=>".... !",
"bat_sent"=>"no",
"type"=>"classic",
"html_content"=>"
<body style=\"margin:0; padding:0;\"><!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n<style type=\"text/css\">\nimg {\n\tbackground-position: center;\n\tmargin: auto;\n\tdisplay: block;\n}\n</style>\n</head>\n<body bgcolor=\"#ffffff\">\n </body>", "entered"=>"2016-05-16 02:28:26",
"modified"=>"2016-05-16 02:29:56",
"status"=>"Sent",
"sent"=>28914,
"remaining"=>0,
"listid"=>[103], "exclude_list"=>[5, 14, 37, 99], "stats_by_domain"=>{"bol.com.br"=>{"sent"=>4335, "delivered"=>4335, "viewed"=>145, "clicked"=>20, "hard_bounce"=>0, "unsub"=>5, "complaints"=>0, "unique_views"=>112, "unique_clicks"=>19, "soft_bounces"=>0}, "uol.com.br"=>{"sent"=>1838, "delivered"=>1838, "viewed"=>53, "clicked"=>5, "hard_bounce"=>0, "unsub"=>2, "complaints"=>0, "unique_views"=>44, "unique_clicks"=>5, "soft_bounces"=>0}, "gmail.com"=>{"sent"=>17274, "delivered"=>17271, "viewed"=>145, "clicked"=>10, "hard_bounce"=>3, "unsub"=>6, "complaints"=>0, "unique_views"=>131, "unique_clicks"=>9, "soft_bounces"=>0}, "globo.com"=>{"sent"=>59, "delivered"=>57, "viewed"=>2, "clicked"=>1, "hard_bounce"=>0, "unsub"=>0, "complaints"=>0, "unique_views"=>2, "unique_clicks"=>1, "soft_bounces"=>2}, "outlook.com"=>{"sent"=>1311, "delivered"=>1311, "viewed"=>12, "clicked"=>4, "hard_bounce"=>0, "unsub"=>1, "complaints"=>0, "unique_views"=>10, "unique_clicks"=>3, "soft_bounces"=>0}, "yahoo.com.br"=>{"sent"=>2641, "delivered"=>2641, "viewed"=>163, "clicked"=>18, "hard_bounce"=>0, "unsub"=>7, "complaints"=>2, "unique_views"=>142, "unique_clicks"=>17, "soft_bounces"=>0}, "ig.com.br"=>{"sent"=>102, "delivered"=>102, "viewed"=>4, "clicked"=>1, "hard_bounce"=>0, "unsub"=>0, "complaints"=>0, "unique_views"=>4, "unique_clicks"=>1, "soft_bounces"=>0}, "yahoo.com"=>{"sent"=>230, "delivered"=>230, "viewed"=>5, "clicked"=>0, "hard_bounce"=>0, "unsub"=>1, "complaints"=>0, "unique_views"=>5, "unique_clicks"=>0, "soft_bounces"=>0}, "msn.com"=>{"sent"=>61, "delivered"=>61, "viewed"=>3, "clicked"=>0, "hard_bounce"=>0, "unsub"=>0, "complaints"=>0, "unique_views"=>2, "unique_clicks"=>0, "soft_bounces"=>0}, "brturbo.com.br"=>{"sent"=>6, "delivered"=>6, "viewed"=>0, "clicked"=>0, "hard_bounce"=>0, "unsub"=>0, "complaints"=>0, "unique_views"=>0, "unique_clicks"=>0, "soft_bounces"=>0}, "claro.com.br"=>{"sent"=>2, "delivered"=>2, "viewed"=>0, "clicked"=>0, "hard_bounce"=>0, "unsub"=>0, "complaints"=>0, "unique_views"=>0, "unique_clicks"=>0, "soft_bounces"=>0}, "globomail.com"=>{"sent"=>35, "delivered"=>32, "viewed"=>0, "clicked"=>0, "hard_bounce"=>0, "unsub"=>0, "complaints"=>0, "unique_views"=>0, "unique_clicks"=>0, "soft_bounces"=>3}, "ymail.com"=>{"sent"=>78, "delivered"=>78, "viewed"=>7, "clicked"=>0, "hard_bounce"=>0, "unsub"=>0, "complaints"=>0, "unique_views"=>7, "unique_clicks"=>0, "soft_bounces"=>0}, "icloud.com"=>{"sent"=>60, "delivered"=>26, "viewed"=>9, "clicked"=>0, "hard_bounce"=>0, "unsub"=>0, "complaints"=>0, "unique_views"=>6, "unique_clicks"=>0, "soft_bounces"=>35}, "live.com"=>{"sent"=>581, "delivered"=>581, "viewed"=>5, "clicked"=>0, "hard_bounce"=>0, "unsub"=>0, "complaints"=>0, "unique_views"=>4, "unique_clicks"=>0, "soft_bounces"=>0}, "oi.com.br"=>{"sent"=>136, "delivered"=>132, "viewed"=>0, "clicked"=>1, "hard_bounce"=>0, "unsub"=>0, "complaints"=>0, "unique_views"=>0, "unique_clicks"=>1, "soft_bounces"=>4}, "outlook.com.br"=>{"sent"=>111, "delivered"=>111, "viewed"=>0, "clicked"=>0, "hard_bounce"=>0, "unsub"=>1, "complaints"=>0, "unique_views"=>0, "unique_clicks"=>0, "soft_bounces"=>0}, "ibest.com.br"=>{"sent"=>12, "delivered"=>5, "viewed"=>0, "clicked"=>0, "hard_bounce"=>0, "unsub"=>0, "complaints"=>0, "unique_views"=>0, "unique_clicks"=>0, "soft_bounces"=>7}, "zipmail.com.br"=>{"sent"=>13, "delivered"=>13, "viewed"=>0, "clicked"=>0, "hard_bounce"=>0, "unsub"=>0, "complaints"=>0, "unique_views"=>0, "unique_clicks"=>0, "soft_bounces"=>0}, "iclaro.com.br"=>{"sent"=>3, "delivered"=>3, "viewed"=>0, "clicked"=>0, "hard_bounce"=>0, "unsub"=>0, "complaints"=>0, "unique_views"=>0, "unique_clicks"=>0, "soft_bounces"=>0}, "facebook.com"=>{"sent"=>2, "delivered"=>2, "viewed"=>0, "clicked"=>0, "hard_bounce"=>0, "unsub"=>0, "complaints"=>0, "unique_views"=>0, "unique_clicks"=>0, "soft_bounces"=>0}, "superig.com.br"=>{"sent"=>5, "delivered"=>5, "viewed"=>0, "clicked"=>0, "hard_bounce"=>0, "unsub"=>0, "complaints"=>0, "unique_views"=>0, "unique_clicks"=>0, "soft_bounces"=>0}, "terra.com.br"=>{"sent"=>3, "delivered"=>3, "viewed"=>0, "clicked"=>0, "hard_bounce"=>0, "unsub"=>0, "complaints"=>0, "unique_views"=>0, "unique_clicks"=>0, "soft_bounces"=>0}}, "scheduled_date"=>"2016-05-16 02:33:42", "filter"=>[{"search"=>{"listid.id"=>{"$nin"=>[5, 14, 37, 99], "$in"=>[103]}, "deleteAt"=>{"$exists"=>false}}, "segment_data"=>[], "match"=>"or", "new"=>"true", "segments_query_txt"=>nil, "type"=>false}], "tags"=>"english", "header"=>"EXISTS", "footer"=>"EXISTS", "from_name"=>"... !", "from_email"=>"", "form_name"=>"", "reply_to"=>"...t", "delivered"=>28861, "unique_views"=>469, "viewed"=>553, "clicked"=>60, "clicker"=>56, "hard_bounce"=>3, "soft_bounce"=>51, "unsub"=>23, "mirror_click"=>20, "complaints"=>2, "links_message"=>{".."=>{"clicked"=>60}}, "to_field"=>"", "share_link"=>"...."}]
# fetchs SIB Campaigns
def fetch
sib = Mailin.new("xxxx","xxxx")
data = { "type"=>"classic", "status" => "sent", "page"=>1,"page_limit"=>1000 }
shoots = sib.get_campaigns_v2(data)
shoots = shoots.dig('data')
rst = shoots.dig('campaign_records')
# update/create
rst.each do |row|
shoot = Shoot.find_or_initialize_by(row[:id], shoot.update_attributes(campaign_name: row['campaign_name'],subject: row['subject'],bat_sent: row['bat_sent'],html_content: row['html_content'],entered: row['entered'],modified: row['modified'],status: row['status'],sent: row['sent'],remaining: row['remaining'],listid: row['listid'],exclude_list: row['exclude_list'],stats_by_domain: row['stats_by_domain'],scheduled_date: row['scheduled_date'],filter: row['filter'],tags: row['tags'],header: row['header'],footer: row['footer'],to_field: row['to_field'],from_name: row['from_name'],from_email: row['from_email'],form_name: row['form_name'],reply_to: row['reply_to'],delivered: row['delivered'],unique_views: row['unique_views'],viewed: row['viewed'],clicked: row['clicked'],clicker: row['clicker'],hard_bounce: row['hard_bounce'],soft_bounce: row['soft_bounce'],unsub: row['unsub'],mirror_click: row['mirror_click'],complaints: row['complaints'],links_message: row['links_message'],share_link: row['share_link'],payout: row['payout'],ecpm: row['ecpm'],turnover: row['turnover'],cost: row['cost'],margin: row['margin'],result: row['result'],roi: row['roi'],name: row['name'],segment: row['segment'],deal: row['deal'],partner: row['partner'] ))
end
redirect_to(:action => 'index')
end
class CreateShoots < ActiveRecord::Migration
def self.inheritance_column
nil
end
def change
create_table :shoots do |t|
t.string :campaign_name
t.string :subject
t.string :bat_sent
t.string :type
t.string :html_content
t.date :entered
t.date :modified
t.string :status
t.integer :sent
t.integer :remaining
t.string :listid
t.string :exclude_list
t.json :stats_by_domain
t.date :scheduled_date
t.string :filter
t.string :tags
t.string :header
t.string :footer
t.string :to_field
t.string :from_name
t.string :from_email
t.string :form_name
t.string :reply_to
t.integer :delivered
t.integer :unique_views
t.integer :viewed
t.integer :clicked
t.integer :clicker
t.integer :hard_bounce
t.integer :soft_bounce
t.integer :unsub
t.integer :mirror_click
t.integer :complaints
t.integer :links_message
t.integer :share_link
t.integer :payout
t.integer :ecpm
t.integer :turnover
t.integer :cost
t.integer :margin
t.integer :result
t.integer :roi
t.string :name
t.string :segment
t.string :deal
t.string :partner
t.timestamps null: false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment