class Semaphore::Sms::Client

Constants

BASE_URI
Response

Public Class Methods

new(config) click to toggle source
# File lib/semaphore/sms/client.rb, line 15
def initialize(config)
  raise ConfigurationError, 'Config must have api key credentials' unless config.respond_to? :api_key
  @config = config
end

Public Instance Methods

account() click to toggle source
# File lib/semaphore/sms/client.rb, line 58
def account
  api_get("account")
end
messages(id: nil, page: nil, limit: nil, start_date: nil, end_date: nil, network: nil, status: nil) click to toggle source
# File lib/semaphore/sms/client.rb, line 46
def messages(id: nil, page: nil, limit: nil, start_date: nil, end_date: nil, network: nil, status: nil)
  options = {
    page: page,
    limit: limit,
    startDate: start_date,
    endDate: end_date,
    network: network,
    status: status
  }.compact
  api_get("messages#{id.nil? ? "" : "/#{id}"}", options)
end
priority(message, recipients, sendername = nil) click to toggle source
# File lib/semaphore/sms/client.rb, line 33
def priority(message, recipients, sendername = nil)
  recipients = clean_and_validate(recipients)

  raise PhoneNumberError, 'Please verify the phonenumber' unless recipients

  options = {
    message: message,
    sendername: sendername || sender_name,
    number: recipients.kind_of?(Array) ? recipients.join(",") : recipients
  }.compact
  api_post("priority", options)
end
send(message, recipients, sendername = nil) click to toggle source
# File lib/semaphore/sms/client.rb, line 20
def send(message, recipients, sendername = nil)
  recipients = clean_and_validate(recipients)

  raise PhoneNumberError, 'Please verify the phonenumber' unless recipients

  options = {
    message: message,
    sendername: sendername || sender_name,
    number: recipients
  }.compact
  api_post("messages", options)
end
sender_names(page: nil, limit: nil) click to toggle source
# File lib/semaphore/sms/client.rb, line 70
def sender_names(page: nil, limit: nil)
  options = {
    page: page,
    limit: limit
  }.compact
  api_get("account/sendernames", options)
end
transactions(page: nil, limit: nil) click to toggle source
# File lib/semaphore/sms/client.rb, line 62
def transactions(page: nil, limit: nil)
  options = {
    page: page,
    limit: limit
  }.compact
  api_get("account/transactions", options)
end
users(page: nil, limit: nil) click to toggle source
# File lib/semaphore/sms/client.rb, line 78
def users(page: nil, limit: nil)
  options = {
    page: page,
    limit: limit
  }.compact
  api_get("account/users", options)
end

Private Instance Methods

api_get(uri, params = {}) click to toggle source
# File lib/semaphore/sms/client.rb, line 99
def api_get(uri, params = {})
  handle_errors do
    resp = Curl.get("#{BASE_URI}#{uri}", params.merge(apikey: api_key))
    JSON.parse(resp.body_str)
  end
end
api_post(uri, params = {}) click to toggle source
# File lib/semaphore/sms/client.rb, line 92
def api_post(uri, params = {})
  handle_errors do
    resp = Curl.post("#{BASE_URI}#{uri}", params.merge(apikey: api_key))
    JSON.parse(resp.body_str)
  end
end
clean(num) click to toggle source
# File lib/semaphore/sms/client.rb, line 127
def clean(num)
  Piliponi.clean num
end
clean_and_validate(recipients) click to toggle source
# File lib/semaphore/sms/client.rb, line 106
def clean_and_validate(recipients)
  if recipients.kind_of?(Array)
    recipients = recipients.reduce([]) do |mem, num|
      mem << (validate(num) ? clean(num) : false)
      mem
    end
    recipients.include?(false) ? false : recipients.join(",")
  else
    recipients = clean(recipients)
    validate(recipients) ? recipients : false
  end
end
handle_errors() { || ... } click to toggle source
# File lib/semaphore/sms/client.rb, line 119
def handle_errors
  begin
    yield
  rescue => e
    Response.new(:error, { error: e.message })
  end
end
validate(num) click to toggle source
# File lib/semaphore/sms/client.rb, line 131
def validate(num)
  Piliponi.plausible? num
end