Class: ActionTexter::NexmoClient

Inherits:
Client
  • Object
show all
Defined in:
lib/action_texter/nexmo.rb

Overview

Implementation of client for Nexmo: nexmo.com

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Client

default, default=, setup

Constructor Details

#initialize(key, secret) ⇒ NexmoClient

Create a new Nexmo client with key and secret.

Parameters:

  • key (String)

    key as specified by Nexmo for authenticating.

  • secret (String)

    secret as specified by Nexmo for authenticating.


63
64
65
66
67
# File 'lib/action_texter/nexmo.rb', line 63

def initialize(key, secret)
  super()
  self.key = key
  self.secret = secret
end

Instance Attribute Details

#keyObject

Returns the value of attribute key


57
58
59
# File 'lib/action_texter/nexmo.rb', line 57

def key
  @key
end

#secretObject

Returns the value of attribute secret


57
58
59
# File 'lib/action_texter/nexmo.rb', line 57

def secret
  @secret
end

Instance Method Details

#deliver(message) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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_sObject


93
94
95
# File 'lib/action_texter/nexmo.rb', line 93

def to_s
  "#<#{self.class.name}:#{key}>"
end