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, MissingParameter, ServerError

Constant Summary collapse

VERSION =
"0.0.3"

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.foo = :bar
end

MultiSms.config.foo # => :bar

Yields:

  • (configatron.multi_sms)


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

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/multi_sms.rb', line 89

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)



75
76
77
# File 'lib/multi_sms.rb', line 75

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

.get_provider(country_code) ⇒ Object



67
68
69
70
71
72
# File 'lib/multi_sms.rb', line 67

def get_provider(country_code)
  name = config.providers[country_code.to_s]
  name = config.default_provider unless name
  provider = "Providers::#{name}".split('::').inject(Object) {|o,c| o.const_get c}
  provider.new
end

.loggerObject



59
60
61
62
63
64
65
# File 'lib/multi_sms.rb', line 59

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



107
108
109
110
111
# File 'lib/multi_sms.rb', line 107

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)



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

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

.providersObject

Responds with all the currently supported providers



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

def providers
  %W(Clickatell Elk Twilio)
end

.send(country_code, parameters) ⇒ Object



53
54
55
56
57
# File 'lib/multi_sms.rb', line 53

def send(country_code, parameters)
  verify_parameters(parameters, [:from, :message, :to])
  provider = get_provider country_code
  provider.send(parameters)
end