Class: Adapi::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/adapi/config.rb

Constant Summary collapse

DEFAULT_LOG_PATH =
File.join(self.dir, 'adapi.log')

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dirObject

Returns the value of attribute dir.



8
9
10
# File 'lib/adapi/config.rb', line 8

def dir
  @dir
end

.filenameObject

Returns the value of attribute filename.



8
9
10
# File 'lib/adapi/config.rb', line 8

def filename
  @filename
end

Class Method Details

.load_settings(params = {}) ⇒ Object

Loads adapi configuration from given hash or from external configuration file

params:

  • dir (default: HOME)

  • filename (default: adapi.yml)

  • in_hash - hash to use instead of external configuration



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/adapi/config.rb', line 50

def self.load_settings(params = {})
  if params[:in_hash]
    return @settings = params[:in_hash]
  end

  # load external config file (defaults to ~/adapi.yml)
  self.dir = params[:dir] if params[:dir]
  self.filename = params[:filename] if params[:filename]
  path = (self.dir.present? ? File.join(self.dir, self.filename) : self.filename)

  if File.exists?(path)
    @settings = YAML::load(File.read(path)) rescue {}
    @settings.symbolize_keys!

    # is it an adwords_api config-file?
    if @settings.present? && @settings[:authentication].present?
      @settings = {:default => @settings}
    end
  end

  @settings
end

.log_pathObject

Returns complete path to log file - both directory and file name.



75
76
77
# File 'lib/adapi/config.rb', line 75

def self.log_path
  (Adapi::Config.read[:library][:log_path] rescue nil) || DEFAULT_LOG_PATH 
end

.readObject

display actual account settings if it’s not available, set to :default account settings



29
30
31
# File 'lib/adapi/config.rb', line 29

def self.read # = @data
  @data ||= self.settings[:default]
end

.set(account_alias = :default, authentication_params = {}) ⇒ Object

account_alias - alias of an account set in settings authentication_params - …which we want to override



36
37
38
39
40
# File 'lib/adapi/config.rb', line 36

def self.set( = :default, authentication_params = {})
  custom_settings = @settings[.to_sym]
  custom_settings[:authentication] = custom_settings[:authentication].merge(authentication_params)
  @data = custom_settings
end

.settings(reload = false) ⇒ Object

display hash of all account settings



18
19
20
21
22
23
24
# File 'lib/adapi/config.rb', line 18

def self.settings(reload = false)
  if reload
    @settings = self.load_settings
  else
    @settings ||= self.load_settings
  end
end

.setup_loggerObject

Returns freshly initialized logger object (or nil, if not available)



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/adapi/config.rb', line 81

def self.setup_logger
  log_level = self.read[:library][:log_level] rescue nil

  if log_level
    logger = Logger.new(self.log_path)
    logger.level = eval("Logger::%s" % log_level.to_s.upcase)
    logger
  else
    nil
  end
end