Class: RtrHttpGateway::HttpGatewayApi

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

Instance Method Summary collapse

Constructor Details

#initialize(username, password, service_key, url = 'https://connect.runthered.com:14004/public_api/sms/gateway/', dlr_url = 'https://connect.runthered.com:14004/public_api/sms/dlr/') ⇒ HttpGatewayApi

Returns a new instance of HttpGatewayApi.



20
21
22
23
24
25
26
# File 'lib/rtr_http_gateway.rb', line 20

def initialize(username, password, service_key, url='https://connect.runthered.com:14004/public_api/sms/gateway/', dlr_url='https://connect.runthered.com:14004/public_api/sms/dlr/')
  @url = url
  @dlr_url = dlr_url
  @username = username
  @password= password
  @service_key = service_key
end

Instance Method Details

#do_get_request(url_string) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/rtr_http_gateway.rb', line 39

def do_get_request(url_string)
  uri = URI.parse(url_string)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == 'https'
  request = Net::HTTP::Get.new(uri.request_uri)
  request.basic_auth @username, @password
  response = http.request(request)
  return response
end

#do_post_request(url_string, values) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/rtr_http_gateway.rb', line 28

def do_post_request(url_string, values)
  uri = URI.parse(url_string)
  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.set_form_data(values)
  request.basic_auth @username, @password
  response = http.request(request)
  return response	
end

#push_message(message, to, from_number = nil, billing_code = nil, partner_reference = nil) ⇒ String

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

  • billing_code (String) (defaults to: nil)

    the billing code for the message, if required

  • partner_reference (String) (defaults to: nil)

    a client supplied reference string, if required

Returns:

  • (String)

    the message id of the message created in Run The Red’s system

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rtr_http_gateway.rb', line 57

def push_message(message, to, from_number=nil, billing_code=nil, partner_reference=nil)
  values = {'message'=>message, 'to'=>to}
  unless from_number.nil?
    values["from"] = from_number
  end
  unless billing_code.nil?
    values["billingCode"] = billing_code
  end
  unless partner_reference.nil?
    values["partnerReference"] = partner_reference
  end
  response = do_post_request(@url + @service_key, values)
  if response.code != '200'
    raise HttpGatewayException, response.code
  end
  return response.body
end

#query_dlr(msg_id) ⇒ 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

Returns:

  • (DlrQueryResponse)

    an object with the status, reason_code and msg_id as attributes

Raises:



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rtr_http_gateway.rb', line 79

def query_dlr(msg_id)
  values = {'id' => msg_id}
  params = URI.encode_www_form(values)
  response = do_get_request(@dlr_url + @service_key + '?' + params)
  if response.code != '200'
    raise HttpGatewayException, response.code
  end
  data = JSON.parse response.body
  msg_id = data['id']
  status = data['status']
  reason_code = data['reason']
  return DlrQueryResponse.new(status, reason_code, msg_id)
end