Class: AdGear::Config

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

Defined Under Namespace

Classes: MissingEnvironmentSpecification

Instance Method Summary collapse

Constructor Details

#initialize(obj = {}, environment = nil) ⇒ Config

Reads in a YAML file containing configuration parameters for the AdGear::Client. The file can contain sections relating to the environment the client is running within.

The YAML configuration file must use strings as keys.

Examples:

A sample YAML configuration file:

development:
  site:     http://localhost:3000/api
  user:     dummy
  password: whatever
  logger:   <%= Rails.root %>/path/to/log/file.log
production:
  site:     http://admin.adgear.com/api
  user:     your_real_login
  password: your_real_digest_password
  logger:   /var/log/ad_gear.log

Reading this sample file:

AdGear.config = AdGear::Config.new("path/to/config/file", "development")

Reading from config/initializers/ad_gear.rb in a Rails environment

AdGear.config = AdGear::Config.new(Rails.root + "config/ad_gear.yml", Rails.env)

Parameters:

  • obj (#read, Hash, String) (defaults to: {})

    When the object responds to #read, reads in the IO-like object and parses it as a YAML stream. If the object is a Hash, use it as-is. Else, treat the object as the path to a YAML file containing the configuration.

  • environment (String, Symbol, nil) (defaults to: nil)

    If this is nil, then do no environment unpacking, else find the environment that matches this string. If none matches, a MissingEnvironmentSpecification exception will be raised.

Raises:

  • MissingEnvironmentSpecification When the environment parameter specifies a missing environment declaration.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ad_gear/config.rb', line 42

def initialize(obj={}, environment=nil)
  @config = Hash.new
  @logger = nil

  config = if obj.kind_of?(Hash) then
             obj
           elsif obj.respond_to?(:read) then
             YAML.load(ERB.new(obj.read).result)
           else
             YAML.load(ERB.new(IO.read(obj)).result)
           end
  if environment then
    raise MissingEnvironmentSpecification, "Could not find #{environment.to_s.inspect} in #{config.inspect} read from #{obj.inspect}.  Add the missing environment declaration, or change the parameter to this method." unless config.respond_to?(:has_key?) && config.has_key?(environment.to_s)
    config = config[environment.to_s]
  end

  config.each_pair do |key, value|
    send("#{key}=", value)
  end
end

Instance Method Details

#formatObject

Returns a format object suitable for use by ActiveResource.



96
97
98
# File 'lib/ad_gear/config.rb', line 96

def format
  AdGear::XmlFormat
end

#loggerObject

Returns the Logger instance that was configured in #logger=.



101
102
103
# File 'lib/ad_gear/config.rb', line 101

def logger
  @logger
end

#logger=(value) ⇒ Object

Register a Logger for use by AdGear’s client.

Parameters:

  • value (nil, String)

    When nil, no Logger is assigned. When the values STDERR or STDOUT are used, logs to the specified stream. Else, it is taken as the path to a log file.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ad_gear/config.rb', line 109

def logger=(value)
  @config["logger"] = value
  @logger = case value
            when nil, ""
              # No logger
              nil
            when /^std(err|out)$/i
              # Existing stream
              Logger.new(Object.const_get(value.upcase))
            else
              warn("WARNING: path to logger in AdGear::Client configuration is relative: CWD is #{Dir.pwd.inspect}") unless Pathname.new(value).absolute?
              # Path to a file
              Logger.new(value)
            end
end

#passwordObject

Returns the configured password.



91
92
93
# File 'lib/ad_gear/config.rb', line 91

def password
  @config["password"]
end

#password=(value) ⇒ Object



86
87
88
# File 'lib/ad_gear/config.rb', line 86

def password=(value)
  @config["password"] = value
end

#siteObject

Returns the configured site, which is the root of the API.



73
74
75
# File 'lib/ad_gear/config.rb', line 73

def site
  @config["site"]
end

#site=(value) ⇒ Object



68
69
70
# File 'lib/ad_gear/config.rb', line 68

def site=(value)
  @config["site"] = value
end

#to_hashObject

Returns a Hash of the currect configuration.



64
65
66
# File 'lib/ad_gear/config.rb', line 64

def to_hash
  @config.dup
end

#userObject

Returns the configured username / login.



82
83
84
# File 'lib/ad_gear/config.rb', line 82

def user
  @config["user"]
end

#user=(value) ⇒ Object



77
78
79
# File 'lib/ad_gear/config.rb', line 77

def user=(value)
  @config["user"] = value
end