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'
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



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

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.



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

def callback
  @callback
end

#loginObject (readonly)

Returns the value of attribute login.



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

def 
  @login
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#balanceFixnum

Get de current amount of sending credits

Returns:

  • (Fixnum)

    returns the balance on success



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

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



104
105
106
# File 'lib/locasms/client.rb', line 104

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



111
112
113
# File 'lib/locasms/client.rb', line 111

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 }



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
98
99
# File 'lib/locasms/client.rb', line 73

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| # rubocop:disable Metrics/ParameterLists
      status = case status
               when /aguardando envio/i
                 waiting
               when /sucesso/i
                 success
               when /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 StandardError
    raise LocaSMS::Exception.new(message: '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:



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

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:



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

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:



129
130
131
132
133
134
# File 'lib/locasms/client.rb', line 129

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

  raise LocaSMS::Exception.new(message: "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



120
121
122
# File 'lib/locasms/client.rb', line 120

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