Module: MultiSms

Extended by:
Util
Defined in:
lib/multi_sms.rb,
lib/multi_sms/util.rb,
lib/multi_sms/version.rb

Defined Under Namespace

Modules: Util Classes: AuthError, BadRequest, BadResponse, ConfigError, MissingParameter, ServerError

Constant Summary collapse

VERSION =
"0.0.6"

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Util

verify_parameters

Class Attribute Details

.config {|configatron.multi_sms| ... } ⇒ Object

Yields up a configuration object when given a block. Without a block it just returns the configuration object. Uses Configatron under the covers.

Example:

MultiSms.config do |c|
  c.default_provider = "Elk"
end

MultiSms.config.foo # => :bar

Yields:

  • (configatron.multi_sms)


49
50
51
# File 'lib/multi_sms.rb', line 49

def config
  @config
end

Class Method Details

.execute(base_url, method, path, parameters, headers = { accept: :json}, &block) ⇒ Object

Wrapper around RestClient::RestClient.execute

  • Sets accept header to json

  • Handles some exceptions



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/multi_sms.rb', line 93

def execute(base_url, method, path, parameters, headers={ accept: :json}, &block)

  # Warn if the from string will be capped by the sms gateway
  if parameters[:from] && parameters[:from].match(/^(\w{11,})$/)
    warn "SMS 'from' value #{parameters[:from]} will be capped at 11 chars"
  end
  payload = {}.merge(parameters)
  url = base_url + path
  RestClient::Request.execute(:method => method, :url => url, :payload => payload, :headers => headers, &block)
rescue RestClient::Unauthorized
  raise AuthError, "Authentication failed"
rescue RestClient::InternalServerError
  raise ServerError, "Server error"
rescue RestClient::Forbidden => e
  raise BadRequest, e.http_body
end

.get(base_url, path, parameters = {}) ⇒ Object

Wrapper for MultiSms.execute(:get)



79
80
81
# File 'lib/multi_sms.rb', line 79

def get(base_url, path, parameters = {})
  execute(base_url, :get, path, parameters)
end

.get_provider(country_code) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/multi_sms.rb', line 70

def get_provider(country_code)
  name = config.providers[country_code.to_s]
  name = config.default_provider unless name.instance_of?(String)

  provider = "Providers::#{name}".split('::').inject(Object) {|o,c| o.const_get c}
  provider.new
end

.loggerObject



62
63
64
65
66
67
68
# File 'lib/multi_sms.rb', line 62

def logger
  @logger ||= begin
    log = Logger.new($stdout)
    log.level = Logger::INFO
    log
  end
end

.parse_json(body) ⇒ Object

Wrapper around MultiJson.load, symbolize names



111
112
113
114
115
# File 'lib/multi_sms.rb', line 111

def parse_json(body)
  MultiJson.load(body, :symbolize_keys => true)
rescue MultiJson::DecodeError
  raise BadResponse, "Can't parse JSON"
end

.post(base_url, path, parameters = {}) ⇒ Object

Wrapper for MultiSms::execute(:post)



84
85
86
# File 'lib/multi_sms.rb', line 84

def post(base_url, path, parameters = {})
  execute(base_url, :post, path, parameters)
end

.providersObject

Responds with all the currently supported providers



35
36
37
# File 'lib/multi_sms.rb', line 35

def providers
  %W(Clickatell Elk Twilio)
end

.send(country_code, parameters) ⇒ Object

Raises:



54
55
56
57
58
59
60
# File 'lib/multi_sms.rb', line 54

def send(country_code, parameters)
  raise ConfigError, "MultiSms needs a default provider configured." unless config.default_provider.instance_of?(String)
  verify_parameters(parameters, [:from, :message, :to])

  provider = get_provider country_code
  provider.send(parameters)
end