lib/sem4r/adwords.rb in sem4r-0.1.3 vs lib/sem4r/adwords.rb in sem4r-0.1.5
- old
+ new
@@ -20,11 +20,10 @@
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# -------------------------------------------------------------------
-
module Sem4r
class Adwords
attr_reader :profile
@@ -44,82 +43,10 @@
#
def production(options = nil)
new("production", options)
end
- #
- # @private
- # Search configuration file in standard locations
- # @return [OpenStruct] with
- # :config_dir
- # :config_file
- # :password_file
- #
- def search_config
- config_filename = "sem4r.yml"
- password_filename = "sem4r_passwords.yml"
-
- #
- # try current directory
- #
- # return File.expand_path(config_filename) if File.exists?( config_filename)
-
- #
- # try ~/.sem4r/sem4r.yml
- #
- # require 'etc'
- # homedir = Etc.getpwuid.dir
- homedir = ENV['HOME']
- config_filepath = File.join(homedir, ".sem4r", config_filename)
- if File.exists?(config_filepath)
- return OpenStruct.new(
- :config_dir => File.join(homedir, ".sem4r"),
- :config_file => config_filepath,
- :password_file => File.join(homedir, ".sem4r", password_filename))
- end
-
- #
- # try <home_sem4r>/config/sem4r
- #
- config_filepath = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "config", config_filename))
- if File.exists?(config_filepath)
- return OpenStruct.new(
- :config_dir => nil,
- :config_file => config_filepath,
- :password_file => nil)
- end
-
- config_filepath = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "config", "sem4r.example.yml"))
- if File.exists?(config_filepath)
- return OpenStruct.new(
- :config_dir => nil,
- :config_file => config_filepath,
- :password_file => nil)
- end
-
- OpenStruct.new(
- :config_dir => nil,
- :config_file => nil,
- :password_file => nil)
- end
-
- #
- # Return list of profile contained into config file
- #
- def profiles(profile_file = nil)
- unless profile_file
- config = search_config
- unless config
- raise Sem4rError, "config file 'sem4r' not found"
- end
- profile_file = config.config_file
- end
- # puts "Loaded profiles from #{profile_file}"
- yaml = YAML::load(File.open(profile_file))
- profiles = yaml['google_adwords']
- profiles
- end
end
#
# initialize Adwords lib
# profiles "sandbox" and "production" have blocked environment
@@ -152,57 +79,57 @@
options = options.stringify_keys
options.assert_valid_keys(*all_keys)
config_file = options.delete("config_file")
password_file = options.delete("password_file")
- configure(config_file, password_file)
+ @config = Profile.configure(config_file, password_file)
@profile = profile.to_s
- @options = load_config(@profile)
+ @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}) )
- configure
+ @config = Profile.configure
@options = options
@profile = "anonymous_" + @options["environment"]
else
# new( "sandbox" )
@profile = profile.to_s
- configure
- @options = load_config(@profile)
+ @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
- try_to_find_password_in_password_file unless @options["password"]
+ Profile.try_to_find_in_password_file("password", @options, @config) unless @options["password"]
end
def to_s
"adwords profile: '#{profile}' config file: '#{config_file}'"
end
#
# returns profiles contained into current config file
#
def profiles
- self.class.profiles @config.config_file
+ Profile.profiles @config.config_file
end
#
# @return [String] the config file name
#
def config_file
return @config.config_file if @config
- @config = Adwords.search_config
+ @config = Profile.search_config
unless @config
puts "cannot find configuration files"
end
@config.config_file
end
@@ -225,70 +152,9 @@
raise "already connected cannot change password"
end
@options["password"]=pwd
end
- def try_to_find_password_in_password_file
- return unless @config.password_file
- return unless File.exists?(@config.password_file)
- passwords = YAML.load(File.open(@config.password_file))
- pass = passwords[@options["email"]]
- @options["password"] = pass if pass
- end
-
- def save_passwords
- if @config.password_file.nil?
- raise "cannot save password"
- end
- puts "save password in #{@config.password_file} (security warning!)"
- passwords = {}
- if File.exists?(@config.password_file)
- passwords = YAML.load(File.open(@config.password_file))
- end
- passwords[@options["email"]] = @options["password"]
- File.open(@config.password_file, "w") do |f|
- f.write(passwords.to_yaml)
- end
- end
-
- private
-
- #
- # @private
- #
- # force config paths
- # and the config file
- #
- def configure(config_file = nil, password_file = nil)
- @config = Adwords.search_config
- unless config_file.nil?
- config_file = File.expand_path(config_file)
- unless File.exists?(config_file)
- raise "#{config_file} not exists"
- end
- @config.config_file = config_file
- end
- unless password_file.nil?
- password_file = File.expand_path(password_file)
- @config.password_file = password_file
- end
- @config
- end
-
-
- # @private
- # load the configuration
- def load_config(profile)
- unless @config.config_file
- raise Sem4rError, "config file 'sem4r' not found"
- end
- # puts "Loaded profiles from #{config_file}"
- yaml = YAML::load(File.open(@config.config_file))
- config = yaml['google_adwords'][profile]
- config || {}
- end
-
- public
##########################################################################
# logging
def dump_soap_options(dump_options)