Class: ZenSend::Client

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

Constant Summary collapse

ZENSEND_URL =
"https://api.zensend.io"
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) ⇒ Client

Returns a new instance of Client.



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

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

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#http_optsObject

Returns the value of attribute http_opts.



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

def http_opts
  @http_opts
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#check_balanceObject



68
69
70
71
# File 'lib/zensend/client.rb', line 68

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

#get_pricesObject



73
74
75
76
# File 'lib/zensend/client.rb', line 73

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

#lookup_operator(msisdn) ⇒ Object



78
79
80
81
82
# File 'lib/zensend/client.rb', line 78

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

#send_sms(options) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/zensend/client.rb', line 40

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