Class: ActionSms::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/action_sms/base.rb,
lib/action_sms/connection_adapters/tropo_adapter.rb,
lib/action_sms/connection_adapters/sms_global_adapter.rb

Constant Summary collapse

@@connection =
nil

Class Method Summary collapse

Class Method Details

.adapters(adapter_method, config = {}) ⇒ Object

Returns all adapters that respond to the given method



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/action_sms/base.rb', line 8

def adapters(adapter_method, config = {})
  config = connection.configuration if config.empty? && connected?
  adapters = []
  methods.each do |method|
    if method.to_s =~ /\_connection$/
      begin
        adapter = send(method, config)
      rescue
      end
      adapters << adapter if adapter && adapter.respond_to?(adapter_method)
    end
  end
  adapters
end

.authenticate(params) ⇒ Object

Adapter Helper Methods



70
71
72
# File 'lib/action_sms/base.rb', line 70

def authenticate(params)
  adapter_method_result(:authenticate, params)
end

.authentication_keyObject



74
75
76
# File 'lib/action_sms/base.rb', line 74

def authentication_key
  connection.authentication_key
end

.authentication_key=(value) ⇒ Object



78
79
80
# File 'lib/action_sms/base.rb', line 78

def authentication_key=(value)
  connection.authentication_key = value
end

.connected?Boolean

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

Returns:

  • (Boolean)


25
26
27
# File 'lib/action_sms/base.rb', line 25

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.



32
33
34
35
# File 'lib/action_sms/base.rb', line 32

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

.connection=(spec) ⇒ Object

Set the gateway connection for the class.



38
39
40
41
# File 'lib/action_sms/base.rb', line 38

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

.deliver(sms, options = {}) ⇒ Object



82
83
84
# File 'lib/action_sms/base.rb', line 82

def deliver(sms, options = {})
  connection.deliver(sms, options)
end

.delivery_request_successful?(delivery_request) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/action_sms/base.rb', line 86

def delivery_request_successful?(delivery_request)
  connection.delivery_request_successful?(delivery_request)
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"
)

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



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/action_sms/base.rb', line 56

def establish_connection(config)
  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

.message_id(data) ⇒ Object



90
91
92
# File 'lib/action_sms/base.rb', line 90

def message_id(data)
  adapter_method_result(:message_id, data)
end

.message_text(params) ⇒ Object



94
95
96
# File 'lib/action_sms/base.rb', line 94

def message_text(params)
  adapter_method_result(:message_text, params)
end

.sample_configuration(options = {}) ⇒ Object

Test Helper Methods



120
121
122
# File 'lib/action_sms/base.rb', line 120

def sample_configuration(options = {})
  connection.sample_configuration(options)
end

.sample_delivery_receipt(options = {}) ⇒ Object



124
125
126
# File 'lib/action_sms/base.rb', line 124

def sample_delivery_receipt(options = {})
  adapter_method_result(:sample_delivery_receipt, options)
end

.sample_delivery_response(options = {}) ⇒ Object



128
129
130
# File 'lib/action_sms/base.rb', line 128

def sample_delivery_response(options = {})
  connection.sample_delivery_response(options)
end

.sample_delivery_response_with_message_id(message_id, options = {}) ⇒ Object



132
133
134
135
136
# File 'lib/action_sms/base.rb', line 132

def sample_delivery_response_with_message_id(message_id, options = {})
  adapter_method_result(
    :sample_delivery_response_with_message_id, message_id, options
  )
end

.sample_incoming_sms(options = {}) ⇒ Object



138
139
140
# File 'lib/action_sms/base.rb', line 138

def sample_incoming_sms(options = {})
  adapter_method_result(:sample_incoming_sms, options)
end

.sample_message_id(options = {}) ⇒ Object



142
143
144
# File 'lib/action_sms/base.rb', line 142

def sample_message_id(options = {})
  adapter_method_result(:sample_message_id, options)
end

.sender(params) ⇒ Object



98
99
100
# File 'lib/action_sms/base.rb', line 98

def sender(params)
  adapter_method_result(:sender, params)
end

.service_urlObject



102
103
104
# File 'lib/action_sms/base.rb', line 102

def service_url
  connection.service_url
end

.sms_global_connection(config) ⇒ Object

:nodoc:



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/action_sms/connection_adapters/sms_global_adapter.rb', line 5

def self.sms_global_connection(config) #:nodoc:
  if config[:environment].to_s == "test"
    test_helper = File.expand_path(File.dirname(__FILE__) + '/test_helpers/sms_global')
    if File.exists?("#{test_helper}.rb")
      require test_helper
      ConnectionAdapters::SMSGlobalAdapter.class_eval do
        include ActionSms::ConnectionAdapters::TestHelpers::SMSGlobal
      end
    end
  end
  ConnectionAdapters::SMSGlobalAdapter.new(config)
end

.status(params) ⇒ Object



106
107
108
# File 'lib/action_sms/base.rb', line 106

def status(params)
  adapter_method_result(:status, params)
end

.tropo_connection(config) ⇒ Object

:nodoc:



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/action_sms/connection_adapters/tropo_adapter.rb', line 5

def self.tropo_connection(config) #:nodoc:
  if config[:environment].to_s == "test"
    test_helper = File.expand_path(File.dirname(__FILE__) + '/test_helpers/tropo')
    if File.exists?("#{test_helper}.rb")
      require test_helper
      ConnectionAdapters::TropoAdapter.class_eval do
        include ActionSms::ConnectionAdapters::TestHelpers::Tropo
      end
    end
  end
  ConnectionAdapters::TropoAdapter.new(config)
end

.use_sslObject



110
111
112
# File 'lib/action_sms/base.rb', line 110

def use_ssl
  connection.use_ssl
end

.use_ssl=(value) ⇒ Object



114
115
116
# File 'lib/action_sms/base.rb', line 114

def use_ssl=(value)
  connection.use_ssl = value
end