Class: MegafonSms::Client
- Inherits:
-
Object
- Object
- MegafonSms::Client
- Defined in:
- lib/megafon_sms/client.rb
Constant Summary collapse
- URL =
"https://a2p-api.megalabs.ru/sms/v1/sms"
Instance Attribute Summary collapse
-
#adapter ⇒ Object
Returns the value of attribute adapter.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#login ⇒ Object
readonly
Returns the value of attribute login.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #failure? ⇒ Boolean
-
#initialize(login, password) ⇒ Client
constructor
A new instance of Client.
- #send(from:, to:, message:, callback_url: nil, msg_id: nil) ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(login, password) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/megafon_sms/client.rb', line 15 def initialize(login, password) @adapter = Faraday.default_adapter @login = login @password = password @connection = Faraday.new do |conn| conn.response :json, content_type: /\bjson$/ conn.adapter adapter end end |
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
12 13 14 |
# File 'lib/megafon_sms/client.rb', line 12 def adapter @adapter end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
13 14 15 |
# File 'lib/megafon_sms/client.rb', line 13 def connection @connection end |
#login ⇒ Object (readonly)
Returns the value of attribute login.
13 14 15 |
# File 'lib/megafon_sms/client.rb', line 13 def login @login end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
13 14 15 |
# File 'lib/megafon_sms/client.rb', line 13 def password @password end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
13 14 15 |
# File 'lib/megafon_sms/client.rb', line 13 def response @response end |
Instance Method Details
#failure? ⇒ Boolean
37 38 39 |
# File 'lib/megafon_sms/client.rb', line 37 def failure? !success? end |
#send(from:, to:, message:, callback_url: nil, msg_id: nil) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/megafon_sms/client.rb', line 26 def send(from:, to:, message:, callback_url: nil, msg_id: nil) body = { from: from, to: to.to_i, message: .to_s, callback_url: callback_url, msg_id: msg_id }.to_json @response = connection.post(URL, body, headers) rescue StandardError => e raise MegafonSms::Error, e end |
#success? ⇒ Boolean
33 34 35 |
# File 'lib/megafon_sms/client.rb', line 33 def success? response && response.status == 200 && response.body["result"]["status"]["code"] == 0 end |