Class: Sem4r::Adwords
- Inherits:
-
Object
- Object
- Sem4r::Adwords
- Defined in:
- lib/sem4r/adwords.rb
Instance Attribute Summary collapse
-
#profile ⇒ Object
readonly
Returns the value of attribute profile.
-
#service ⇒ Object
readonly
methods only for internal use.
Class Method Summary collapse
-
.production(options = nil) ⇒ Object
Initialize Adwords with production profile.
-
.sandbox(options = nil) ⇒ Object
Initialize Adwords with sandbox profile.
Instance Method Summary collapse
- #account ⇒ Account
-
#add_counters(credentials, counters) ⇒ Object
TODO: credentials are necessary because you might use more then account/credentials at the same time.
-
#config_file ⇒ String
The config file name.
- #dump_soap? ⇒ Boolean
-
#dump_soap_options(dump_options) ⇒ Object
logging.
- #dump_soap_where ⇒ Object
-
#has_password? ⇒ Boolean
password is defined?.
-
#initialize(profile = "sandbox", options = nil) ⇒ Adwords
constructor
initialize Adwords lib profiles “sandbox” and “production” have blocked environment.
- #logger ⇒ Object
- #logger=(logger) ⇒ Object
-
#p_counters ⇒ Object
print api counters on stdout.
-
#password=(pwd) ⇒ Object
set password.
-
#profiles ⇒ Object
returns profiles contained into current config file.
- #to_s ⇒ Object
Constructor Details
#initialize(profile = "sandbox", options = nil) ⇒ Adwords
initialize Adwords lib profiles “sandbox” and “production” have blocked environment
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", = nil) all_keys = %w{environment email password developer_token mutable config_file password_file} @logger = nil if not .nil? # new( "profile", {:email=>"..."} ) = .stringify_keys .assert_valid_keys(*all_keys) config_file = .delete("config_file") password_file = .delete("password_file") @config = Profile.configure(config_file, password_file) @profile = profile.to_s @options = Profile.load_config(@config, @profile) @options = @options.merge() elsif profile.respond_to?(:keys) # new( {:environment=>"...", email => "..." } ) = profile.stringify_keys .assert_valid_keys( *(all_keys-%w{config_file password_file}) ) @config = Profile.configure @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
#profile ⇒ Object (readonly)
Returns the value of attribute profile.
28 29 30 |
# File 'lib/sem4r/adwords.rb', line 28 def profile @profile end |
#service ⇒ Object (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
44 45 46 |
# File 'lib/sem4r/adwords.rb', line 44 def production( = nil) new("production", ) end |
.sandbox(options = nil) ⇒ Object
Initialize Adwords with sandbox profile
36 37 38 |
# File 'lib/sem4r/adwords.rb', line 36 def sandbox( = nil) new("sandbox", ) end |
Instance Method Details
#account ⇒ Account
202 203 204 205 |
# File 'lib/sem4r/adwords.rb', line 202 def account 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_file ⇒ String
Returns 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
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 = @connector.() if @connector end |
#dump_soap_where ⇒ Object
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?
143 144 145 |
# File 'lib/sem4r/adwords.rb', line 143 def has_password? !@options["password"].nil? end |
#logger ⇒ Object
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_counters ⇒ Object
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 |
#profiles ⇒ Object
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_s ⇒ Object
114 115 116 |
# File 'lib/sem4r/adwords.rb', line 114 def to_s "adwords profile: '#{profile}' config file: '#{config_file}'" end |