Class: AdvertiserFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/advertiser_factory.rb

Constant Summary collapse

FIELDS =
[
  {:xml => './advertiser-name/text()',        :db => 'name'},
  {:xml => './advertiser-name/text()',        :db => 'nice_name'},
  {:xml => './advertiser-id/text()',          :db => 'advertiser_id'},
  {:xml => './account-status/text()',         :db => 'account_status'},
  {:xml => './seven-day-epc/text()',          :db => 'seven_day_epc'},
  {:xml => './three-month-epc/text()',        :db => 'three_month_epc'},
  {:xml => './language/text()',               :db => 'language'},
  {:xml => './program-url/text()',            :db => 'program_url'},
  {:xml => './relationship-status/text()',    :db => 'relationship_status'},
  {:xml => './network-rank/text()',           :db => 'network_rank'},
  {:xml => './performance-incentives/text()', :db => 'performance_incentives'},
].freeze

Class Method Summary collapse

Class Method Details

.update_fields(advertiser, advertiser_xml) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/helpers/advertiser_factory.rb', line 17

def self.update_fields(advertiser, advertiser_xml)
  #puts advertiser_xml.xpath(FIELDS[0][:xml]).to_s
  
  #Iterate over every field and attempt to insert
  FIELDS.each_with_index do |field, index|
    
    xml_field = field[:xml]
    db_field = field[:db]
    
    #Magic baby!
    unless db_field == 'nice_name'
      advertiser[db_field] = advertiser_xml.xpath(xml_field).to_s
    else
      advertiser[db_field] = (advertiser_xml.xpath(xml_field).to_s).gsub(/[^A-Za-z0-9]+/, '-').strip.gsub(/\ +/, '-').downcase
    end
    
  end

  #Update categories & Relationships
  advertiser_xml.xpath('./primary-category').children.each_with_index do |children, index|
    category_name = children.child.to_s
    category_nice_name = category_name.gsub(/[^A-Za-z0-9]+/, '-').strip.gsub(/\ +/, '-').downcase
    
    
    category = Category.find_or_create_by_nice_name(category_nice_name, :name => category_name)
    AdvertiserCategoryLink.find_or_create_by_advertiser_id_and_category_id(
      :advertiser_id => advertiser.id,
      :category_id => category.id)
  end
  
  advertiser.save
end