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'
- 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.
21 22 23 24 25 26 27 |
# File 'lib/locasms/client.rb', line 21 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.
15 16 17 |
# File 'lib/locasms/client.rb', line 15 def callback @callback end |
#login ⇒ Object (readonly)
Returns the value of attribute login.
15 16 17 |
# File 'lib/locasms/client.rb', line 15 def login @login end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
15 16 17 |
# File 'lib/locasms/client.rb', line 15 def password @password end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
15 16 17 |
# File 'lib/locasms/client.rb', line 15 def type @type end |
Instance Method Details
#balance ⇒ Fixnum
Get de current amount of sending credits
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
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
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
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, _, | # 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: } 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
34 35 36 37 38 39 40 41 |
# File 'lib/locasms/client.rb', line 34 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
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/locasms/client.rb', line 49 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
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 |
#rest ⇒ RestClient (private)
Gets the current RestClient to handle http requests
120 121 122 |
# File 'lib/locasms/client.rb', line 120 def rest @rest ||= RestClient.new ENDPOINT[type], lgn: login, pwd: password end |