Class: SendWithUs::Config

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

Constant Summary collapse

DEFAULT_URL =

attr_writer :url, :api_key, :protocol, :host, :port, :api_version, :debug

'https://api.sendwithus.com'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



25
26
27
# File 'lib/send_with_us/config.rb', line 25

def initialize(options={})
  @settings = SendWithUs::Config.defaults.merge(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/send_with_us/config.rb', line 29

def method_missing(meth, *args, &block)
  meth_str = meth.to_s

  if meth_str.include?('=')
    # If this is a write attempt, see if we can write to that key
    meth_sym = meth_str.gsub('=', '').to_sym
    has?(meth_sym) ? @settings[meth_sym] = args[0] : super
  else
    # It's a read attempt, see if that key exists
    has?(meth) ? @settings[meth] : super
  end
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



5
6
7
# File 'lib/send_with_us/config.rb', line 5

def settings
  @settings
end

Class Method Details

.defaultsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/send_with_us/config.rb', line 10

def self.defaults
  source = URI.parse(DEFAULT_URL)

  {
    url: DEFAULT_URL,
    api_key:      nil,
    protocol:     source.scheme,
    host:         source.host,
    port:         source.port,
    api_version:  '1',
    debug:        true,
    client_stub:  "ruby-#{VERSION}"
  }
end

Instance Method Details

#respond_to_missing?(meth, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/send_with_us/config.rb', line 42

def respond_to_missing?(meth, include_private = false)
  has?(meth) || super
end