Class: Smshelper::Api::Nexmo

Inherits:
Base
  • Object
show all
Defined in:
lib/smshelper/api/nexmo.rb

Instance Attribute Summary

Attributes inherited from Base

#extra_options, #sent_message_ids, #sent_message_statuses

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Nexmo

Returns a new instance of Nexmo.



7
8
9
10
11
# File 'lib/smshelper/api/nexmo.rb', line 7

def initialize(*args)
  config = args.shift
  @uname, @passwd = config.nexmo[:uname], config.nexmo[:passwd]
  super
end

Instance Method Details

#get_balanceObject



28
29
30
# File 'lib/smshelper/api/nexmo.rb', line 28

def get_balance
  {'EUR' => (get "account/get-balance/#{@uname}/#{@passwd}").values.last.to_s}
end

#get_callback_response(args = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/smshelper/api/nexmo.rb', line 36

def get_callback_response(args = {})
  if args['type']
    InboundMessage.new(
                       :message_id => args['messageId'],
                       :sender => args['msisdn'],
                       :recipient => args['to'],
                       :text => args['text'],
                       :timestamp => Time.parse(args['message-timestamp']),
                       :original_params => args
                       )
  elsif args['network-code']
    DeliveryReport.new(
                       :message_id => args['messageId'],
                       :timestamp => Time.parse(args['message-timestamp']),
                       :delivered => ((args['status'] == 'delivered') ? true : false),
                       :original_params => args
                       )
  else
    UnknownReply.new(args)
  end
end

#get_status(message_id) ⇒ Object



32
33
34
# File 'lib/smshelper/api/nexmo.rb', line 32

def get_status(message_id)
  raise NotImplementedError, "Sms status checks unsupported by #{self.class.name}"
end

#send_message(message) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/smshelper/api/nexmo.rb', line 13

def send_message(message)
  message.utf_8 ? (q = {:type => 'unicode'}) : (q = {:type => 'text'})

  options = {
    :username => @uname,
    :password => @passwd,
    :to => message.recipient,
    :text => message.text,
    :from => message.sender}
  options = options.merge(@extra_options) unless @extra_options.nil?
  options = options.merge(q)
  resp = (post 'sms/json', :extra_query => options)
  process_response_code(resp['messages'].collect{|m| m['status']}.first) ? (@sent_message_ids << resp['messages'].collect{|m| m['message-id']}.first; resp['messages'].collect{|m| m['message-id']}.first) : (raise ErrorDuringSend, 'No response code provided by Nexmo!')
end