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.2"

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)


43
44
45
# File 'lib/multi_sms.rb', line 43

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/multi_sms.rb', line 82

def execute(base_url, method, path, parameters, headers={ accept: :json}, &block)
  verify_parameters(parameters, [:from, :message, :to])

  # 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)



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

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

.get_provider(country_code) ⇒ Object



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

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

.loggerObject



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

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



101
102
103
104
105
# File 'lib/multi_sms.rb', line 101

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)



73
74
75
# File 'lib/multi_sms.rb', line 73

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

.send(country_code, parameters) ⇒ Object



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

def send(country_code, parameters)
  provider = get_provider country_code
  provider.send(parameters)
end