Skip to content

Instantly share code, notes, and snippets.

irb(main):050:0> info
=> {"code"=>"success", "message"=>"Data retrieved", "data"=>[{"plan_type"=>"CREDIT_REC", "credits"=>384501, "credit_type"=>"Send Limit", "start_date"=>"2016-05-27", "end_date"=>"2016-06-27"}, {"plan_type"=>"SMS", "credits"=>0, "credit_type"=>"Send Limit"}, {"first_name"=>"xxx", "last_name"=>"xxxx", "email"=>"xxx", "company"=>"xxx", "address"=>"xxxx", "city"=>"xxx", "zip_code"=>"xxx", "country"=>"xxx"}]}
irb(main):051:0> info.inspect
=> "{\"code\"=>\"success\", \"message\"=>\"Data retrieved\", \"data\"=>[{\"plan_type\"=>\"CREDIT_REC\", \"credits\"=>384501, \"credit_type\"=>\"Send Limit\", \"start_date\"=>\"2016-05-27\", \"end_date\"=>\"2016-06-27\"}, {\"plan_type\"=>\"SMS\", \"credits\"=>0, \"credit_type\"=>\"Send Limit\"}, {\"first_name\"=>\"xxx\", \"last_name\"=>\"xxx\", \"email\"=>\"xxx\", \"company\"=>\"xxx\", \"address\"=>\"xxx\", \"city\"=>\"xxx\", \"zip_code\"=>\"xxx\", \"country\"=>\"xxx\"}]}"
irb(main):052:0> company = info['data'][0]['country']
=> nil
irb(main):053:0> info.dig
A
irb(main):016:0> acct_info
=> {"first_name"=>"xxx", "last_name"=>"xxx", "email"=>"xxx", "company"=>"xxx", "address"=>"xxx", "city"=>"xx", "zip_code"=>"xxx", "country"=>"xxx"}
irb(main):017:0>
irb(main):017:0> acct_info.each do |x|
irb(main):018:1* Acct.create! x.to_a
irb(main):019:1> end
@crova
crova / api call on controller
Created June 8, 2016 23:35
how to ignore non-existant attributes ?
# Single Campaign Information
def single_shoot_fetch
sib = Mailin.new("xxx","xxx")
data = { "id" => 200 }
data.to_s
result = sib.get_campaign_v2(data)
single_shoot = result.to_h
Deliv.create(single_shoot)
end
@crova
crova / IRB log
Created June 9, 2016 22:01
Trying to iterate through each row to update if necessary
irb(main):006:0> rst.each do |row|
irb(main):007:1* Shoot = Shoot.find_by_id(row["id"]) || new
irb(main):008:1> Shoot.attributes = row
irb(main):009:1> Shoot.save!
irb(main):010:1> end
Shoot Load (0.9ms) SELECT "shoots".* FROM "shoots" WHERE "shoots"."id" = $1 LIMIT 1 [["id", 274]]
(irb):7: warning: already initialized constant Shoot
/home/crova/Shelter/Dev/dash/app/models/shoot.rb:1: warning: previous definition of Shoot was here
(0.1ms) BEGIN
SQL (0.4ms) UPDATE "shoots" SET "delivered" = $1, "unique_views" = $2, "viewed" = $3, "clicked" = $4, "clicker" = $5, "share_link" = $6, "stats_by_domain" = $7, "updated_at" = $8 WHERE "shoots"."id" = $9 [["delivered", 2830], ["unique_views", 107], ["viewed", 131], ["clicked", 50], ["clicker", 49], ["share_link", 0], ["stats_by_domain", "{\"hotmail.com\"=>{\"sent\"=>745, \"delivered\"=>734, \"viewed\"=>22, \"clicked\"=>21, \"hard_bounce\"=>11, \"unsub\"=>1, \"complaints\"=>0, \"unique_views\"=>21, \"unique_clicks\"=>21, \"soft_bounces\"=>1}, \"yahoo.com.br
@crova
crova / gist:7a78bd5428b876103e6b75f6470199e0
Created June 10, 2016 18:57
How to update rather than create if ID matches?
# fetchs SIB Campaigns
def fetch
sib = Mailin.new("xxx","xxx")
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|
@crova
crova / 'campaign_records'
Last active June 10, 2016 21:28
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",
create_table "shoots", force: :cascade do |t|
t.string "campaign_id"
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"
@crova
crova / Controller try #1 This creates but with 'campaign_id' = 'nil'
Last active June 12, 2016 12:34
Trying to create (if nothing matchs 'campaign_id') or update (if 'campaign_id' is found on database)
# This creates but with 'campaign_id' = 'nil'
# fetchs SIB Campaigns
def fetch
# update/create
campaign_records.each do |record|
shoot = Shoot.find_or_create_by(:id => record['campaign_id'])
unless shoot.update(record.symbolize_keys.except(:id))
Shoot.create(record)
# the data was invalid, so the shoot wasn't saved, do something about that here
end
Shoot Load (0.3ms) SELECT "shoots".* FROM "shoots" WHERE "shoots"."campaign_id" = $1 LIMIT 1 [["campaign_id", "136"]]
(0.2ms) BEGIN
SQL (0.3ms) INSERT INTO "shoots" ("campaign_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["campaign_id", "136"], ["created_at", "2016-06-12 12:31:33.265367"], ["updated_at", "2016-06-12 12:31:33.265367"]]
(4.0ms) COMMIT
(0.2ms) BEGIN
SQL (0.7ms) UPDATE "shoots" SET "id" = $1, "campaign_name" = $2, "subject" = $3, "bat_sent" = $4, "type" = $5, "html_content" = $6, "entered" = $7, "modified" = $8, "status" = $9, "sent" = $10, "remaining" = $11, "listid" = $12, "exclude_list" = $13, "scheduled_date" = $14, "filter" = $15, "tags" = $16, "header" = $17, "footer" = $18, "from_name" = $19, "from_email" = $20, "form_name" = $21, "reply_to" = $22, "delivered" = $23, "unique_views" = $24, "viewed" = $25, "clicked" = $26, "clicker" = $27, "hard_bounce" = $28, "soft_bounce" = $29, "unsub" = $30, "mirror_click" = $31, "complaints" = $32, "to_fi
@crova
crova / _form.html
Created June 12, 2016 20:48
Having trouble with strong params and update action
<table summary="Shoot form fields">
<tr>
<th><%= f.label('campaign_id', "Campaign ID") %></th>
<td><%= @single_shoot.id %></td>
<td><%= f.hidden_field :id, :value => @single_shoot.id %></td>
</tr>
<tr>
<th><%= f.label(:name, "Name") %></th>
<td><%= f.text_field(:name) %></td>