Created
December 6, 2017 16:34
-
-
Save crova/d8553b17029d53b8087e13f4a9bcd460 to your computer and use it in GitHub Desktop.
Why does it work on Console but not on App?
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
# Fetchs campaigns from SiB | |
class SibCampaignFetcher | |
# fetchs SIB Campaigns | |
# Starts SiB Communication | |
def sib | |
@sib ||= Mailin.new("URL","APIKEY") | |
end | |
# Define the data to collect | |
def shoot_criteria | |
{ "type"=>"classic", "status" => "sent", "page"=>1, "page_limit"=>1000 } | |
end | |
def shoot_criteria2 | |
{ "type"=>"classic", "status" => "sent", "page"=>2, "page_limit"=>1000 } | |
end | |
# Map the data to collect | |
def shoots | |
@shoots ||= sib.get_campaigns_v2(shoot_criteria) | |
end | |
def shoots2 | |
@shoots2 ||= sib.get_campaigns_v2(shoot_criteria2) | |
end | |
def needed_records | |
first_cut = shoots.dig('data') | |
final_cut = first_cut.dig('campaign_records') | |
end | |
def needed_records2 | |
first_cut2 = shoots2.dig('data') | |
final_cut2 = first_cut2.dig('campaign_records') | |
end | |
def campaign_records | |
@campaign_records ||= needed_records.map | |
end | |
def campaign_records2 | |
@campaign_records2 ||= needed_records2.map | |
end | |
# Insert data collected to database | |
def sib_campaigns_fetcher | |
# update/create | |
campaign_records.each do |record| | |
shoot = SibCampaign.find_or_create_by(:campaign_id => record["id"]) | |
unless shoot.update(record) | |
# the data was invalid, so the shoot wasn't saved, do something about that here | |
end | |
end | |
campaign_records2.each do |record| | |
shoot2 = SibCampaign.find_or_create_by(:campaign_id => record["id"]) | |
unless shoot2.update(record) | |
# the data was invalid, so the shoot wasn't saved, do something about that here | |
end | |
x = SibCampaign.where(:turnover => nil) | |
x.each do |i| | |
i.turnover = 0 | |
i.cost = 0 | |
i.margin = 0 | |
i.save | |
end | |
end | |
flash[:notice] = "Shoots beign updated successfully." | |
redirect_to('/fetcher') | |
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
<%= link_to(image_tag("refresh.png", :size => '25x25', :alt => "details"), {:controller => 'fetcher', :action => 'sib_campaigns_fetcher'}, :class => 'back-link') %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment