Class: LocaSMS::Client

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

Overview

Client to interact with LocaSMS API

Constant Summary collapse

DOMAIN =

Default API “domain”

'app.locasms.com.br'.freeze
ENDPOINT =

Default API address

{
  default:   "http://#{DOMAIN}/painel/api.ashx",
  shortcode: "http://#{DOMAIN}/shortcode/api.ashx"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login, password, opts = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • login (String)

    authorized user

  • password (String)

    access password

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :rest_client (Object) — default: RestClient

    client to be used to handle http requests



20
21
22
23
24
25
26
# File 'lib/locasms/client.rb', line 20

def initialize(, password, opts={})
  @login    = 
  @password = password
  @type     = opts[:type] || :default
  @rest     = opts[:rest_client]
  @callback = opts[:url_callback]
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



14
15
16
# File 'lib/locasms/client.rb', line 14

def callback
  @callback
end

#loginObject (readonly)

Returns the value of attribute login.



14
15
16
# File 'lib/locasms/client.rb', line 14

def 
  @login
end

#passwordObject (readonly)

Returns the value of attribute password.



14
15
16
# File 'lib/locasms/client.rb', line 14

def password
  @password
end

#typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/locasms/client.rb', line 14

def type
  @type
end

Instance Method Details

#balanceFixnum

Get de current amount of sending credits

Returns:

  • (Fixnum)

    returns the balance on success



62
63
64
# File 'lib/locasms/client.rb', line 62

def balance
  rest.get(:getbalance)['data']
end

#campaign_hold(id) ⇒ TrueClass, FalseClass

Holds the given campaign to fire

Parameters:

  • id (String)

    campaign id

Returns:

  • (TrueClass, FalseClass)

    returns true on success



102
103
104
# File 'lib/locasms/client.rb', line 102

def campaign_hold(id)
  rest.get(:holdsms, id: id)['data']
end

#campaign_release(id) ⇒ TrueClass, FalseClass

Restart firing the given campaign

Parameters:

  • id (String)

    campaign id

Returns:

  • (TrueClass, FalseClass)

    returns true on success



109
110
111
# File 'lib/locasms/client.rb', line 109

def campaign_release(id)
  rest.get(:releasesms, id: id)['data']
end

#campaign_status(id) ⇒ Array<Hash>

Gets the current status of the given campaign

Parameters:

  • id (String)

    campaign id

Returns:

  • (Array<Hash>)

    {campaign_id: id, delivery_id: delivery_id, enqueue_time: enqueue_time, delivery_time: delivery_time, status: status, carrier: carrier, mobile_number: mobile_number, message: message }



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
94
95
96
97
# File 'lib/locasms/client.rb', line 69

def campaign_status(id)
  response = rest.get(:getstatus, id: id)
  begin
    CSV.new(response['data'] || '', col_sep: ';', quote_char: '"').map do |delivery_id, _, enqueue_time, _, delivery_time, _, status, _, _, carrier, mobile_number, _, message|
      status = if status =~ /aguardando envio/i
        :waiting
      elsif status =~ /sucesso/i
        :success
      elsif status =~ /numero invalido|nao cadastrado/i
        :invalid
      else
        :unknown
      end

      {
        campaign_id: id,
        delivery_id: delivery_id,
        enqueue_time: enqueue_time,
        delivery_time: delivery_time,
        status: status,
        carrier: carrier,
        mobile_number: mobile_number,
        message: message
      }
    end
  rescue
    raise Exception.new 'Invalid delivery response data'
  end
end

#deliver(message, *mobiles, **opts) ⇒ String

Sends a message to one or more mobiles

Parameters:

  • message (String)

    message to be sent

  • mobiles (String, Array<String>)

    number of the mobiles to address the message

Returns:

  • (String)

    campaign id on success

Raises:



33
34
35
36
37
38
39
40
# File 'lib/locasms/client.rb', line 33

def deliver(message, *mobiles, **opts)
  attrs = {
    msg: message,
    numbers: numbers(mobiles),
    url_callback: callback
  }.merge(opts)
  rest.get(:sendsms, attrs)['data']
end

#deliver_at(message, datetime, *mobiles, **opts) ⇒ Object

Schedule the send of a message to one or more mobiles

Parameters:

  • message (String)

    message to be sent

  • datetime (Time, DateTime, Fixnum, String)
  • mobiles (String, Array<String>)

    number of the mobiles to address the message

Returns:

  • UNDEF

Raises:



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/locasms/client.rb', line 48

def deliver_at(message, datetime, *mobiles, **opts)
  date, time = Helpers::DateTimeHelper.split datetime
  attrs = {
    msg: message,
    numbers: numbers(mobiles),
    jobdate: date,
    jobtime: time,
    url_callback: callback
  }.merge(opts)
  rest.get(:sendsms, attrs)['data']
end

#numbers(*mobiles) ⇒ String (private)

Processes and returns all good numbers in a string

Parameters:

  • (Array<String>)

Returns:

  • (String)

Raises:



127
128
129
130
131
132
# File 'lib/locasms/client.rb', line 127

def numbers(*mobiles)
  numbers = Numbers.new mobiles
  return numbers.to_s unless numbers.bad?

  raise Exception("Bad numbers were given: #{numbers.bad.join(',')}")
end

#restRestClient (private)

Gets the current RestClient to handle http requests

Returns:

  • (RestClient)

    you can set on class creation passing it on the options



118
119
120
# File 'lib/locasms/client.rb', line 118

def rest
  @rest ||= RestClient.new ENDPOINT[type], lgn: , pwd: password
end