Class: RtrPushApi::PushApi
- Inherits:
-
Object
- Object
- RtrPushApi::PushApi
- Defined in:
- lib/push_api/rtr_push_api.rb
Instance Method Summary collapse
- #do_json_request(data) ⇒ Object
-
#initialize(username, password, service_key, url = 'https://connect.runthered.com:10443/public_api/service') ⇒ PushApi
constructor
A new instance of PushApi.
-
#push_message(message, to, from_number = nil, push_id = 1) ⇒ PushApiResponse
Send a message to Run The Red.
-
#query_dlr(msg_id, push_id = 1) ⇒ DlrQueryResponse
Query a delivery receipt using the message id supplied by Run The Red.
Constructor Details
#initialize(username, password, service_key, url = 'https://connect.runthered.com:10443/public_api/service') ⇒ PushApi
Returns a new instance of PushApi.
31 32 33 34 35 36 |
# File 'lib/push_api/rtr_push_api.rb', line 31 def initialize(username, password, service_key, url='https://connect.runthered.com:10443/public_api/service') @url = url @username = username @password = password @service_key = service_key end |
Instance Method Details
#do_json_request(data) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/push_api/rtr_push_api.rb', line 38 def do_json_request(data) uri = URI.parse(@url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.scheme == 'https' request = Net::HTTP::Post.new(uri.request_uri) request.body = data.to_json request.basic_auth @username, @password response = http.request(request) if response.code != '200' raise PushApiException, response.body else data = JSON.parse response.body if !data.has_key?("result") error = data['error'] = error['message'] code = error['code'] raise PushApiException, end return data end return response end |
#push_message(message, to, from_number = nil, push_id = 1) ⇒ PushApiResponse
Send a message to Run The Red
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/push_api/rtr_push_api.rb', line 68 def (, to, from_number=nil, push_id=1) json_data = {"jsonrpc" => "2.0", "method" => "sendsms", "params" => {"service_key" => @service_key, "to" => to, "body" => }, "id" => push_id} unless from_number.nil? json_data["params"]["frm"] = from_number end data = do_json_request(json_data) push_id = data['id'] result = data['result'] status = result['status'] msg_id = result['msg_id'] return PushApiResponse.new(status, msg_id, push_id) end |
#query_dlr(msg_id, push_id = 1) ⇒ DlrQueryResponse
Query a delivery receipt using the message id supplied by Run The Red
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/push_api/rtr_push_api.rb', line 85 def query_dlr(msg_id, push_id=1) json_data = {"jsonrpc" => "2.0", "method" => "querydlr", "params" => {"service_key" => @service_key, "msg_id" => msg_id}, "id" => push_id} data = do_json_request(json_data) push_id = data['id'] result = data['result'] status = result['status'] reason_code = result['reason'] msg_id = result['msg_id'] return DlrQueryResponse.new(status, reason_code, push_id) end |