mxit-rails 0.3.4 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mxit-api.rb +7 -0
- data/lib/mxit-rails.rb +1 -4
- data/lib/{mxit_rails/mxit_api → mxit_api}/auth_token.rb +1 -1
- data/lib/{mxit_rails/mxit_api/api_client.rb → mxit_api/client.rb} +72 -50
- data/lib/{mxit_rails/mxit_api/mxit_api_exception.rb → mxit_api/exception.rb} +2 -1
- data/lib/{mxit_rails/mxit_api → mxit_api}/request_exception.rb +2 -2
- data/lib/mxit_rails/version.rb +1 -1
- metadata +7 -6
data/lib/mxit-api.rb
ADDED
data/lib/mxit-rails.rb
CHANGED
@@ -10,7 +10,4 @@ require "mxit_rails/page"
|
|
10
10
|
module MxitRails
|
11
11
|
end
|
12
12
|
|
13
|
-
require
|
14
|
-
require "mxit_rails/mxit_api/auth_token"
|
15
|
-
require "mxit_rails/mxit_api/mxit_api_exception"
|
16
|
-
require "mxit_rails/mxit_api/request_exception"
|
13
|
+
require 'mxit-api'
|
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'net/http'
|
2
2
|
require 'uri'
|
3
|
+
require 'json'
|
3
4
|
|
4
|
-
module
|
5
|
+
module MxitApi
|
5
6
|
class Client
|
6
7
|
MXIT_AUTH_BASE_URI = 'https://auth.mxit.com'
|
7
8
|
MXIT_AUTH_TOKEN_URI = MXIT_AUTH_BASE_URI + '/token'
|
@@ -20,7 +21,7 @@ module MxitRails::MxitApi
|
|
20
21
|
|
21
22
|
def request_app_auth(scopes)
|
22
23
|
if scopes.empty?
|
23
|
-
raise
|
24
|
+
raise MxitApi::Exception.new("No scopes were provided.")
|
24
25
|
end
|
25
26
|
|
26
27
|
response = http_client(MXIT_AUTH_TOKEN_URI) do |http, path|
|
@@ -33,13 +34,7 @@ module MxitRails::MxitApi
|
|
33
34
|
http.request(request)
|
34
35
|
end
|
35
36
|
|
36
|
-
|
37
|
-
when Net::HTTPSuccess then
|
38
|
-
@auth_token = AuthToken.new(JSON.parse(response.body))
|
39
|
-
|
40
|
-
else
|
41
|
-
raise MxitRails::MxitApi::RequestException.new(response.message, response.code)
|
42
|
-
end
|
37
|
+
@auth_token = AuthToken.new(handle_response(response))
|
43
38
|
end
|
44
39
|
|
45
40
|
# The user's response to the authorisation code request will be redirected to `redirect_uri`. If
|
@@ -50,7 +45,7 @@ module MxitRails::MxitApi
|
|
50
45
|
# scopes - list of scopes to which access is required
|
51
46
|
def user_code_request_uri(redirect_uri, state, scopes)
|
52
47
|
if scopes.empty?
|
53
|
-
raise
|
48
|
+
raise MxitApi::Exception.new("No scopes were provided.")
|
54
49
|
end
|
55
50
|
|
56
51
|
# build parameters
|
@@ -79,13 +74,7 @@ module MxitRails::MxitApi
|
|
79
74
|
http.request(request)
|
80
75
|
end
|
81
76
|
|
82
|
-
|
83
|
-
when Net::HTTPSuccess then
|
84
|
-
@auth_token = AuthToken.new(JSON.parse(response.body))
|
85
|
-
|
86
|
-
else
|
87
|
-
raise MxitRails::MxitApi::RequestException.new(response.message, response.code)
|
88
|
-
end
|
77
|
+
@auth_token = AuthToken.new(handle_response(response))
|
89
78
|
end
|
90
79
|
|
91
80
|
def revoke_token(auth_token)
|
@@ -98,15 +87,12 @@ module MxitRails::MxitApi
|
|
98
87
|
http.request(request)
|
99
88
|
end
|
100
89
|
|
101
|
-
|
102
|
-
raise MxitRails::MxitApi::RequestException.new(response.message, response.code)
|
103
|
-
end
|
90
|
+
handle_response(response)
|
104
91
|
end
|
105
92
|
|
106
93
|
def refresh_token(auth_token)
|
107
94
|
if auth_token.refresh_token.nil?
|
108
|
-
raise
|
109
|
-
"token.")
|
95
|
+
raise MxitApi::Exception.new("The provided auth token doesn't have a refresh token.")
|
110
96
|
end
|
111
97
|
|
112
98
|
response = http_client(MXIT_AUTH_TOKEN_URI) do |http, path|
|
@@ -119,20 +105,16 @@ module MxitRails::MxitApi
|
|
119
105
|
http.request(request)
|
120
106
|
end
|
121
107
|
|
122
|
-
|
123
|
-
when Net::HTTPSuccess then
|
124
|
-
auth_token = AuthToken.new(JSON.parse(response.body))
|
125
|
-
|
126
|
-
else
|
127
|
-
raise MxitRails::MxitApi::RequestException.new(response.message, response.code)
|
128
|
-
end
|
108
|
+
handle_response(response)
|
129
109
|
end
|
130
110
|
|
131
111
|
### API methods requiring authorisation.
|
132
112
|
|
133
113
|
# When sending as the app the `message/send` scope is required otherwise `message/user`
|
134
|
-
def send_message(from, to, body, contains_markup,
|
135
|
-
|
114
|
+
def send_message(from, to, body, contains_markup, options={ spool: true,
|
115
|
+
spool_timeout: 60*60*24*7, auth_token: nil })
|
116
|
+
|
117
|
+
auth_token = options[:auth_token] || @auth_token
|
136
118
|
|
137
119
|
if from == @app_name
|
138
120
|
check_auth_token(auth_token, ["message/send"])
|
@@ -145,21 +127,22 @@ module MxitRails::MxitApi
|
|
145
127
|
request = Net::HTTP::Post.new(path)
|
146
128
|
set_api_headers(request, auth_token.access_token)
|
147
129
|
|
130
|
+
spool = options[:spool].nil? ? true : options[:spool]
|
131
|
+
spool_timeout = options[:spool_timeout] || 60*60*24*7
|
132
|
+
|
148
133
|
request.body = {
|
149
134
|
"Body" => body,
|
150
135
|
"ContainsMarkup" => contains_markup,
|
151
136
|
"From" => from,
|
152
|
-
"To" => to
|
153
|
-
|
154
|
-
|
137
|
+
"To" => to,
|
138
|
+
"Spool" => spool,
|
139
|
+
"SpoolTimeOut" => spool_timeout
|
155
140
|
}.to_json
|
156
141
|
|
157
142
|
http.request(request)
|
158
143
|
end
|
159
144
|
|
160
|
-
|
161
|
-
raise MxitRails::MxitApi::RequestException.new(response.message, response.code)
|
162
|
-
end
|
145
|
+
handle_response(response)
|
163
146
|
end
|
164
147
|
|
165
148
|
# The following filter parameters are available (only one can be specified at a time):
|
@@ -172,7 +155,7 @@ module MxitRails::MxitApi
|
|
172
155
|
# @Pending - Return all entries that is waiting to be accepted by the other party
|
173
156
|
# @Deleted - Return all entries that was deleted
|
174
157
|
# @Blocked - Return all entries that was blocked
|
175
|
-
def get_contact_list(filter, options={ :
|
158
|
+
def get_contact_list(filter, options={ skip: nil, count: nil, auth_token: nil })
|
176
159
|
auth_token = options[:auth_token] || @auth_token
|
177
160
|
check_auth_token(auth_token, ["graph/read"])
|
178
161
|
|
@@ -180,8 +163,8 @@ module MxitRails::MxitApi
|
|
180
163
|
|
181
164
|
parameters = { :filter => filter }
|
182
165
|
# skip and count are optional
|
183
|
-
parameters[:skip] = skip if options[:skip]
|
184
|
-
parameters[:count] = count if options[:count]
|
166
|
+
parameters[:skip] = options[:skip] if options[:skip]
|
167
|
+
parameters[:count] = options[:count] if options[:count]
|
185
168
|
|
186
169
|
request = Net::HTTP::Get.new(path + "?#{URI.encode_www_form(parameters)}")
|
187
170
|
set_api_headers(request, auth_token.access_token)
|
@@ -189,13 +172,30 @@ module MxitRails::MxitApi
|
|
189
172
|
http.request(request)
|
190
173
|
end
|
191
174
|
|
192
|
-
|
193
|
-
|
194
|
-
data = JSON.parse(response.body)
|
175
|
+
handle_response(response)
|
176
|
+
end
|
195
177
|
|
196
|
-
|
197
|
-
|
178
|
+
def recommend_app(callback_url, from_user_id, to_user_id, message, options={ auth_token: nil })
|
179
|
+
auth_token = options[:auth_token] || @auth_token
|
180
|
+
check_auth_token(auth_token, ["contact/recommend"])
|
181
|
+
|
182
|
+
response = http_client(MXIT_API_URI + "/user/recommend") do |http, path|
|
183
|
+
|
184
|
+
request = Net::HTTP::Post.new(path)
|
185
|
+
set_api_headers(request, auth_token.access_token)
|
186
|
+
|
187
|
+
request.body = {
|
188
|
+
"Application" => @app_name,
|
189
|
+
"CallbackUrl" => callback_url,
|
190
|
+
"FromUserId" => from_user_id,
|
191
|
+
"ToUserId" => to_user_id,
|
192
|
+
"Message" => message
|
193
|
+
}.to_json
|
194
|
+
|
195
|
+
http.request(request)
|
198
196
|
end
|
197
|
+
|
198
|
+
handle_response(response)
|
199
199
|
end
|
200
200
|
|
201
201
|
def batch_notify_users(mxit_ids, message, contains_markup)
|
@@ -208,12 +208,12 @@ module MxitRails::MxitApi
|
|
208
208
|
i = 0
|
209
209
|
while i < mxit_ids.count
|
210
210
|
current_batch = mxit_ids[i, batch_size]
|
211
|
-
i +=
|
211
|
+
i += current_batch.count
|
212
212
|
|
213
213
|
to = current_batch.join(',')
|
214
214
|
send_message(@app_name, to, message, contains_markup)
|
215
215
|
|
216
|
-
Rails.logger.info("Total users notified: " +
|
216
|
+
Rails.logger.info("Total users notified: " + i.to_s)
|
217
217
|
end
|
218
218
|
Rails.logger.info('Finished notifying!')
|
219
219
|
end
|
@@ -248,10 +248,32 @@ module MxitRails::MxitApi
|
|
248
248
|
|
249
249
|
def check_auth_token(auth_token, scopes)
|
250
250
|
if auth_token.nil?
|
251
|
-
raise
|
251
|
+
raise MxitApi::Exception.new("No auth token has been set/provided.")
|
252
252
|
elsif not auth_token.has_scopes? scopes
|
253
|
-
raise
|
254
|
-
|
253
|
+
raise MxitApi::Exception.new("The auth token doesn't have the required scope(s): " +
|
254
|
+
scopes.join(","))
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
def handle_response(response)
|
259
|
+
begin
|
260
|
+
data = JSON.parse(response.body)
|
261
|
+
rescue JSON::ParserError
|
262
|
+
data = {}
|
263
|
+
end
|
264
|
+
|
265
|
+
case response
|
266
|
+
when Net::HTTPSuccess then
|
267
|
+
data
|
268
|
+
|
269
|
+
when Net::HTTPBadRequest, Net::HTTPUnauthorized, Net::HTTPForbidden then
|
270
|
+
error_message = "#{response.code}::#{response.message}"
|
271
|
+
error_message += " - #{data["error"]}: #{data["error_description"]}" if not data.empty?
|
272
|
+
raise MxitApi::RequestException.new(error_message, response.code), error_message
|
273
|
+
|
274
|
+
else
|
275
|
+
raise MxitApi::RequestException.new(response.message, response.code),
|
276
|
+
"#{response.code}::#{response.message}"
|
255
277
|
end
|
256
278
|
end
|
257
279
|
|
data/lib/mxit_rails/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mxit-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -90,15 +90,16 @@ files:
|
|
90
90
|
- app/views/layouts/mxit.html.erb
|
91
91
|
- app/views/mxit_rails/error.html.erb
|
92
92
|
- config/routes.rb
|
93
|
+
- lib/mxit-api.rb
|
93
94
|
- lib/mxit-rails.rb
|
95
|
+
- lib/mxit_api/auth_token.rb
|
96
|
+
- lib/mxit_api/client.rb
|
97
|
+
- lib/mxit_api/exception.rb
|
98
|
+
- lib/mxit_api/request_exception.rb
|
94
99
|
- lib/mxit_rails/controller_extensions.rb
|
95
100
|
- lib/mxit_rails/descriptor.rb
|
96
101
|
- lib/mxit_rails/engine.rb
|
97
102
|
- lib/mxit_rails/exception.rb
|
98
|
-
- lib/mxit_rails/mxit_api/api_client.rb
|
99
|
-
- lib/mxit_rails/mxit_api/auth_token.rb
|
100
|
-
- lib/mxit_rails/mxit_api/mxit_api_exception.rb
|
101
|
-
- lib/mxit_rails/mxit_api/request_exception.rb
|
102
103
|
- lib/mxit_rails/page.rb
|
103
104
|
- lib/mxit_rails/railtie.rb
|
104
105
|
- lib/mxit_rails/styles.rb
|