Class: RtrPushApi::PushApi

Inherits:
Object
  • Object
show all
Defined in:
lib/push_api/rtr_push_api.rb

Instance Method Summary collapse

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']
      message = error['message']
      code = error['code']
      raise PushApiException, message
    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

Parameters:

  • message (String)

    the message to send

  • to (String)

    the mobile number to send to

  • from_number (String) (defaults to: nil)

    the shortcode the message will come from

  • push_id (Integer) (defaults to: 1)

    the push_id sent by the client to comply with the JSON-RPC 2.0 spec

Returns:

  • (PushApiResponse)

    a response object with the status, msg_id and id as attributes



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/push_api/rtr_push_api.rb', line 68

def push_message(message, to, from_number=nil, push_id=1)
  json_data = {"jsonrpc" => "2.0", "method" => "sendsms", "params" => {"service_key" => @service_key, "to" => to, "body" => message}, "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

Parameters:

  • msg_id (String)

    the message id of the message to check the delivery status of

  • push_id (Integer) (defaults to: 1)

    the push_id sent by the client to comply with the JSON-RPC 2.0 spec

Returns:

  • (DlrQueryResponse)

    an object with the status, reason_code and id as attributes



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