Class: ZenSend::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zensend/client.rb

Constant Summary collapse

ZENSEND_URL =
"https://api.zensend.io"
VERIFY_URL =
"https://verify.zensend.io"
VERIFY_OPTIONS =
[:message, :originator]
VALID_OPTIONS =

Sends an sms message

Named paramters:

originator: the originator to send from
body: the body of the sms message

  numbers: an array of numbers to send to. they must not contain the ‘,’ character

originator_type: :alpha or :msisdn (not required)
timetolive_in_minutes: number of minutes before message expires (not required)
encoding: :ucs2 or :gsm (not required defaults to automatic)

A ZenSend::ZenSendException or StandardError or ArgumentError can be raised by this method. StandardError is raised by net/http. It may be one of the following subclasses:

Errno::ETIMEDOUT, Errno::ECONNRESET, Errno::EHOSTUNREACH, SocketError, Net::ReadTimeout, Net::OpenTimeout

This list is not exhaustive.

An ArgumentError can be raised if any of the numbers includes a ‘,’ character or a required parameter is not specified or an unknown parameter is specified.

[:originator, :body, :numbers, :originator_type, :timetolive_in_minutes, :encoding]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, http_opts = {open_timeout: 60, read_timeout: 60}, url = ZENSEND_URL, verify_url = VERIFY_URL) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
# File 'lib/zensend/client.rb', line 14

def initialize(api_key, http_opts = {open_timeout: 60, read_timeout: 60}, url = ZENSEND_URL, verify_url = VERIFY_URL)
  @api_key = api_key
  @url = url
  @http_opts = http_opts
  @verify_url = verify_url
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



12
13
14
# File 'lib/zensend/client.rb', line 12

def api_key
  @api_key
end

#http_optsObject

Returns the value of attribute http_opts.



12
13
14
# File 'lib/zensend/client.rb', line 12

def http_opts
  @http_opts
end

#urlObject

Returns the value of attribute url.



12
13
14
# File 'lib/zensend/client.rb', line 12

def url
  @url
end

Instance Method Details

#check_balanceObject



95
96
97
98
# File 'lib/zensend/client.rb', line 95

def check_balance
  response = make_request("/v3/checkbalance", :get)
  response["balance"]
end

#create_msisdn_verification(msisdn, verify_options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/zensend/client.rb', line 23

def create_msisdn_verification(msisdn, verify_options = {})
  params = {}
  params["NUMBER"] = msisdn
  add_optional_param(params, verify_options, "MESSAGE", :message)
  add_optional_param(params, verify_options, "ORIGINATOR", :originator)

  verify_options.keys.each do |key|
    raise ArgumentError.new("unknown option: #{key}") if !VERIFY_OPTIONS.include?(key)
  end

  result = make_request("/api/msisdn_verify", :post, params, @verify_url)

  result["session"]
end

#get_pricesObject



100
101
102
103
# File 'lib/zensend/client.rb', line 100

def get_prices
  response = make_request("/v3/prices", :get)
  response["prices_in_pence"]
end

#lookup_operator(msisdn) ⇒ Object



105
106
107
108
109
# File 'lib/zensend/client.rb', line 105

def lookup_operator(msisdn)
  operator_lookup_response = OperatorLookupResponse.new
  operator_lookup_response.set_from_response(make_request("/v3/operator_lookup?" + URI.encode_www_form({"NUMBER" => msisdn}), :get))
  operator_lookup_response
end

#msisdn_verification_status(session) ⇒ Object



38
39
40
41
42
# File 'lib/zensend/client.rb', line 38

def msisdn_verification_status(session)
  params = {"SESSION" => session}
  json = make_request("/api/msisdn_verify?" + URI.encode_www_form(params), :get, nil, @verify_url)
  json["msisdn"]
end

#send_sms(options) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/zensend/client.rb', line 67

def send_sms(options)

  originator = required_param(options, :originator)
  body = required_param(options, :body)
  numbers = required_param(options, :numbers)

  raise ArgumentError.new("invalid character in numbers") if numbers.any? {|x| x.include?(",")}


  options.keys.each do |key|
    raise ArgumentError.new("unknown option: #{key}") if !VALID_OPTIONS.include?(key)
  end

  sms_response = SmsResponse.new

  params = {"ORIGINATOR" => originator, "BODY" => body, "NUMBERS" => numbers.join(",")}
  
  add_optional_param(params, options, "ORIGINATOR_TYPE", :originator_type)
  add_optional_param(params, options, "TIMETOLIVE", :timetolive_in_minutes)
  add_optional_param(params, options, "ENCODING", :encoding)

  

  sms_response.set_from_response(make_request("/v3/sendsms", :post, params))

  sms_response
end