Class: AdGear::Config
- Inherits:
-
Object
- Object
- AdGear::Config
- Defined in:
- lib/ad_gear/config.rb
Defined Under Namespace
Classes: MissingEnvironmentSpecification
Instance Method Summary collapse
-
#format ⇒ Object
Returns a format object suitable for use by ActiveResource.
-
#initialize(obj = {}, environment = nil) ⇒ Config
constructor
Reads in a YAML file containing configuration parameters for the AdGear::Client.
-
#logger ⇒ Object
Returns the Logger instance that was configured in #logger=.
-
#logger=(value) ⇒ Object
Register a Logger for use by AdGear’s client.
-
#password ⇒ Object
Returns the configured password.
- #password=(value) ⇒ Object
-
#site ⇒ Object
Returns the configured site, which is the root of the API.
- #site=(value) ⇒ Object
-
#to_hash ⇒ Object
Returns a Hash of the currect configuration.
-
#user ⇒ Object
Returns the configured username / login.
- #user=(value) ⇒ Object
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.
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
#format ⇒ Object
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 |
#logger ⇒ Object
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.
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 |
#password ⇒ Object
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 |
#site ⇒ Object
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_hash ⇒ Object
Returns a Hash of the currect configuration.
64 65 66 |
# File 'lib/ad_gear/config.rb', line 64 def to_hash @config.dup end |
#user ⇒ Object
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 |