login_radius 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,347 @@
1
+ module LoginRadiusRaas
2
+ module CustomObjectApi
3
+ #
4
+ # This API is used to retrieve all of the custom objects by account ID (UID).
5
+ #
6
+ # objectId = 'xxxxxxxxxxxx',
7
+ # accountId = 'xxxxxxxxxxxx'
8
+ #
9
+ # return all custom field
10
+ # {
11
+ # "Id": "53e31d61164ff214a0814327",
12
+ # "IsActive": true,
13
+ # "DateCreated": "2014-08-07T06:32:01.016Z",
14
+ # "DateModified": "2014-08-07T09:09:21.08Z",
15
+ # "IsDeleted": true,
16
+ # "Uid": "676d5049aba24314b8a5c5af1b80c0cb",
17
+ # "CustomObject": {
18
+ # "Id": "53e30b2c164ff114a044f3f4",
19
+ # "IsActive": true,
20
+ # "DateCreated": "2014-08-07T05: 14: 20.573Z",
21
+ # "DateModified": "2014-08-07T05: 14: 20.573Z",
22
+ # "IsDeleted": false,
23
+ # "Uid": "81ef41c461aa4a5eacba0a06f10c1481",
24
+ # "CustomObject": {
25
+ # "Industry": "chemical",
26
+ # "website": "http: //localhost23423423",
27
+ # "lastname": "",
28
+ # "RelationshipStatus": "married",
29
+ # "customfield1": {
30
+ # "field1": "1",
31
+ # "field2": "2",
32
+ # "field5": "5",
33
+ # "field6": "6"
34
+ # }
35
+ # }
36
+ # }
37
+ # }
38
+ #
39
+ #
40
+ def custom_object_get_by_accountid!(objectId, accountId)
41
+ api_client("raas/v1/user/customObject", {:objectid => objectId,:accountid => accountId});
42
+ end
43
+
44
+ def custom_object_get_by_accountid(objectId, accountId)
45
+ custom_object_get_by_accountid!(objectId, accountId)
46
+ rescue LoginRadiusRaas::Exception => e
47
+ false
48
+ end
49
+
50
+ #
51
+ # This API is used to retrieve all of the custom objects by an object’s unique ID.
52
+ #
53
+ # objectId = 'xxxxxxxxxxxx';
54
+ # recordId = 'xxxxxxxxxxxx';
55
+ #
56
+ # return all custom field
57
+ # {
58
+ # "Id": "53e31d61164ff214a0814327",
59
+ # "IsActive": true,
60
+ # "DateCreated": "2014-08-07T06:32:01.016Z",
61
+ # "DateModified": "2014-08-07T09:09:21.08Z",
62
+ # "IsDeleted": true,
63
+ # "Uid": "676d5049aba24314b8a5c5af1b80c0cb",
64
+ # "CustomObject": {
65
+ # "Id": "53e30b2c164ff114a044f3f4",
66
+ # "IsActive": true,
67
+ # "DateCreated": "2014-08-07T05: 14: 20.573Z",
68
+ # "DateModified": "2014-08-07T05: 14: 20.573Z",
69
+ # "IsDeleted": false,
70
+ # "Uid": "81ef41c461aa4a5eacba0a06f10c1481",
71
+ # "CustomObject": {
72
+ # "Industry": "chemical",
73
+ # "website": "http: //localhost23423423",
74
+ # "lastname": "",
75
+ # "RelationshipStatus": "married",
76
+ # "customfield1": {
77
+ # "field1": "1",
78
+ # "field2": "2",
79
+ # "field5": "5",
80
+ # "field6": "6"
81
+ # }
82
+ # }
83
+ # }
84
+ # }
85
+ #
86
+ #
87
+
88
+ def custom_object_get_by_recordid!(objectId, recordId)
89
+ api_client("raas/v1/user/customObject", {:objectid => objectId,:id => recordId});
90
+ end
91
+
92
+ def custom_object_get_by_recordid(objectId, recordId)
93
+ custom_object_get_by_recordid!(objectId, recordId)
94
+ rescue LoginRadiusRaas::Exception => e
95
+ false
96
+ end
97
+
98
+ #
99
+ # This API is used to retrieve all of the custom objects via a list of account IDs(UID) separated by , (Max 20).
100
+ #
101
+ # objectId = 'xxxxxxxxxxxx';
102
+ # accountIds = 'xxxxxxxxxxxx,xxxxxxxxxxxx,xxxxxxxxxxxx';
103
+ #
104
+ # return all custom field
105
+ # [{
106
+ # "Id": "53e31d61164ff214a0814327",
107
+ # "IsActive": true,
108
+ # "DateCreated": "2014-08-07T06:32:01.016Z",
109
+ # "DateModified": "2014-08-07T09:09:21.08Z",
110
+ # "IsDeleted": true,
111
+ # "Uid": "676d5049aba24314b8a5c5af1b80c0cb",
112
+ # "CustomObject": {
113
+ # "Id": "53e30b2c164ff114a044f3f4",
114
+ # "IsActive": true,
115
+ # "DateCreated": "2014-08-07T05: 14: 20.573Z",
116
+ # "DateModified": "2014-08-07T05: 14: 20.573Z",
117
+ # "IsDeleted": false, #
118
+ # "Uid": "81ef41c461aa4a5eacba0a06f10c1481",
119
+ # "CustomObject": {
120
+ # "Industry": "chemical",
121
+ # "website": "http: //localhost23423423",
122
+ # "lastname": "", #
123
+ # "RelationshipStatus": "married",
124
+ # "customfield1": {
125
+ # "field1": "1",
126
+ # "field2": "2",
127
+ # "field5": "5",
128
+ # "field6": "6"
129
+ # }
130
+ # }
131
+ # }
132
+ # },
133
+ # {
134
+ # "Id": "53e31d61164ff214a0814327",
135
+ # "IsActive": true,
136
+ # "DateCreated": "2014-08-07T06:32:01.016Z",
137
+ # "DateModified": "2014-08-07T09:09:21.08Z",
138
+ # "IsDeleted": true,
139
+ # "Uid": "676d5049aba24314b8a5c5af1b80c0cb",
140
+ # "CustomObject": {
141
+ # "Id": "53e30b2c164ff114a044f3f4",
142
+ # "IsActive": true,
143
+ # "DateCreated": "2014-08-07T05: 14: 20.573Z",
144
+ # "DateModified": "2014-08-07T05: 14: 20.573Z",
145
+ # "IsDeleted": false,
146
+ # "Uid": "81ef41c461aa4a5eacba0a06f10c1481",
147
+ # "CustomObject": {
148
+ # "Industry": "chemical",
149
+ # "website": "http: //localhost23423423",
150
+ # "lastname": "",
151
+ # "RelationshipStatus": "married",
152
+ # "customfield1": {
153
+ # "field1": "1",
154
+ # "field2": "2",
155
+ # "field5": "5",
156
+ # "field6": "6"
157
+ # }
158
+ # }
159
+ # }
160
+ # }]
161
+ #
162
+ #
163
+ def custom_object_get_by_accountids!(objectId, accountIds)
164
+ api_client("raas/v1/user/customObject", {:objectid => objectId,:accountids => accountIds});
165
+ end
166
+
167
+ def custom_object_get_by_accountids(objectId, accountIds)
168
+ custom_object_get_by_accountids!(objectId, accountIds)
169
+ rescue LoginRadiusRaas::Exception => e
170
+ false
171
+ end
172
+
173
+ #
174
+ # This API is used to retrieve all of the custom objects by an object’s unique ID and filtered by a query
175
+ #
176
+ # objectId = 'xxxxxxxxxx';
177
+ # query = "<Expression LogicalOperation='AND'>
178
+ # <Field Name='Provider' ComparisonOperator='Equal'>facebook</Field>
179
+ # <Expression LogicalOperation='OR'>
180
+ # <Field Name='Gender' ComparisonOperator='Equal'>M</Field>
181
+ # <Field Name='Gender' ComparisonOperator='Equal'>U</Field>
182
+ # </Expression>
183
+ # </Expression>";
184
+ # ------------------ OR ------------------
185
+ # query = "<Field Name='Gender' ComparisonOperator='Equal'>F</Field>";
186
+ #
187
+ # nextCursor=>[1]; (optional)
188
+ # );
189
+ #
190
+ # return all custom field
191
+ # {
192
+ # "Id": "53e31d61164ff214a0814327",
193
+ # "IsActive": true,
194
+ # "DateCreated": "2014-08-07T06:32:01.016Z",
195
+ # "DateModified": "2014-08-07T09:09:21.08Z",
196
+ # "IsDeleted": true,
197
+ # "Uid": "676d5049aba24314b8a5c5af1b80c0cb",
198
+ # "CustomObject": {
199
+ # "Id": "53e30b2c164ff114a044f3f4",
200
+ # "IsActive": true,
201
+ # "DateCreated": "2014-08-07T05: 14: 20.573Z",
202
+ # "DateModified": "2014-08-07T05: 14: 20.573Z",
203
+ # "IsDeleted": false,
204
+ # "Uid": "81ef41c461aa4a5eacba0a06f10c1481",
205
+ # "CustomObject": {
206
+ # "Industry": "chemical",
207
+ # "website": "http: //localhost23423423",
208
+ # "lastname": "",
209
+ # "RelationshipStatus": "married",
210
+ # "customfield1": {
211
+ # "field1": "1",
212
+ # "field2": "2",
213
+ # "field5": "5",
214
+ # "field6": "6"
215
+ # }
216
+ # }
217
+ # }
218
+ # }
219
+ #
220
+
221
+ def custom_object_get_by_query!(objectId, query, nextCursor)
222
+ api_client("raas/v1/user/customObject", {:objectid => objectId,:q => query,:cursor => nextCursor});
223
+ end
224
+
225
+ def custom_object_get_by_query(objectId, query, nextCursor=1)
226
+ custom_object_get_by_query!(objectId, query, nextCursor)
227
+ rescue LoginRadiusRaas::Exception => e
228
+ false
229
+ end
230
+
231
+ #
232
+ # This API is used to retrieve all records from a custom object.
233
+ #
234
+ # obejctId = 'xxxxxxxxxx';
235
+ # nextCursor = [1]; (optional)
236
+ #
237
+ # return
238
+ # {
239
+ # "Id": "53e31d61164ff214a0814327",
240
+ # "IsActive": true,
241
+ # "DateCreated": "2014-08-07T06:32:01.016Z",
242
+ # "DateModified": "2014-08-07T09:09:21.08Z",
243
+ # "IsDeleted": true,
244
+ # "Uid": "676d5049aba24314b8a5c5af1b80c0cb",
245
+ # "CustomObject": {
246
+ # "Id": "53e30b2c164ff114a044f3f4",
247
+ # "IsActive": true,
248
+ # "DateCreated": "2014-08-07T05: 14: 20.573Z",
249
+ # "DateModified": "2014-08-07T05: 14: 20.573Z",
250
+ # "IsDeleted": false,
251
+ # "Uid": "81ef41c461aa4a5eacba0a06f10c1481",
252
+ # "CustomObject": {
253
+ # "Industry": "chemical",
254
+ # "website": "http: //localhost23423423",
255
+ # "lastname": "",
256
+ # "RelationshipStatus": "married",
257
+ # "customfield1": {
258
+ # "field1": "1",
259
+ # "field2": "2",
260
+ # "field5": "5",
261
+ # "field6": "6"
262
+ # }
263
+ # }
264
+ # }
265
+ # }
266
+ #
267
+
268
+ def custom_object_get_all_objects!(objectId, nextCursor)
269
+ api_client("raas/v1/user/customObject", {:objectid => objectId,:cursor => nextCursor});
270
+ end
271
+
272
+ def custom_object_get_all_objects(objectId, nextCursor=1)
273
+ custom_object_get_all_objects!(objectId, nextCursor)
274
+ rescue LoginRadiusRaas::Exception => e
275
+ false
276
+ end
277
+
278
+ #
279
+ # This API is used to retrieve stats associated with a custom object
280
+ #
281
+ # objectId = 'xxxxxxxxxx';
282
+ #
283
+ # return
284
+ # {
285
+ # "TotalUsedMemory": 0.01,
286
+ # "RemainingMemory": 9.99,
287
+ # "TotalRecords": 7
288
+ # }
289
+ #
290
+ #
291
+
292
+ def custom_object_get_stats!(objectId)
293
+ api_client("raas/v1/user/customObject/stats", {:objectid => objectId});
294
+ end
295
+
296
+ def custom_object_get_all_objects(objectId)
297
+ custom_object_get_all_objects!(objectId)
298
+ rescue LoginRadiusRaas::Exception => e
299
+ false
300
+ end
301
+
302
+ #
303
+ # This API is used to save custom objects, by providing ID of object, to a specified account if the object is not exist it will create a new object.
304
+ #
305
+ # objectId = 'xxxxxxxxxx';
306
+ # accountId = 'xxxxxxxxxx';
307
+ # param = {
308
+ # firstname => 'first name',
309
+ # lastname => 'last name',
310
+ # gender => 'm',
311
+ # birthdate => 'MM-DD-YYYY',
312
+ # ....................
313
+ # ....................
314
+ # }
315
+ #
316
+ # return { “isPosted” : true }
317
+ #
318
+ def custom_object_upsert!(objectId, accountId, param)
319
+ api_client("raas/v1/user/customObject/upsert", {:objectid => objectId, :accountid => accountId}, param, 'json');
320
+ end
321
+
322
+ def custom_object_upsert(objectId, accountId, param = {})
323
+ custom_object_upsert!(objectId, accountId, param)
324
+ rescue LoginRadiusRaas::Exception => e
325
+ false
326
+ end
327
+
328
+ #
329
+ # This API is used to block Custom Object.
330
+ #
331
+ # objectId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
332
+ # accountId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
333
+ # action = true/false(boolean)
334
+ #
335
+ # return { “isPosted” : true }
336
+ #
337
+ def custom_object_set_status!(objectId, accountId, action)
338
+ api_client("raas/v1/user/customObject/status", {:objectid => objectId, :accountid => accountId}, {:isblock => action});
339
+ end
340
+
341
+ def custom_object_set_status(objectId, accountId, action=true)
342
+ custom_object_set_status!(objectId, accountId, action)
343
+ rescue LoginRadiusRaas::Exception => e
344
+ false
345
+ end
346
+ end
347
+ end
@@ -1,4 +1,4 @@
1
- module LoginRadius
2
- class Exception < Exception
3
- end
4
- end
1
+ module LoginRadiusRaas
2
+ class Exception < Exception
3
+ end
4
+ end
@@ -0,0 +1,102 @@
1
+ require 'net/http'
2
+
3
+ module LoginRadiusRaas
4
+ class RaasApi
5
+ include BasicApi
6
+ include AccountApi
7
+ include UserApi
8
+ include CustomObjectApi
9
+
10
+ attr_accessor :appkey, :appsecret, :ssl_verify_peer
11
+
12
+ RAAS_API_DOMAIN = "https://api.loginradius.com/"
13
+ RAAS_CDN_ENDPOINT = "https://cdn.loginradius.com/"
14
+ # Takes a hash of account secret, token, and connection type(net_http or em_http)
15
+ # and uses it to auth against the LoginRadius API. Then it returns the Account object. The
16
+ # async key is optional, if set to true, will use Em::HTTP instead of Net::HTTP.
17
+ #
18
+ # @param opts [Hash] Must have keys :token, :secret, :async(optional) and :ssl_verify_peer(optional)
19
+ # @return [LoginRadius::Account]
20
+ def initialize(opts = {})
21
+ self.appsecret = opts[:appsecret]
22
+ self.appkey = opts[:appkey]
23
+ self.ssl_verify_peer = opts[:ssl_verify_peer]
24
+ raise LoginRadiusRaas::Exception.new("Invalid appkey") unless guid_valid?(appkey)
25
+ raise LoginRadiusRaas::Exception.new("Invalid appsecret") unless guid_valid?(appsecret)
26
+ end
27
+
28
+ # Takes a guid and returns whether or not it is valid.
29
+ #
30
+ # @param guid [String]
31
+ # @return [Boolean]
32
+ def guid_valid?(guid)
33
+ guid.match(/^\{?[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}\}?$/i)
34
+ end
35
+
36
+ # Generic GET call function that other submodules can use to hit the API.
37
+ #
38
+ # @param url [String] Target URL to fetch data from.
39
+ # @param params [Hash] Parameters to send
40
+ # @return data [Hash] Parsed JSON data from the call
41
+ def api_client(url, getparams = {}, postparams='get', header='x-www-form-urlencoded')
42
+ url = RAAS_API_DOMAIN + url unless url.match(/^#{RAAS_API_DOMAIN}/) #in case api root is included,
43
+ url_obj = URI.parse(url)
44
+ authentication = {
45
+ :appkey => appkey,
46
+ :appsecret => appsecret
47
+ }
48
+ params = authentication.merge(getparams)
49
+ url_obj.query = URI.encode_www_form(params)
50
+
51
+ http = Net::HTTP.new(url_obj.host, url_obj.port)
52
+ http.use_ssl = true
53
+ if !ssl_verify_peer
54
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
55
+ end
56
+
57
+ if postparams == 'get'
58
+ response = http.get(url_obj.request_uri)
59
+ else
60
+ if header == "json"
61
+ postparamdata = postparams.to_json;
62
+ else
63
+ postparamdata = postparams.to_query;
64
+ end
65
+ response = http.post(url_obj.request_uri, postparamdata, {'Content-Type' => 'application/'+header })
66
+ end
67
+
68
+ if response.is_a?(Net::HTTPTemporaryRedirect)
69
+ #for some reason, we always get redirected when calling server first time.
70
+ #so if we do, we scan body for the redirect url, and the scan returns
71
+ #an array of arrays. So we grab the array we know has what we need,
72
+ #and grab the first element.
73
+ redirect_url_array = response.body.scan(/<a href=\"([^>]+)\">/i)[1]
74
+ redirect_url = redirect_url_array.first
75
+ return api_client(redirect_url, getparams, postparams, header)
76
+ end
77
+
78
+ response = response.body
79
+
80
+ # For some reason, this API returns true/false instead of JSON responses for certain calls.
81
+ # We catch this here.
82
+ return true if response.match(/^true/i)
83
+ return false if response.match(/^false/i)
84
+
85
+ #We rescue this because sometimes the API returns HTML pages(which can't be JSON parsed)
86
+ #This mostly happens when people use expired tokens. So we go ahead and raise an exception
87
+ #About it if it gets caught.
88
+ begin
89
+ converted_response = JSON.parse(response, :symbolize_names => true)
90
+ #it's all String keys in CamelCase above, so...
91
+ # IF we got a hash back, convert it directly, if its an array, convert each item which is a hash
92
+ # into snake case
93
+ # converted_response = unconverted_response.is_a?(Hash) ?
94
+ # Hash.lr_convert_hash_keys(unconverted_response).symbolize_keys! :
95
+ # unconverted_response.map { |item| Hash.lr_convert_hash_keys(item).symbolize_keys!
96
+ return converted_response
97
+ rescue JSON::ParserError => e
98
+ raise LoginRadiusRaas::Exception.new("A JSON parsing error occurred because the API returned an HTML page instead of JSON. This happens mostly when you're using an expired Token. Specifics: #{e.message}")
99
+ end
100
+ end
101
+ end
102
+ end