Class: MegafonSms::Client

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

Constant Summary collapse

URL =
"https://a2p-api.megalabs.ru/sms/v1/sms"

Instance Attribute Summary collapse

Instance Method Summary collapse

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(, password)
  @adapter = Faraday.default_adapter
  @login = 
  @password = password

  @connection = Faraday.new do |conn|
    conn.response :json, content_type: /\bjson$/
    conn.adapter adapter
  end
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



12
13
14
# File 'lib/megafon_sms/client.rb', line 12

def adapter
  @adapter
end

#connectionObject (readonly)

Returns the value of attribute connection.



13
14
15
# File 'lib/megafon_sms/client.rb', line 13

def connection
  @connection
end

#loginObject (readonly)

Returns the value of attribute login.



13
14
15
# File 'lib/megafon_sms/client.rb', line 13

def 
  @login
end

#passwordObject (readonly)

Returns the value of attribute password.



13
14
15
# File 'lib/megafon_sms/client.rb', line 13

def password
  @password
end

#responseObject (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

Returns:

  • (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: 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

Returns:

  • (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