Class: ActionSms::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/action_sms/base.rb,
lib/action_sms/connections.rb

Direct Known Subclasses

SMSNotifier

Constant Summary collapse

@@logger =
nil
@@connection =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.connected?Boolean

Returns true if a connection that’s accessible to this class has already been opened.

Returns:

  • (Boolean)


9
10
11
# File 'lib/action_sms/connections.rb', line 9

def connected?
  return !@@connection.nil?
end

.connectionObject

Returns the connection currently associated with the class. This can also be used to “borrow” the connection to do work that is specific to a particular SMS gateway.



16
17
18
19
# File 'lib/action_sms/connections.rb', line 16

def connection
  raise ConnectionNotEstablished unless @@connection
  return @@connection
end

.connection=(spec) ⇒ Object

Set the gateway connection for the class.



22
23
24
25
# File 'lib/action_sms/connections.rb', line 22

def connection=(spec) #:nodoc:
  raise ConnectionNotEstablished unless spec
  @@connection = spec
end

.deliver(sms) ⇒ Object



6
7
8
# File 'lib/action_sms/base.rb', line 6

def self.deliver(sms)
  self.connection.deliver(sms)
end

.establish_connection(config) ⇒ Object

Establishes the connection to the SMS gateway. Accepts a hash as input where the :adapter key must be specified with the name of a gateway adapter (in lower-case)

ActionSms::Base.establish_connection(
  :adapter  => "clickatell",
  :username => "myusername",
  :password => "mypassword"
  :api_id   => "myapiid"
)

Also accepts keys as strings (for parsing from YAML, for example).

The exceptions AdapterNotSpecified, AdapterNotFound, and ArgumentError may be returned.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/action_sms/connections.rb', line 42

def establish_connection(config)
  config = config.symbolize_keys
  unless config.key?(:adapter)
    raise AdapterNotSpecified, "#{config} adapter is not configured"
  end
  adapter_method = "#{config[:adapter]}_connection"
  unless respond_to?(adapter_method)
    raise AdapterNotFound,
          "configuration specifies nonexistent #{config[:adapter]} adapter"
  end
  self.connection = self.send(adapter_method, config)
end

Instance Method Details

#connectionObject

Returns the connection currently associated with the class. This can also be used to “borrow” the connection to do work that is specific to a particular SMS gateway.



59
60
61
# File 'lib/action_sms/connections.rb', line 59

def connection
  self.class.connection
end