Last active
October 21, 2017 19:22
-
-
Save crova/95ac81258d1079bbdfffdb3877a0bb3f to your computer and use it in GitHub Desktop.
How to save the needed data to model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [{"Stat"=>{"affiliate_info1"=>nil, "approved_payout"=>"277.000000"}, "Offer"=>{"name"=>"Johnson&Johnson Promo - BR"}}, {"Stat"=>{"affiliate_info1"=>"", "approved_payout"=>"99.003150"}, "Offer"=>{"name"=>"Toluna - BR"}}, {"Stat"=>{"affiliate_info1"=>"", "approved_payout"=>"54.000000"}, "Offer"=>{"name"=>"eCGlobal - 168742 - Tecnologia - BR"}}, {"Stat"=>{"affiliate_info1"=>"", "approved_payout"=>"11.850000"}, "Offer"=>{"name"=>"Clube da Lu - BR"}}, {"Stat"=>{"affiliate_info1"=>"", "approved_payout"=>"81.250000"}, "Offer"=>{"name"=>"Conecta í Uber - BR"}}, {"Stat"=>{"affiliate_info1"=>"BASE", "approved_payout"=>"104.751760"}, "Offer"=>{"name"=>"Global Test Market - BR"}}, {"Stat"=>{"affiliate_info1"=>"SUBID", "approved_payout"=>"10.500000"}, "Offer"=>{"name"=>"Eudora - BR"}}, {"Stat"=>{"affiliate_info1"=>"SUBID", "approved_payout"=>"397.928968"}, "Offer"=>{"name"=>"Angela - BR"}}, {"Stat"=>{"affiliate_info1"=>"subid", "approved_payout"=>"498.369118"}, "Offer"=>{"name"=>"Mundo de Opiniões - BR"}}, {"Stat"=>{"affiliate_info1"=>"subid", "approved_payout"=>"59.820411"}, "Offer"=>{"name"=>"Padre - BR"}}, {"Stat"=>{"affiliate_info1"=>"subid", "approved_payout"=>"810.397266"}, "Offer"=>{"name"=>"Coração de ouro - BR"}}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 2.3.3 :031 > camp_data_name | |
| => ["Johnson&Johnson Promo - BR", "Toluna - BR", "eCGlobal - 168742 - Tecnologia - BR", "Clube da Lu - BR", "Conecta í Uber - BR", "Global Test Market - BR", "Eudora - BR", "Angela - BR", "Mundo de Opiniões - BR", "Padre - BR", "Coração de ouro - BR"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def ybox_camps | |
| # Make the call to Ybox API and isolate the needed data | |
| x = Typhoeus.get("https://network.api.hasoffers.com/Apiv3/json?api_key=xxx&Target=Affiliate_Report&Method=getConversions&fields[]=Stat.affiliate_info1&fields[]=Offer.name&fields[]=Stat.approved_payout&groups[]=Offer.name") | |
| y = x.body | |
| z = JSON.parse(y) | |
| resp = z.dig('response') | |
| resp_data = resp.dig('data') | |
| camp_data = resp_data.dig('data') | |
| # Isolates Model data | |
| camp_data_offer = camp_data.pluck('Offer') | |
| camp_data_stat = camp_data.pluck('Stat') | |
| # Isolates needed data: Name / Subid / Total comission | |
| camp_data_name = camp_data_offer.pluck('name') | |
| camp_data_comission = camp_data_stat.pluck('approved_payout') | |
| camp_data_subid = camp_data_stat.pluck('affiliate_info1') | |
| # Saves needed data to Database | |
| # Campaign names | |
| camp_data_name.each do |record| | |
| campaign_name = Ybox.find_or_create_by(:id => record["id"]) | |
| unless campaign_name.update(record) | |
| # the data was invalid, so the shoot wasn't saved, do something about that here | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Ybox Load (0.8ms) SELECT "yboxes".* FROM "yboxes" WHERE "yboxes"."id" IS NULL LIMIT $1 [["LIMIT", 1]] | |
| (0.2ms) BEGIN | |
| SQL (0.7ms) INSERT INTO "yboxes" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2017-10-21 18:27:51.389092"], ["updated_at", "2017-10-21 18:27:51.389092"]] | |
| (4.0ms) COMMIT | |
| (0.1ms) BEGIN | |
| (0.1ms) ROLLBACK | |
| ArgumentError: When assigning attributes, you must pass a hash as an argument. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 2.3.3 :058 > z | |
| => {"request"=>{"Target"=>"Affiliate_Report", "Format"=>"json", "Service"=>"HasOffers", "Version"=>"2", "api_key"=>"51ac97d56d465391cb752e75ff54f4d16778b0c7d870f5ed085880ef9a401e88", "Method"=>"getConversions", "fields"=>["Stat.affiliate_info1", "Offer.name", "Stat.approved_payout"], "groups"=>["Offer.name"]}, "response"=>{"status"=>1, "httpStatus"=>200, "data"=>{"page"=>1, "current"=>100, "count"=>11, "pageCount"=>1, "data"=>[{"Stat"=>{"affiliate_info1"=>nil, "approved_payout"=>"277.000000"}, "Offer"=>{"name"=>"Johnson&Johnson Promo - BR"}}, {"Stat"=>{"affiliate_info1"=>"", "approved_payout"=>"99.003150"}, "Offer"=>{"name"=>"Toluna - BR"}}, {"Stat"=>{"affiliate_info1"=>"", "approved_payout"=>"54.000000"}, "Offer"=>{"name"=>"eCGlobal - 168742 - Tecnologia - BR"}}, {"Stat"=>{"affiliate_info1"=>"", "approved_payout"=>"11.850000"}, "Offer"=>{"name"=>"Clube da Lu - BR"}}, {"Stat"=>{"affiliate_info1"=>"", "approved_payout"=>"81.250000"}, "Offer"=>{"name"=>"Conecta í Uber - BR"}}, {"Stat"=>{"affiliate_info1"=>"BASE", "approved_payout"=>"104.751760"}, "Offer"=>{"name"=>"Global Test Market - BR"}}, {"Stat"=>{"affiliate_info1"=>"SUBID", "approved_payout"=>"10.500000"}, "Offer"=>{"name"=>"Eudora - BR"}}, {"Stat"=>{"affiliate_info1"=>"SUBID", "approved_payout"=>"397.928968"}, "Offer"=>{"name"=>"Angela - BR"}}, {"Stat"=>{"affiliate_info1"=>"subid", "approved_payout"=>"498.369118"}, "Offer"=>{"name"=>"Mundo de Opiniões - BR"}}, {"Stat"=>{"affiliate_info1"=>"subid", "approved_payout"=>"59.820411"}, "Offer"=>{"name"=>"Padre - BR"}}, {"Stat"=>{"affiliate_info1"=>"subid", "approved_payout"=>"810.397266"}, "Offer"=>{"name"=>"Coração de ouro - BR"}}], "dbSource"=>"branddb"}, "errors"=>[], "errorMessage"=>nil}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| create_table "yboxes", force: :cascade do |t| | |
| t.string "camp_name" | |
| t.float "camp_payout" | |
| t.string "camp_subid" | |
| t.float "camp_conversion" | |
| t.float "camp_total_comission" | |
| t.datetime "created_at", null: false | |
| t.datetime "updated_at", null: false | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Want to store into database: | |
| camp_data_name as camp_name | |
| camp_data_comission as camp_total_comission | |
| camp_data_subid as camp_subid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment