Class: Sem4r::Adwords

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile = "sandbox", options = nil) ⇒ Adwords

initialize Adwords lib profiles “sandbox” and “production” have blocked environment

Examples:

Adwords.new( "sandbox", {:email=>"..."} )
Adwords.new( {:environment=>"...", email => "..." } )
Adwords.new( "sandbox" )
Adwords.new() # default to sandbox

Parameters:

  • profile (String) (defaults to: "sandbox")

    name

  • options (Hash) (defaults to: nil)

    the options for profile.

  • opts (Hash)

    a customizable set of options



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sem4r/adwords.rb', line 74

def initialize(profile = "sandbox", options = nil)
  all_keys = %w{environment email password developer_token mutable config_file password_file}
  @logger  = nil
  if not options.nil?
    # new( "profile", {:email=>"..."} )
    options  = options.stringify_keys
    options.assert_valid_keys(*all_keys)

    config_file   = options.delete("config_file")
    password_file = options.delete("password_file")
    @config = Profile.configure(config_file, password_file)

    @profile = profile.to_s
    @options = Profile.load_config(@config, @profile)
    @options = @options.merge(options)
  elsif profile.respond_to?(:keys)
    # new( {:environment=>"...", email => "..." } )
    options = profile.stringify_keys
    options.assert_valid_keys( *(all_keys-%w{config_file password_file}) )
    @config = Profile.configure
    @options = options
    @profile = "anonymous_" + @options["environment"]
  else
    # new( "sandbox" )
    @profile = profile.to_s
    @config = Profile.configure
    @options = Profile.load_config(@config, @profile)
  end
  if ["sandbox", "production"].include?(profile)
    if @options["environment"].nil?
      @options["environment"] = profile
    end
    if @options["environment"] != profile
      raise "you cannot use profile '#{profile}' with environment '#{@options["environment"]}'"
    end
  end

  Profile.try_to_find_in_password_file("password", @options, @config) unless @options["password"]
end

Instance Attribute Details

#profileObject (readonly)

Returns the value of attribute profile.



28
29
30
# File 'lib/sem4r/adwords.rb', line 28

def profile
  @profile
end

#serviceObject (readonly)

methods only for internal use



220
221
222
# File 'lib/sem4r/adwords.rb', line 220

def service
  @service
end

Class Method Details

.production(options = nil) ⇒ Object

Initialize Adwords with production profile

See Also:



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

def production(options = nil)
  new("production", options)
end

.sandbox(options = nil) ⇒ Object

Initialize Adwords with sandbox profile

See Also:



36
37
38
# File 'lib/sem4r/adwords.rb', line 36

def sandbox(options = nil)
  new("sandbox", options)
end

Instance Method Details

#accountAccount

Returns:



202
203
204
205
# File 'lib/sem4r/adwords.rb', line 202

def 
  deferred_initialize unless @initialized
  @account
end

#add_counters(credentials, counters) ⇒ Object

TODO: credentials are necessary because you might use more then account/credentials

at the same time


226
227
228
229
230
231
# File 'lib/sem4r/adwords.rb', line 226

def add_counters(credentials, counters)
  counters.each_pair { |k, v|
    @counters[k] ||= 0
    @counters[k] += v
  }
end

#config_fileString

Returns the config file name.

Returns:

  • (String)

    the config file name



128
129
130
131
132
133
134
135
# File 'lib/sem4r/adwords.rb', line 128

def config_file
  return @config.config_file if @config
  @config = Profile.search_config
  unless @config
    puts "cannot find configuration files"
  end
  @config.config_file
end

#dump_soap?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/sem4r/adwords.rb', line 166

def dump_soap?
  not @dump_soap_options.nil?
end

#dump_soap_options(dump_options) ⇒ Object

logging



161
162
163
164
# File 'lib/sem4r/adwords.rb', line 161

def dump_soap_options(dump_options)
  @dump_soap_options = dump_options
  @connector.dump_soap_options(dump_options) if @connector
end

#dump_soap_whereObject



170
171
172
173
174
175
176
# File 'lib/sem4r/adwords.rb', line 170

def dump_soap_where
  if @dump_soap_options[:directory]
    "directory:#{@dump_soap_options[:directory]}"
  else
    "file:#{@dump_soap_options[:file]}"
  end
end

#has_password?Boolean

password is defined?

Returns:

  • (Boolean)


143
144
145
# File 'lib/sem4r/adwords.rb', line 143

def has_password?
  !@options["password"].nil?
end

#loggerObject



192
193
194
# File 'lib/sem4r/adwords.rb', line 192

def logger
  @logger
end

#logger=(logger) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/sem4r/adwords.rb', line 178

def logger= logger
  unless logger.instance_of?(Logger)
    file             = File.open(logger, "a")
    file.sync        = true
    logger           = Logger.new(file)
    logger.formatter = proc { |severity, datetime, progname, msg|
      "#{datetime.strftime("%H:%M:%S")}: #{msg}\n"
    }
  end
  @logger= logger

  @connector.logger= logger if @connector
end

#p_countersObject

print api counters on stdout



210
211
212
213
214
215
# File 'lib/sem4r/adwords.rb', line 210

def p_counters
  operations    = @counters[:operations]
  units         = @counters[:units]
  response_time = @counters[:response_time]
  puts "#{units} unit spent for #{operations} operations in #{response_time}ms"
end

#password=(pwd) ⇒ Object

set password



150
151
152
153
154
155
# File 'lib/sem4r/adwords.rb', line 150

def password=(pwd)
  if @account
    raise "already connected cannot change password"
  end
  @options["password"]=pwd
end

#profilesObject

returns profiles contained into current config file



121
122
123
# File 'lib/sem4r/adwords.rb', line 121

def profiles
  Profile.profiles @config.config_file
end

#to_sObject



114
115
116
# File 'lib/sem4r/adwords.rb', line 114

def to_s
  "adwords profile: '#{profile}' config file: '#{config_file}'"
end