runthered_ruby 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ac272f58d08fa4bad33809573f6da3745c3d7d6
4
- data.tar.gz: 69177fc41eb5c2d50faf1117f14865a876ba23de
3
+ metadata.gz: 1aae1d0961bc9734e056edd10441eb2b77b1e94f
4
+ data.tar.gz: 3643a3bf5c1710f5a499805d9aff760bc47d4a74
5
5
  SHA512:
6
- metadata.gz: 63d8394cdb585e77f8361c093ffb98cedc998626bacc96e7e9201d18cb886b59eb57150860ad90842dce619f50e48988efd2ec50f4a5af4a7e469d2575708492
7
- data.tar.gz: f673a734bd3af4afd174663764c6db2b0cfa5a1deac9bd67b8acf79ea3a54b506e98fac36534c0b1022907d7ef57849a29862c9b7a73c336d09507754115bb03
6
+ metadata.gz: b1eb73172503843a70e1b2f71f04a9442cef2f0cbef53b84634b4eab9e8f7afac8e6000a8ac248c408f5a4badec38191883549aee1e4b0685996bea897e7057c
7
+ data.tar.gz: 609b0d29198d53316653b1ea37f10d1a81e0155b46f3149ea83160191171e18fa3da1a3d1e1840bf7db8baf51709a5d1d0c38691f403b5d30808da689687a19f
@@ -0,0 +1,97 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'uri'
4
+
5
+ module RtrPushApi
6
+
7
+ # Represents The Push Api status and message id
8
+ class PushApiResponse
9
+ def initialize(status, msg_id, push_id)
10
+ @status = status
11
+ @msg_id = msg_id
12
+ @id = push_id
13
+ end
14
+ attr_reader :status, :msg_id, :id
15
+ end
16
+
17
+ # Represents The Delivery receipt status and reason code
18
+ class DlrQueryResponse
19
+ def initialize(status, reason_code, push_id)
20
+ @status = status
21
+ @reason_code = reason_code
22
+ @id = push_id
23
+ end
24
+ attr_reader :status, :reason_code, :id
25
+ end
26
+
27
+ class PushApiException < StandardError
28
+ end
29
+
30
+ class PushApi
31
+ def initialize(username, password, service_key, url='https://connect.runthered.com:10443/public_api/service')
32
+ @url = url
33
+ @username = username
34
+ @password = password
35
+ @service_key = service_key
36
+ end
37
+
38
+ def do_json_request(data)
39
+ uri = URI.parse(@url)
40
+ http = Net::HTTP.new(uri.host, uri.port)
41
+ http.use_ssl = true if uri.scheme == 'https'
42
+ request = Net::HTTP::Post.new(uri.request_uri)
43
+ request.body = data.to_json
44
+ request.basic_auth @username, @password
45
+ response = http.request(request)
46
+ if response.code != '200'
47
+ raise PushApiException, response.body
48
+ else
49
+ data = JSON.parse response.body
50
+ if !data.has_key?("result")
51
+ error = data['error']
52
+ message = error['message']
53
+ code = error['code']
54
+ raise PushApiException, message
55
+ end
56
+ return data
57
+ end
58
+
59
+ return response
60
+ end
61
+
62
+ # Send a message to Run The Red
63
+ # @param message [String] the message to send
64
+ # @param to [String] the mobile number to send to
65
+ # @param from_number [String] the shortcode the message will come from
66
+ # @param push_id [Integer] the push_id sent by the client to comply with the JSON-RPC 2.0 spec
67
+ # @return [PushApiResponse] a response object with the status, msg_id and id as attributes
68
+ def push_message(message, to, from_number=nil, push_id=1)
69
+ json_data = {"jsonrpc" => "2.0", "method" => "sendsms", "params" => {"service_key" => @service_key, "to" => to, "body" => message}, "id" => push_id}
70
+ unless from_number.nil?
71
+ json_data["params"]["frm"] = from_number
72
+ end
73
+ data = do_json_request(json_data)
74
+ push_id = data['id']
75
+ result = data['result']
76
+ status = result['status']
77
+ msg_id = result['msg_id']
78
+ return PushApiResponse.new(status, msg_id, push_id)
79
+ end
80
+
81
+ # Query a delivery receipt using the message id supplied by Run The Red
82
+ # @param msg_id [String] the message id of the message to check the delivery status of
83
+ # @param push_id [Integer] the push_id sent by the client to comply with the JSON-RPC 2.0 spec
84
+ # @return [DlrQueryResponse] an object with the status, reason_code and id as attributes
85
+ def query_dlr(msg_id, push_id=1)
86
+ json_data = {"jsonrpc" => "2.0", "method" => "querydlr", "params" => {"service_key" => @service_key, "msg_id" => msg_id}, "id" => push_id}
87
+ data = do_json_request(json_data)
88
+ push_id = data['id']
89
+ result = data['result']
90
+ status = result['status']
91
+ reason_code = result['reason']
92
+ msg_id = result['msg_id']
93
+ return DlrQueryResponse.new(status, reason_code, push_id)
94
+ end
95
+ end
96
+ end
97
+
@@ -1 +1,2 @@
1
1
  require 'http_gateway/rtr_http_gateway'
2
+ require 'push_api/rtr_push_api'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: runthered_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Finn Colman
@@ -59,6 +59,7 @@ extensions: []
59
59
  extra_rdoc_files: []
60
60
  files:
61
61
  - lib/http_gateway/rtr_http_gateway.rb
62
+ - lib/push_api/rtr_push_api.rb
62
63
  - lib/runthered_ruby.rb
63
64
  homepage: http://rubygems.org/gems/rtr_http_gateway
64
65
  licenses: