Class: Sem4r::Campaign

Inherits:
Base
  • Object
show all
Defined in:
lib/sem4r/campaign/campaign.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#adwords, #credentials, #service

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#add_counters

Methods included from Sem4rSoap::SoapAttributes

#_from_element, #_to_s, #_to_xml, included

Constructor Details

#initialize(account, name = nil, &block) ⇒ Campaign

Returns a new instance of Campaign.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sem4r/campaign/campaign.rb', line 52

def initialize(, name = nil, &block)
  super( .adwords, .credentials )
  @account = 
  @ad_groups = nil
  self.name = name
  if block_given?
    @inside_initialize = true
    block.arity < 1 ? instance_eval(&block) : block.call(self)
    save
  end
  @inside_initialize = false
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



44
45
46
# File 'lib/sem4r/campaign/campaign.rb', line 44

def 
  @account
end

#idObject (readonly)

Returns the value of attribute id.



43
44
45
# File 'lib/sem4r/campaign/campaign.rb', line 43

def id
  @id
end

Class Method Details

.create(account, &block) ⇒ Object



104
105
106
# File 'lib/sem4r/campaign/campaign.rb', line 104

def self.create(, &block)
  new(, &block).save
end

.from_element(account, el) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/sem4r/campaign/campaign.rb', line 93

def self.from_element(, el)
  new() do
    @id          = el.at_xpath("id").text.strip.to_i
    name           el.at_xpath("name").text.strip
    status         el.at_xpath('status').text.strip # ACTIVE, PAUSED, DELETED
    serving_status el.at_xpath('servingStatus')
    start_date     el.at_xpath('startDate')
    end_date       el.at_xpath('endDate')
  end
end

Instance Method Details

#ad_group(name = nil, &block) ⇒ Object

ad_group management



144
145
146
147
148
149
150
151
# File 'lib/sem4r/campaign/campaign.rb', line 144

def ad_group(name = nil, &block)
  save
  ad_group = AdGroup.new(self, name, &block)
  ad_group.save
  @ad_groups ||= []
  @ad_groups.push(ad_group)
  ad_group
end

#ad_groups(refresh = false, opts = {}) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/sem4r/campaign/campaign.rb', line 153

def ad_groups(refresh = false, opts = {})
  if refresh.respond_to?(:keys)
    opts = refresh
    refresh = false
  end
  _ad_groups unless @ad_groups and !refresh
  # statuses = [:ACTIVE, :PAUSED]
  # @ad_groups.select do |ad_group|
  #   statuses.include?(ad_group.status)
  # end
  @ad_groups
end

#bidding_strategy(strategy = nil) ⇒ Object

ManualCPC, ManualCPM



135
136
137
138
139
# File 'lib/sem4r/campaign/campaign.rb', line 135

def bidding_strategy(strategy = nil)
  @bidding_strategy = strategy if strategy
  @bidding_strategy ||= "ManualCPC"
  @bidding_strategy
end

#deleteObject



119
120
121
122
123
# File 'lib/sem4r/campaign/campaign.rb', line 119

def delete
  soap_message = service.campaign.delete(credentials, @id)
  add_counters( soap_message.counters )
  @id = -1 # logical delete
end

#empty?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/sem4r/campaign/campaign.rb', line 127

def empty?
  _ad_groups.empty?
end

#inside_initialize?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/sem4r/campaign/campaign.rb', line 65

def inside_initialize?
  @inside_initialize
end

#p_ad_groups(refresh = false, opts = {}) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/sem4r/campaign/campaign.rb', line 166

def p_ad_groups(refresh = false, opts = {})
  if refresh.respond_to?(:keys)
    opts = refresh
    refresh = false
  end
  cs = ad_groups(refresh, opts)
  puts "#{cs.length} ad_groups"
  cs.each do |ad_group|
    puts ad_group.to_s
  end
  self
end

#saveObject



108
109
110
111
112
113
114
115
116
117
# File 'lib/sem4r/campaign/campaign.rb', line 108

def save
  unless @id
    soap_message = service.campaign.create(credentials, to_xml)
    add_counters( soap_message.counters )
    rval = soap_message.response.at_xpath("//mutateResponse/rval")
    id = rval.xpath("value/id").first
    @id = id.text.strip.to_i
  end
  self
end

#to_sObject



69
70
71
# File 'lib/sem4r/campaign/campaign.rb', line 69

def to_s
  "#{@id} '#{@name}' (#{@status})"
end

#to_xmlObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sem4r/campaign/campaign.rb', line 73

def to_xml
  <<-EOS
    <name>#{name}</name>
    <status>#{status}</status>

    <budget>
      <period>DAILY</period>
      <amount xsi:type="Money">
        <microAmount>50000000</microAmount>
      </amount>
      <deliveryMethod>STANDARD</deliveryMethod>
    </budget>

    <biddingStrategy xsi:type="#{bidding_strategy}"></biddingStrategy>

  EOS
end