Class: LocaSMS::Client
- Inherits:
-
Object
- Object
- LocaSMS::Client
- 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
-
#callback ⇒ Object
readonly
Returns the value of attribute callback.
-
#login ⇒ Object
readonly
Returns the value of attribute login.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#balance ⇒ Fixnum
Get de current amount of sending credits.
-
#campaign_hold(id) ⇒ TrueClass, FalseClass
Holds the given campaign to fire.
-
#campaign_release(id) ⇒ TrueClass, FalseClass
Restart firing the given campaign.
-
#campaign_status(id) ⇒ Array<Hash>
Gets the current status of the given campaign.
-
#deliver(message, *mobiles, **opts) ⇒ String
Sends a message to one or more mobiles.
-
#deliver_at(message, datetime, *mobiles, **opts) ⇒ Object
Schedule the send of a message to one or more mobiles.
-
#initialize(login, password, opts = {}) ⇒ Client
constructor
A new instance of Client.
-
#numbers(*mobiles) ⇒ String
private
Processes and returns all good numbers in a string.
-
#rest ⇒ RestClient
private
Gets the current RestClient to handle http requests.
Constructor Details
#initialize(login, password, opts = {}) ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 26 |
# File 'lib/locasms/client.rb', line 20 def initialize(login, password, opts={}) @login = login @password = password @type = opts[:type] || :default @rest = opts[:rest_client] @callback = opts[:url_callback] end |
Instance Attribute Details
#callback ⇒ Object (readonly)
Returns the value of attribute callback.
14 15 16 |
# File 'lib/locasms/client.rb', line 14 def callback @callback end |
#login ⇒ Object (readonly)
Returns the value of attribute login.
14 15 16 |
# File 'lib/locasms/client.rb', line 14 def login @login end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
14 15 16 |
# File 'lib/locasms/client.rb', line 14 def password @password end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
14 15 16 |
# File 'lib/locasms/client.rb', line 14 def type @type end |
Instance Method Details
#balance ⇒ Fixnum
Get de current amount of sending credits
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
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
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
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, _, | 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: } end rescue raise Exception.new 'Invalid delivery response data' end end |
#deliver(message, *mobiles, **opts) ⇒ String
Sends a message to one or more mobiles
33 34 35 36 37 38 39 40 |
# File 'lib/locasms/client.rb', line 33 def deliver(, *mobiles, **opts) attrs = { msg: , 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
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/locasms/client.rb', line 48 def deliver_at(, datetime, *mobiles, **opts) date, time = Helpers::DateTimeHelper.split datetime attrs = { msg: , 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
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 |
#rest ⇒ RestClient (private)
Gets the current RestClient to handle http requests
118 119 120 |
# File 'lib/locasms/client.rb', line 118 def rest @rest ||= RestClient.new ENDPOINT[type], lgn: login, pwd: password end |