Class: MessageBird::Config

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

Defined Under Namespace

Classes: ConfigBlock

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



7
8
9
# File 'lib/messagebird/config.rb', line 7

def config
  @config
end

Class Method Details

.[](key) ⇒ Object

Retrieve the value of a config entry.

Parameters

key<Object>

The key to retrieve the parameter for.

Returns

Object

The value of the configuration parameter.

:api: public



67
68
69
# File 'lib/messagebird/config.rb', line 67

def [](key)
  configuration[key]
end

.[]=(key, val) ⇒ Object

Set the value of a config entry.

Parameters

key<Object>

The key to set the parameter for.

val<Object>

The value of the parameter.

:api: public



78
79
80
# File 'lib/messagebird/config.rb', line 78

def []=(key, val)
  configuration[key] = val
end

.configurationObject

Returns the current configuration or sets it up



21
22
23
# File 'lib/messagebird/config.rb', line 21

def configuration
  @config ||= setup
end

.configure(&block) ⇒ Object

Set configuration parameters from a code block, where each method evaluates to a config parameter.

Parameters

&block

Configuration parameter block.

Examples

# Set environment and log level.
MessageBird::Config.configure do
  environment "development"
  log_level   "debug"
end

Returns

nil

:api: public



157
158
159
160
# File 'lib/messagebird/config.rb', line 157

def configure(&block)
  ConfigBlock.new(self, &block) if block_given?
  nil
end

.defaultsObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/messagebird/config.rb', line 9

def defaults
  @defaults ||= {
    :module =>      :http,
    :api_url =>     "https://api.messagebird.com/api/sms",
    :username =>    'replace_me',
    :password =>    'replace_me',
    :sender_name => 'replace_me',
    :test_mode =>   true
  }
end

.delete(key) ⇒ Object

Remove the value of a config entry.

Parameters

key<Object>

The key of the parameter to delete.

Returns

Object

The value of the removed entry.

:api: public



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

def delete(key)
  configuration.delete(key)
end

.fetch(key, default = nil) ⇒ Object

Retrieve the value of a config entry, returning the provided default if the key is not present

Parameters

key<Object>

The key to retrieve the parameter for.

default<Object>

The default value to return if the parameter is not set.

Returns

Object

The value of the configuration parameter or the default.

:api: public



112
113
114
# File 'lib/messagebird/config.rb', line 112

def fetch(key, default = nil)
  configuration.fetch(key, default)
end

.key?(key) ⇒ Boolean

Detects whether the provided key is in the config.

Parameters

key<Object>

The key to check.

Returns

Boolean

True if the key exists in the config.

:api: public

Returns:

  • (Boolean)


54
55
56
# File 'lib/messagebird/config.rb', line 54

def key?(key)
  configuration.key?(key)
end

.method_missing(method, *args) ⇒ Object

Allows retrieval of single key config values via MessageBird::Config.<key> Allows single key assignment via Merb.config.<key> = …

Parameters

method<~to_s>

Method name as hash key value.

*args

Value to set the configuration parameter to.

Returns

The value of the entry fetched or assigned to.

:api: public



173
174
175
176
177
178
179
# File 'lib/messagebird/config.rb', line 173

def method_missing(method, *args)
  if method.to_s[-1,1] == '='
    self[method.to_s.tr('=','').to_sym] = args.first
  else
    self[method]
  end
end

.resetObject

Resets the configuration to its default state



97
98
99
# File 'lib/messagebird/config.rb', line 97

def reset
  setup
end

.setup(settings = {}) ⇒ Object

Sets up the configuration by storing the given settings.

Parameters

settings<Hash>

Configuration settings to use. These are merged with the defaults.

Returns

The configuration as a hash.

:api: private



136
137
138
# File 'lib/messagebird/config.rb', line 136

def setup(settings = {})
  @config = defaults.merge(settings)
end

.to_hashObject

Returns the configuration as a hash.

Returns

Hash

The config as a hash.

:api: public



122
123
124
# File 'lib/messagebird/config.rb', line 122

def to_hash
  configuration
end

.use {|configuration| ... } ⇒ Object

Yields the configuration.

Block parameters

c<Hash>

The configuration parameters.

Examples

MessageBird::Config.use do |config|
  config[:exception_details] = false
  config[:log_stream]        = STDOUT
end

Returns

nil

:api: public

Yields:



40
41
42
43
# File 'lib/messagebird/config.rb', line 40

def use
  yield configuration
  nil
end