Class: BabySMS::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/babysms/adapter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from:) ⇒ Adapter

Returns a new instance of Adapter.



6
7
8
# File 'lib/babysms/adapter.rb', line 6

def initialize(from:)
  @from = Phony.normalize(from)
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/babysms/adapter.rb', line 4

def client
  @client
end

#fromObject (readonly)

Returns the value of attribute from.



3
4
5
# File 'lib/babysms/adapter.rb', line 3

def from
  @from
end

Class Method Details

.adapter_nameObject



20
21
22
23
# File 'lib/babysms/adapter.rb', line 20

def self.adapter_name
  bare_name = name.split('::').last
  bare_name.gsub(/Adapter\z/, '').downcase
end

.for_adapter_id(adapter_id, pool: BabySMS.adapters) ⇒ Object



16
17
18
# File 'lib/babysms/adapter.rb', line 16

def self.for_adapter_id(adapter_id, pool: BabySMS.adapters)
  pool.find { |adapter| adapter.adapter_id == adapter_id }
end

.for_number(number, pool: BabySMS.adapters) ⇒ Object

Locates an adapter instance associated with a phone number



11
12
13
14
# File 'lib/babysms/adapter.rb', line 11

def self.for_number(number, pool: BabySMS.adapters)
  number = Phony.normalize(number)
  pool.find { |adapter| adapter.from == number }
end

Instance Method Details

#adapter_idObject

e.g., “15555555555@twilio”



31
32
33
# File 'lib/babysms/adapter.rb', line 31

def adapter_id
  "#{from}@#{adapter_name}"
end

#adapter_nameObject

e.g., “twilio”



26
27
28
# File 'lib/babysms/adapter.rb', line 26

def adapter_name
  self.class.adapter_name
end

#web_hookObject

Returns an instance of the web hook handler, if it exists



46
47
48
49
50
51
52
53
54
# File 'lib/babysms/adapter.rb', line 46

def web_hook
  unless instance_variable_defined?(:@web_hook)
    @web_hook = if (cls = web_hook_class)
      cls.new(self)
    end
  end

  @web_hook
end

#web_hook?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/babysms/adapter.rb', line 41

def web_hook?
  !!web_hook_class
end

#web_hook_classObject

if the adapter supports web hooks, it’ll have a nested class called “WebHook”. This returns the class



37
38
39
# File 'lib/babysms/adapter.rb', line 37

def web_hook_class
  self.class.const_get(:WebHook)
end