class ActionTexter::NexmoClient

Implementation of client for Nexmo: nexmo.com

Attributes

key[RW]
secret[RW]

Public Class Methods

new(key, secret) click to toggle source

Create a new Nexmo client with key and secret.

@param [String] key key as specified by Nexmo for authenticating. @param [String] secret secret as specified by Nexmo for authenticating.

Calls superclass method
# File lib/action_texter/nexmo.rb, line 63
def initialize(key, secret)
  super()
  self.key = key
  self.secret = secret
end

Public Instance Methods

deliver(message) click to toggle source
# File lib/action_texter/nexmo.rb, line 69
def deliver(message)
  client = Net::HTTP.new("rest.nexmo.com", 443)
  client.use_ssl = true

  # Nexmo doesn't like phone numbers starting with a +
  # Pattern only matches phones that are pristine phone numbers starting with a +, and leaves everything else alone
  pattern = /^\+(\d+)$/
  from = (message.from =~ pattern ? message.from.gsub(pattern, '\1')  : message.from )
  to = (message.to =~ pattern ? message.to.gsub(pattern, '\1')  : message.to )

  response = client.post(
      "/sms/json",
      URI.encode_www_form("username" => @key,
                          "password" => @secret,
                          "from" => from,
                          "to" => to,
                          "text" => message.text,
                          "client-ref" => message.reference),
      {"Content-Type" => "application/x-www-form-urlencoded"})

  return ActionTexter::NexmoResponse.new(response.body)
end
to_s() click to toggle source

@private

# File lib/action_texter/nexmo.rb, line 93
def to_s
  "#<#{self.class.name}:#{key}>"
end