Class: Smsbd::Sms
- Inherits:
-
Object
- Object
- Smsbd::Sms
- Defined in:
- lib/smsbd.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
Instance Method Summary collapse
- #balance ⇒ Object
-
#initialize(api_key = nil) ⇒ Sms
constructor
A new instance of Sms.
- #report(id) ⇒ Object
- #send(msg, receiver) ⇒ Object
Constructor Details
#initialize(api_key = nil) ⇒ Sms
Returns a new instance of Sms.
13 14 15 16 17 |
# File 'lib/smsbd.rb', line 13 def initialize(api_key=nil) @api_key = ENV["SMS_API_KEY"] @base_url = 'https://api.sms.net.bd' @headers = { 'Content-Type': 'application/json'} end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
11 12 13 |
# File 'lib/smsbd.rb', line 11 def base_url @base_url end |
Instance Method Details
#balance ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/smsbd.rb', line 35 def balance uri = URI(self.base_url + '/user/balance') body = {"api_key": @api_key} uri.query = URI.encode_www_form(body) response = Net::HTTP.get_response(uri) JSON.parse(response.body) end |
#report(id) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/smsbd.rb', line 43 def report(id) uri = URI(self.base_url + '/report/request/' + id.to_s) body = {"api_key": @api_key} uri.query = URI.encode_www_form(body) response = Net::HTTP.get_response(uri) JSON.parse(response.body) end |
#send(msg, receiver) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/smsbd.rb', line 19 def send(msg, receiver) body = {"api_key": @api_key , "msg": msg, "to": receiver} puts body base_url = self.base_url + '/sendsms' response = Net::HTTP.post(URI(base_url), body.to_json, @headers) parsed = JSON.parse(response.body) case parsed["error"] when 0 "Success." when 405 "Authorization required." else "Error: #{parsed["error"]}:#{parsed["msg"]}" end end |