cloudrail_si 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +3 -0
  3. data/examples/README.md +3 -0
  4. data/examples/simple/index.rb +11 -0
  5. data/lib/cloudrail_si.rb +41 -0
  6. data/lib/cloudrail_si/RedirectReceivers.rb +40 -0
  7. data/lib/cloudrail_si/Settings.rb +12 -0
  8. data/lib/cloudrail_si/errors/DetailErrors.rb +13 -0
  9. data/lib/cloudrail_si/errors/InternalError.rb +10 -0
  10. data/lib/cloudrail_si/errors/UserError.rb +11 -0
  11. data/lib/cloudrail_si/helpers/Helper.rb +151 -0
  12. data/lib/cloudrail_si/helpers/LimitedReadableStream.rb +55 -0
  13. data/lib/cloudrail_si/helpers/SequenceReadableStream.rb +59 -0
  14. data/lib/cloudrail_si/servicecode/InitSelfTest.rb +92 -0
  15. data/lib/cloudrail_si/servicecode/Interpreter.rb +213 -0
  16. data/lib/cloudrail_si/servicecode/Sandbox.rb +361 -0
  17. data/lib/cloudrail_si/servicecode/VarAddress.rb +11 -0
  18. data/lib/cloudrail_si/servicecode/commands/AwaitCodeRedirect.rb +48 -0
  19. data/lib/cloudrail_si/servicecode/commands/Break.rb +16 -0
  20. data/lib/cloudrail_si/servicecode/commands/CallFunc.rb +26 -0
  21. data/lib/cloudrail_si/servicecode/commands/Clone.rb +18 -0
  22. data/lib/cloudrail_si/servicecode/commands/Concat.rb +22 -0
  23. data/lib/cloudrail_si/servicecode/commands/Conditional.rb +27 -0
  24. data/lib/cloudrail_si/servicecode/commands/Create.rb +56 -0
  25. data/lib/cloudrail_si/servicecode/commands/Delete.rb +21 -0
  26. data/lib/cloudrail_si/servicecode/commands/Get.rb +25 -0
  27. data/lib/cloudrail_si/servicecode/commands/GetMimeType.rb +1005 -0
  28. data/lib/cloudrail_si/servicecode/commands/JumpRel.rb +18 -0
  29. data/lib/cloudrail_si/servicecode/commands/Pull.rb +34 -0
  30. data/lib/cloudrail_si/servicecode/commands/Push.rb +32 -0
  31. data/lib/cloudrail_si/servicecode/commands/Return.rb +16 -0
  32. data/lib/cloudrail_si/servicecode/commands/Set.rb +23 -0
  33. data/lib/cloudrail_si/servicecode/commands/Size.rb +26 -0
  34. data/lib/cloudrail_si/servicecode/commands/ThrowError.rb +17 -0
  35. data/lib/cloudrail_si/servicecode/commands/array/Uint8ToBase64.rb +25 -0
  36. data/lib/cloudrail_si/servicecode/commands/debug/Out.rb +24 -0
  37. data/lib/cloudrail_si/servicecode/commands/hash/md5.rb +29 -0
  38. data/lib/cloudrail_si/servicecode/commands/hash/sha1.rb +30 -0
  39. data/lib/cloudrail_si/servicecode/commands/http/RequestCall.rb +51 -0
  40. data/lib/cloudrail_si/servicecode/commands/json/Parse.rb +40 -0
  41. data/lib/cloudrail_si/servicecode/commands/json/Stringify.rb +21 -0
  42. data/lib/cloudrail_si/servicecode/commands/math/Floor.rb +27 -0
  43. data/lib/cloudrail_si/servicecode/commands/math/MathCombine.rb +35 -0
  44. data/lib/cloudrail_si/servicecode/commands/object/GetKeyArray.rb +21 -0
  45. data/lib/cloudrail_si/servicecode/commands/object/GetKeyValueArrays.rb +29 -0
  46. data/lib/cloudrail_si/servicecode/commands/stream/MakeJoinedStream.rb +38 -0
  47. data/lib/cloudrail_si/servicecode/commands/stream/MakeLimitedStream.rb +23 -0
  48. data/lib/cloudrail_si/servicecode/commands/stream/StreamToString.rb +25 -0
  49. data/lib/cloudrail_si/servicecode/commands/stream/StringToStream.rb +20 -0
  50. data/lib/cloudrail_si/servicecode/commands/string/Base64Encode.rb +42 -0
  51. data/lib/cloudrail_si/servicecode/commands/string/Format.rb +43 -0
  52. data/lib/cloudrail_si/servicecode/commands/string/IndexOf.rb +29 -0
  53. data/lib/cloudrail_si/servicecode/commands/string/LastIndexOf.rb +29 -0
  54. data/lib/cloudrail_si/servicecode/commands/string/Split.rb +29 -0
  55. data/lib/cloudrail_si/servicecode/commands/string/StringTransform.rb +29 -0
  56. data/lib/cloudrail_si/servicecode/commands/string/Substr.rb +30 -0
  57. data/lib/cloudrail_si/servicecode/commands/string/Substring.rb +30 -0
  58. data/lib/cloudrail_si/services/Facebook.rb +528 -0
  59. data/lib/cloudrail_si/services/FacebookPage.rb +310 -0
  60. data/lib/cloudrail_si/services/Foursquare.rb +314 -0
  61. data/lib/cloudrail_si/services/GitHub.rb +354 -0
  62. data/lib/cloudrail_si/services/GooglePlaces.rb +309 -0
  63. data/lib/cloudrail_si/services/GooglePlus.rb +367 -0
  64. data/lib/cloudrail_si/services/Instagram.rb +342 -0
  65. data/lib/cloudrail_si/services/LinkedIn.rb +363 -0
  66. data/lib/cloudrail_si/services/MicrosoftLive.rb +346 -0
  67. data/lib/cloudrail_si/services/Nexmo.rb +173 -0
  68. data/lib/cloudrail_si/services/Slack.rb +318 -0
  69. data/lib/cloudrail_si/services/Twilio.rb +173 -0
  70. data/lib/cloudrail_si/services/Twitter.rb +795 -0
  71. data/lib/cloudrail_si/services/Yahoo.rb +408 -0
  72. data/lib/cloudrail_si/services/Yelp.rb +389 -0
  73. data/lib/cloudrail_si/statistics/Statistics.rb +125 -0
  74. data/lib/cloudrail_si/types/Address.rb +9 -0
  75. data/lib/cloudrail_si/types/Charge.rb +33 -0
  76. data/lib/cloudrail_si/types/CloudMetaData.rb +19 -0
  77. data/lib/cloudrail_si/types/CreditCard.rb +76 -0
  78. data/lib/cloudrail_si/types/Date.rb +49 -0
  79. data/lib/cloudrail_si/types/DateOfBirth.rb +16 -0
  80. data/lib/cloudrail_si/types/Error.rb +21 -0
  81. data/lib/cloudrail_si/types/Location.rb +8 -0
  82. data/lib/cloudrail_si/types/POI.rb +17 -0
  83. data/lib/cloudrail_si/types/Refund.rb +25 -0
  84. data/lib/cloudrail_si/types/SandboxObject.rb +20 -0
  85. data/lib/cloudrail_si/types/SpaceAllocation.rb +13 -0
  86. data/lib/cloudrail_si/types/Subscription.rb +27 -0
  87. data/lib/cloudrail_si/types/SubscriptionPlan.rb +26 -0
  88. data/lib/cloudrail_si/types/Types.rb +43 -0
  89. data/lib/cloudrail_si/version.rb +3 -0
  90. metadata +205 -0
@@ -0,0 +1,363 @@
1
+ require_relative '../servicecode/Interpreter'
2
+ require_relative '../servicecode/Sandbox'
3
+ require_relative '../errors/DetailErrors'
4
+ require_relative '../servicecode/InitSelfTest'
5
+ require_relative '../statistics/Statistics'
6
+
7
+ module CloudRailSi
8
+ module Services
9
+ class LinkedIn
10
+
11
+ SERVICE_CODE = {
12
+
13
+ "AdvancedRequestSupporter:advancedRequest" => [
14
+ [ "create", "$L0", "Object"],
15
+ [ "create", "$L0.url", "String"],
16
+ [ "if!=than", "$P2.appendBaseUrl", 0, 1],
17
+ [ "set", "$L0.url", "https://api.linkedin.com/v1"],
18
+ [ "string.concat", "$L0.url", "$L0.url", "$P2.url"],
19
+ [ "set", "$L0.requestHeaders", "$P2.headers"],
20
+ [ "set", "$L0.method", "$P2.method"],
21
+ [ "set", "$L0.requestBody", "$P2.body"],
22
+ [ "if!=than", "$P2.appendAuthorization", 0, 2],
23
+ [ "callFunc", "checkAuthentication", "$P0"],
24
+ [ "string.concat", "$L0.requestHeaders.Authorization", "Bearer ", "$S0.accessToken"],
25
+ [ "http.requestCall", "$L1", "$L0"],
26
+ [ "if!=than", "$P2.checkErrors", 0, 1],
27
+ [ "callFunc", "validateResponse", "$P0", "$L1"],
28
+ [ "create", "$P1", "AdvancedRequestResponse"],
29
+ [ "set", "$P1.status", "$L1.code"],
30
+ [ "set", "$P1.headers", "$L1.responseHeaders"],
31
+ [ "set", "$P1.body", "$L1.responseBody"]
32
+
33
+ ],
34
+
35
+ "getLIIdentifier" => [
36
+ [ "callFunc", "checkUserData", "$P0"],
37
+ [ "string.concat", "$P1", "linkedin-", "$P0.userData.id"]
38
+
39
+ ],
40
+
41
+ "getLIFullName" => [
42
+ [ "callFunc", "checkUserData", "$P0"],
43
+ [ "string.concat", "$P1", "$P0.userData.firstName", " ", "$P0.userData.lastName"]
44
+
45
+ ],
46
+
47
+ "getLIEmail" => [
48
+ [ "callFunc", "checkUserData", "$P0"],
49
+ [ "set", "$P1", "$P0.userData.emailAddress"]
50
+
51
+ ],
52
+
53
+ "getLIGender" => [
54
+
55
+ ],
56
+
57
+ "getLIDescription" => [
58
+ [ "callFunc", "checkUserData", "$P0"],
59
+ [ "set", "$P1", "$P0.userData.summary"]
60
+
61
+ ],
62
+
63
+ "getLIDateOfBirth" => [
64
+ [ "callFunc", "checkUserData", "$P0"],
65
+ [ "create", "$L0", "DateOfBirth"],
66
+ [ "if!=than", "$P0.userData.dateOfBirth", nil, 6],
67
+ [ "if!=than", "$P0.userData.dateOfBirth.day", nil, 1],
68
+ [ "set", "$L0.day", "$P0.userData.dateOfBirth.day"],
69
+ [ "if!=than", "$P0.userData.dateOfBirth.month", nil, 1],
70
+ [ "set", "$L0.month", "$P0.userData.dateOfBirth.month"],
71
+ [ "if!=than", "$P0.userData.dateOfBirth.year", nil, 1],
72
+ [ "set", "$L0.year", "$P0.userData.dateOfBirth.year"],
73
+ [ "set", "$P1", "$L0"]
74
+
75
+ ],
76
+
77
+ "getLILocale" => [
78
+
79
+ ],
80
+
81
+ "getLIPictureURL" => [
82
+ [ "callFunc", "checkUserData", "$P0"],
83
+ [ "set", "$P1", "$P0.userData.pictureUrl"]
84
+
85
+ ],
86
+
87
+ "loginLI" => [
88
+ [ "callFunc", "checkAuthentication", "$P0"]
89
+
90
+ ],
91
+
92
+ "logoutLI" => [
93
+ [ "set", "$S0.accessToken", nil],
94
+ [ "set", "$P0.userData", nil]
95
+
96
+ ],
97
+
98
+ "checkAuthentication" => [
99
+ [ "create", "$L0", "Date"],
100
+ [ "if==than", "$S0.accessToken", nil, 2],
101
+ [ "callFunc", "authenticate", "$P0"],
102
+ [ "return"],
103
+ [ "create", "$L1", "Date"],
104
+ [ "set", "$L1.time", "$S0.expireIn"],
105
+ [ "if<than", "$L1", "$L0", 1],
106
+ [ "callFunc", "authenticate", "$P0"]
107
+
108
+ ],
109
+
110
+ "authenticate" => [
111
+ [ "create", "$L0", "String"],
112
+ [ "create", "$L1", "String"],
113
+ [ "string.concat", "$L0", "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=", "$P0.clientID", "&redirect_uri=", "$P0.redirectUri", "&state=", "$P0.state", "&scope=r_basicprofile%20r_emailaddress"],
114
+ [ "awaitCodeRedirect", "$L2", "$L0"],
115
+ [ "string.concat", "$L1", "grant_type=authorization_code&code=", "$L2", "&redirect_uri=", "$P0.redirectUri", "&client_id=", "$P0.clientID", "&client_secret=", "$P0.clientSecret"],
116
+ [ "size", "$L15", "$L1"],
117
+ [ "string.concat", "$L15", "$L15"],
118
+ [ "stream.stringToStream", "$L3", "$L1"],
119
+ [ "create", "$L4", "Object"],
120
+ [ "set", "$L4", "application/x-www-form-urlencoded", "Content-Type"],
121
+ [ "set", "$L4.Content-Length", "$L15"],
122
+ [ "create", "$L5", "Object"],
123
+ [ "set", "$L5.url", "https://www.linkedin.com/uas/oauth2/accessToken"],
124
+ [ "set", "$L5.method", "POST"],
125
+ [ "set", "$L5.requestBody", "$L3"],
126
+ [ "set", "$L5.requestHeaders", "$L4"],
127
+ [ "http.requestCall", "$L6", "$L5"],
128
+ [ "callFunc", "validateResponse", "$P0", "$L6"],
129
+ [ "stream.streamToString", "$L7", "$L6.responseBody"],
130
+ [ "json.parse", "$L8", "$L7"],
131
+ [ "set", "$S0.accessToken", "$L8.access_token"],
132
+ [ "create", "$L10", "Date"],
133
+ [ "math.multiply", "$L9", "$L8.expires_in", 1000],
134
+ [ "math.add", "$L9", "$L9", "$L10.time", -60000],
135
+ [ "set", "$S0.expireIn", "$L9"]
136
+
137
+ ],
138
+
139
+ "validateResponse" => [
140
+ [ "if>=than", "$P1.code", 400, 20],
141
+ [ "json.parse", "$L0", "$P1.responseBody"],
142
+ [ "set", "$L2", "$L0.message"],
143
+ [ "if==than", "$P1.code", 401, 2],
144
+ [ "create", "$L3", "Error", "$L2", "Authentication"],
145
+ [ "throwError", "$L3"],
146
+ [ "if==than", "$P1.code", 400, 2],
147
+ [ "create", "$L3", "Error", "$L2", "Http"],
148
+ [ "throwError", "$L3"],
149
+ [ "if>=than", "$P1.code", 402, 5],
150
+ [ "if<=than", "$P1.code", 509, 4],
151
+ [ "if!=than", "$P1.code", 503, 3],
152
+ [ "if!=than", "$P1.code", 404, 2],
153
+ [ "create", "$L3", "Error", "$L2", "Http"],
154
+ [ "throwError", "$L3"],
155
+ [ "if==than", "$P1.code", 503, 2],
156
+ [ "create", "$L3", "Error", "$L2", "ServiceUnavailable"],
157
+ [ "throwError", "$L3"],
158
+ [ "if==than", "$P1.code", 404, 2],
159
+ [ "create", "$L3", "Error", "$L2", "NotFound"],
160
+ [ "throwError", "$L3"]
161
+
162
+ ],
163
+
164
+ "getUserData" => [
165
+ [ "callFunc", "checkAuthentication", "$P0"],
166
+ [ "create", "$L0", "Object"],
167
+ [ "set", "$L0.method", "GET"],
168
+ [ "create", "$L1", "String"],
169
+ [ "string.concat", "$L1", "https://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address,summary,picture-url)?format=json"],
170
+ [ "set", "$L0.url", "$L1"],
171
+ [ "create", "$L0.requestHeaders", "Object"],
172
+ [ "string.concat", "$L0.requestHeaders.Authorization", "Bearer ", "$S0.accessToken"],
173
+ [ "create", "$L2", "Object"],
174
+ [ "http.requestCall", "$L2", "$L0"],
175
+ [ "callFunc", "validateResponse", "$P0", "$L2"],
176
+ [ "create", "$L3", "String"],
177
+ [ "stream.streamToString", "$L3", "$L2.responseBody"],
178
+ [ "create", "$L4", "Object"],
179
+ [ "json.parse", "$L4", "$L3"],
180
+ [ "create", "$L5", "Date"],
181
+ [ "math.add", "$L6", "$L5.time", 60000],
182
+ [ "set", "$L4.expirationTime", "$L6"],
183
+ [ "set", "$P0.userData", "$L4"]
184
+
185
+ ],
186
+
187
+ "checkUserData" => [
188
+ [ "create", "$L0", "Date"],
189
+ [ "if!=than", "$P0.userData", nil, 2],
190
+ [ "if>=than", "$P0.userData.expirationTime", "$L0.time", 1],
191
+ [ "return"],
192
+ [ "callFunc", "getUserData", "$P0"]
193
+
194
+ ]
195
+
196
+ }
197
+
198
+ def initialize(redirect_receiver, client_id, client_secret, redirect_uri, state)
199
+ @interpreter_storage = {}
200
+ @persistent_storage = [{}]
201
+ @instance_dependency_storage = {
202
+ redirect_receiver: redirect_receiver
203
+ }
204
+
205
+ ServiceCode::InitSelfTest.init_test('LinkedIn')
206
+
207
+ @interpreter_storage['clientID'] = client_id
208
+ @interpreter_storage['clientSecret'] = client_secret
209
+ @interpreter_storage['redirectUri'] = redirect_uri
210
+ @interpreter_storage['state'] = state
211
+
212
+
213
+
214
+
215
+ # call init servicecode function
216
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
217
+ # check and call the initialization function if is available
218
+
219
+ ip.call_function_sync('init', @interpreter_storage) if (SERVICE_CODE['init'])
220
+ end
221
+
222
+ def check_for_error(error)
223
+ if (error)
224
+ ServiceCode::Statistics.add_error('LinkedIn', '')
225
+ case (error.type)
226
+ when 'IllegalArgument'
227
+ raise Errors::IllegalArgumentError.new(error.message)
228
+ when 'Authentication'
229
+ raise Errors::AuthenticationError.new(error.message)
230
+ when 'NotFound'
231
+ raise Errors::NotFoundError.new(error.message)
232
+ when 'Http'
233
+ raise Errors::HttpError.new(error.message)
234
+ when 'ServiceUnavailable'
235
+ raise Errors::ServiceUnavailableError.new(error.message)
236
+ else
237
+ raise Errors::StandardError.new(error.message)
238
+ end
239
+ end
240
+ end
241
+
242
+
243
+ def get_identifier()
244
+ ServiceCode::Statistics.add_call('LinkedIn', 'get_identifier')
245
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
246
+ ip.call_function('getLIIdentifier', @interpreter_storage, nil)
247
+
248
+ check_for_error(ip.sandbox.thrown_error)
249
+ return nil || ip.get_parameter(1)
250
+ end
251
+
252
+ def get_full_name()
253
+ ServiceCode::Statistics.add_call('LinkedIn', 'get_full_name')
254
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
255
+ ip.call_function('getLIFullName', @interpreter_storage, nil)
256
+
257
+ check_for_error(ip.sandbox.thrown_error)
258
+ return nil || ip.get_parameter(1)
259
+ end
260
+
261
+ def get_email()
262
+ ServiceCode::Statistics.add_call('LinkedIn', 'get_email')
263
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
264
+ ip.call_function('getLIEmail', @interpreter_storage, nil)
265
+
266
+ check_for_error(ip.sandbox.thrown_error)
267
+ return nil || ip.get_parameter(1)
268
+ end
269
+
270
+ def get_gender()
271
+ ServiceCode::Statistics.add_call('LinkedIn', 'get_gender')
272
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
273
+ ip.call_function('getLIGender', @interpreter_storage, nil)
274
+
275
+ check_for_error(ip.sandbox.thrown_error)
276
+ return nil || ip.get_parameter(1)
277
+ end
278
+
279
+ def get_description()
280
+ ServiceCode::Statistics.add_call('LinkedIn', 'get_description')
281
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
282
+ ip.call_function('getLIDescription', @interpreter_storage, nil)
283
+
284
+ check_for_error(ip.sandbox.thrown_error)
285
+ return nil || ip.get_parameter(1)
286
+ end
287
+
288
+ def get_date_of_birth()
289
+ ServiceCode::Statistics.add_call('LinkedIn', 'get_date_of_birth')
290
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
291
+ ip.call_function('getLIDateOfBirth', @interpreter_storage, nil)
292
+
293
+ check_for_error(ip.sandbox.thrown_error)
294
+ return nil || ip.get_parameter(1)
295
+ end
296
+
297
+ def get_locale()
298
+ ServiceCode::Statistics.add_call('LinkedIn', 'get_locale')
299
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
300
+ ip.call_function('getLILocale', @interpreter_storage, nil)
301
+
302
+ check_for_error(ip.sandbox.thrown_error)
303
+ return nil || ip.get_parameter(1)
304
+ end
305
+
306
+ def get_picture_url()
307
+ ServiceCode::Statistics.add_call('LinkedIn', 'get_picture_url')
308
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
309
+ ip.call_function('getLIPictureURL', @interpreter_storage, nil)
310
+
311
+ check_for_error(ip.sandbox.thrown_error)
312
+ return nil || ip.get_parameter(1)
313
+ end
314
+
315
+ def login()
316
+ ServiceCode::Statistics.add_call('LinkedIn', 'login')
317
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
318
+ ip.call_function('loginLI', @interpreter_storage)
319
+
320
+ check_for_error(ip.sandbox.thrown_error)
321
+ return nil
322
+ end
323
+
324
+ def logout()
325
+ ServiceCode::Statistics.add_call('LinkedIn', 'logout')
326
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
327
+ ip.call_function('logoutLI', @interpreter_storage)
328
+
329
+ check_for_error(ip.sandbox.thrown_error)
330
+ return nil
331
+ end
332
+
333
+ def advanced_request(specification)
334
+ ServiceCode::Statistics.add_call('LinkedIn', 'advanced_request')
335
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
336
+ ip.call_function('AdvancedRequestSupporter:advancedRequest', @interpreter_storage, nil, specification)
337
+
338
+ check_for_error(ip.sandbox.thrown_error)
339
+ return nil || ip.get_parameter(1)
340
+ end
341
+
342
+
343
+ def save_as_string
344
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
345
+ return ip.save_as_string()
346
+ end
347
+
348
+ def load_as_string(saved_state)
349
+ sandbox = ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage)
350
+ ip = ServiceCode::Interpreter.new(sandbox)
351
+ ip.load_as_string(saved_state)
352
+ @persistent_storage = sandbox.persistent_storage
353
+ end
354
+
355
+ def resume_login(execution_state, callback)
356
+ sandbox = ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage)
357
+ sandbox.load_state_from_string(execution_state)
358
+ ip = ServiceCode::Interpreter.new(sandbox)
359
+ ip.resume_function('Authenticating:login', @interpreter_storage, callback)
360
+ end
361
+ end
362
+ end
363
+ end
@@ -0,0 +1,346 @@
1
+ require_relative '../servicecode/Interpreter'
2
+ require_relative '../servicecode/Sandbox'
3
+ require_relative '../errors/DetailErrors'
4
+ require_relative '../servicecode/InitSelfTest'
5
+ require_relative '../statistics/Statistics'
6
+
7
+ module CloudRailSi
8
+ module Services
9
+ class MicrosoftLive
10
+
11
+ SERVICE_CODE = {
12
+
13
+ "AdvancedRequestSupporter:advancedRequest" => [
14
+ [ "create", "$L0", "Object"],
15
+ [ "create", "$L0.url", "String"],
16
+ [ "if!=than", "$P2.appendBaseUrl", 0, 1],
17
+ [ "set", "$L0.url", "https://apis.live.net/v5.0"],
18
+ [ "string.concat", "$L0.url", "$L0.url", "$P2.url"],
19
+ [ "set", "$L0.requestHeaders", "$P2.headers"],
20
+ [ "set", "$L0.method", "$P2.method"],
21
+ [ "set", "$L0.requestBody", "$P2.body"],
22
+ [ "if!=than", "$P2.appendAuthorization", 0, 6],
23
+ [ "callFunc", "checkAuthentication", "$P0"],
24
+ [ "string.indexOf", "$L2", "$P2.url", "?"],
25
+ [ "if==than", "$L2", -1, 2],
26
+ [ "string.concat", "$L0.url", "$L0.url", "?access_token=", "$S0.accessToken"],
27
+ [ "jumpRel", 1],
28
+ [ "string.concat", "$L0.url", "$L0.url", "&access_token=", "$S0.accessToken"],
29
+ [ "http.requestCall", "$L1", "$L0"],
30
+ [ "if!=than", "$P2.checkErrors", 0, 1],
31
+ [ "callFunc", "validateResponse", "$P0", "$L1"],
32
+ [ "create", "$P1", "AdvancedRequestResponse"],
33
+ [ "set", "$P1.status", "$L1.code"],
34
+ [ "set", "$P1.headers", "$L1.responseHeaders"],
35
+ [ "set", "$P1.body", "$L1.responseBody"]
36
+
37
+ ],
38
+
39
+ "Authenticating:login" => [
40
+ [ "callFunc", "checkAuthentication", "$P0"]
41
+
42
+ ],
43
+
44
+ "Authenticating:logout" => [
45
+ [ "set", "$S0.accessToken", nil],
46
+ [ "set", "$P0.userInfo", nil]
47
+
48
+ ],
49
+
50
+ "Profile:getIdentifier" => [
51
+ [ "callFunc", "checkUserInfo", "$P0"],
52
+ [ "string.concat", "$P1", "microsoftlive-", "$P0.userInfo.id"]
53
+
54
+ ],
55
+
56
+ "Profile:getFullName" => [
57
+ [ "callFunc", "checkUserInfo", "$P0"],
58
+ [ "if!=than", "$P0.userInfo.name", nil, 1],
59
+ [ "set", "$P1", "$P0.userInfo.name"]
60
+
61
+ ],
62
+
63
+ "Profile:getEmail" => [
64
+ [ "callFunc", "checkUserInfo", "$P0"],
65
+ [ "set", "$P1", "$P0.userInfo.emails.account"]
66
+
67
+ ],
68
+
69
+ "Profile:getGender" => [
70
+
71
+ ],
72
+
73
+ "Profile:getDescription" => [
74
+
75
+ ],
76
+
77
+ "Profile:getDateOfBirth" => [
78
+ [ "callFunc", "checkUserInfo", "$P0"],
79
+ [ "create", "$P1", "DateOfBirth"],
80
+ [ "if!=than", "$P0.userInfo.birth_day", nil, 1],
81
+ [ "set", "$P1.day", "$P0.userInfo.birth_day"],
82
+ [ "if!=than", "$P0.userInfo.birth_month", nil, 1],
83
+ [ "set", "$P1.month", "$P0.userInfo.birth_month"],
84
+ [ "if!=than", "$P0.userInfo.birth_year", nil, 1],
85
+ [ "set", "$P1.year", "$P0.userInfo.birth_year"]
86
+
87
+ ],
88
+
89
+ "Profile:getLocale" => [
90
+ [ "callFunc", "checkUserInfo", "$P0"],
91
+ [ "if!=than", "$P0.userInfo.locale", nil, 1],
92
+ [ "string.substring", "$P1", "$P0.userInfo.locale", 0, 2]
93
+
94
+ ],
95
+
96
+ "Profile:getPictureURL" => [
97
+ [ "callFunc", "checkUserInfo", "$P0"],
98
+ [ "string.concat", "$P1", "https://apis.live.net/v5.0/", "$P0.userInfo.id", "/picture"]
99
+
100
+ ],
101
+
102
+ "checkUserInfo" => [
103
+ [ "create", "$L0", "Date"],
104
+ [ "if!=than", "$P0.userInfo", nil, 2],
105
+ [ "if>than", "$P0.expirationTime", "$L0", 1],
106
+ [ "return"],
107
+ [ "callFunc", "checkAuthentication", "$P0"],
108
+ [ "create", "$L2", "Object"],
109
+ [ "string.concat", "$L2.url", "https://apis.live.net/v5.0/me?access_token=", "$S0.accessToken"],
110
+ [ "set", "$L2.method", "GET"],
111
+ [ "http.requestCall", "$L3", "$L2"],
112
+ [ "callFunc", "validateResponse", "$P0", "$L3"],
113
+ [ "json.parse", "$P0.userInfo", "$L3.responseBody"],
114
+ [ "create", "$P0.expirationTime", "Date"],
115
+ [ "math.add", "$P0.expirationTime.time", "$P0.expirationTime.time", 60000]
116
+
117
+ ],
118
+
119
+ "checkAuthentication" => [
120
+ [ "create", "$L0", "Date"],
121
+ [ "if==than", "$S0.accessToken", nil, 2],
122
+ [ "callFunc", "authenticate", "$P0", "accessToken"],
123
+ [ "return"],
124
+ [ "create", "$L1", "Date"],
125
+ [ "set", "$L1.time", "$S0.expireIn"],
126
+ [ "if<than", "$L1", "$L0", 1],
127
+ [ "callFunc", "authenticate", "$P0", "refreshToken"]
128
+
129
+ ],
130
+
131
+ "authenticate" => [
132
+ [ "create", "$L2", "String"],
133
+ [ "if==than", "$P1", "accessToken", 4],
134
+ [ "string.concat", "$L0", "https://login.live.com/oauth20_authorize.srf?client_id=", "$P0.clientID", "&scope=", "wl.emails%20wl.birthday", "&response_type=code&redirect_uri=", "$P0.redirectUri"],
135
+ [ "awaitCodeRedirect", "$L1", "$L0"],
136
+ [ "string.concat", "$L2", "client_id=", "$P0.clientID", "&redirect_uri=", "$P0.redirectUri", "&client_secret=", "$P0.clientSecret", "&code=", "$L1", "&grant_type=authorization_code"],
137
+ [ "jumpRel", 1],
138
+ [ "string.concat", "$L2", "client_id=", "$P0.clientID", "&redirect_uri=", "$P0.redirectUri", "&client_secret=", "$P0.clientSecret", "&refresh_token=", "$S0.refreshToken", "&grant_type=refresh_token"],
139
+ [ "stream.stringToStream", "$L3", "$L2"],
140
+ [ "create", "$L4", "Object"],
141
+ [ "set", "$L4", "application/x-www-form-urlencoded", "Content-Type"],
142
+ [ "size", "$L19", "$L2"],
143
+ [ "string.concat", "$L4.Content-Length", "$L19"],
144
+ [ "create", "$L5", "Object"],
145
+ [ "set", "$L5.url", "https://login.live.com/oauth20_token.srf"],
146
+ [ "set", "$L5.method", "POST"],
147
+ [ "set", "$L5.requestBody", "$L3"],
148
+ [ "set", "$L5.requestHeaders", "$L4"],
149
+ [ "http.requestCall", "$L6", "$L5"],
150
+ [ "callFunc", "validateResponse", "$P0", "$L6"],
151
+ [ "stream.streamToString", "$L7", "$L6.responseBody"],
152
+ [ "json.parse", "$L8", "$L7"],
153
+ [ "set", "$S0.accessToken", "$L8.access_token"],
154
+ [ "set", "$S0.refreshToken", "$L8.refresh_token"],
155
+ [ "create", "$L10", "Date"],
156
+ [ "math.multiply", "$L9", "$L8.expires_in", 1000],
157
+ [ "math.add", "$L9", "$L9", "$L10.time", -60000],
158
+ [ "set", "$S0.expireIn", "$L9"]
159
+
160
+ ],
161
+
162
+ "validateResponse" => [
163
+ [ "if>=than", "$P1.code", 400, 12],
164
+ [ "if==than", "$P1.code", 401, 2],
165
+ [ "create", "$L3", "Error", "Invalid credentials or access rights. Make sure that your application has read and write permission.", "Authentication"],
166
+ [ "throwError", "$L3"],
167
+ [ "if==than", "$P1.code", 503, 2],
168
+ [ "create", "$L3", "Error", "Service unavailable. Try again later.", "ServiceUnavailable"],
169
+ [ "throwError", "$L3"],
170
+ [ "json.parse", "$L0", "$P1.responseBody"],
171
+ [ "set", "$L1", "$L0.error"],
172
+ [ "set", "$L2", "$L1.message"],
173
+ [ "string.concat", "$L2", "$P1.code", " - ", "$L2"],
174
+ [ "create", "$L3", "Error", "$L2", "Http"],
175
+ [ "throwError", "$L3"]
176
+
177
+ ]
178
+
179
+ }
180
+
181
+ def initialize(redirect_receiver, client_id, client_secret, redirect_uri, state)
182
+ @interpreter_storage = {}
183
+ @persistent_storage = [{}]
184
+ @instance_dependency_storage = {
185
+ redirect_receiver: redirect_receiver
186
+ }
187
+
188
+ ServiceCode::InitSelfTest.init_test('MicrosoftLive')
189
+
190
+ @interpreter_storage['clientID'] = client_id
191
+ @interpreter_storage['clientSecret'] = client_secret
192
+ @interpreter_storage['redirectUri'] = redirect_uri
193
+ @interpreter_storage['state'] = state
194
+
195
+
196
+
197
+
198
+ # call init servicecode function
199
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
200
+ # check and call the initialization function if is available
201
+
202
+ ip.call_function_sync('init', @interpreter_storage) if (SERVICE_CODE['init'])
203
+ end
204
+
205
+ def check_for_error(error)
206
+ if (error)
207
+ ServiceCode::Statistics.add_error('MicrosoftLive', '')
208
+ case (error.type)
209
+ when 'IllegalArgument'
210
+ raise Errors::IllegalArgumentError.new(error.message)
211
+ when 'Authentication'
212
+ raise Errors::AuthenticationError.new(error.message)
213
+ when 'NotFound'
214
+ raise Errors::NotFoundError.new(error.message)
215
+ when 'Http'
216
+ raise Errors::HttpError.new(error.message)
217
+ when 'ServiceUnavailable'
218
+ raise Errors::ServiceUnavailableError.new(error.message)
219
+ else
220
+ raise Errors::StandardError.new(error.message)
221
+ end
222
+ end
223
+ end
224
+
225
+
226
+ def get_identifier()
227
+ ServiceCode::Statistics.add_call('MicrosoftLive', 'get_identifier')
228
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
229
+ ip.call_function('Profile:getIdentifier', @interpreter_storage, nil)
230
+
231
+ check_for_error(ip.sandbox.thrown_error)
232
+ return nil || ip.get_parameter(1)
233
+ end
234
+
235
+ def get_full_name()
236
+ ServiceCode::Statistics.add_call('MicrosoftLive', 'get_full_name')
237
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
238
+ ip.call_function('Profile:getFullName', @interpreter_storage, nil)
239
+
240
+ check_for_error(ip.sandbox.thrown_error)
241
+ return nil || ip.get_parameter(1)
242
+ end
243
+
244
+ def get_email()
245
+ ServiceCode::Statistics.add_call('MicrosoftLive', 'get_email')
246
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
247
+ ip.call_function('Profile:getEmail', @interpreter_storage, nil)
248
+
249
+ check_for_error(ip.sandbox.thrown_error)
250
+ return nil || ip.get_parameter(1)
251
+ end
252
+
253
+ def get_gender()
254
+ ServiceCode::Statistics.add_call('MicrosoftLive', 'get_gender')
255
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
256
+ ip.call_function('Profile:getGender', @interpreter_storage, nil)
257
+
258
+ check_for_error(ip.sandbox.thrown_error)
259
+ return nil || ip.get_parameter(1)
260
+ end
261
+
262
+ def get_description()
263
+ ServiceCode::Statistics.add_call('MicrosoftLive', 'get_description')
264
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
265
+ ip.call_function('Profile:getDescription', @interpreter_storage, nil)
266
+
267
+ check_for_error(ip.sandbox.thrown_error)
268
+ return nil || ip.get_parameter(1)
269
+ end
270
+
271
+ def get_date_of_birth()
272
+ ServiceCode::Statistics.add_call('MicrosoftLive', 'get_date_of_birth')
273
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
274
+ ip.call_function('Profile:getDateOfBirth', @interpreter_storage, nil)
275
+
276
+ check_for_error(ip.sandbox.thrown_error)
277
+ return nil || ip.get_parameter(1)
278
+ end
279
+
280
+ def get_locale()
281
+ ServiceCode::Statistics.add_call('MicrosoftLive', 'get_locale')
282
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
283
+ ip.call_function('Profile:getLocale', @interpreter_storage, nil)
284
+
285
+ check_for_error(ip.sandbox.thrown_error)
286
+ return nil || ip.get_parameter(1)
287
+ end
288
+
289
+ def get_picture_url()
290
+ ServiceCode::Statistics.add_call('MicrosoftLive', 'get_picture_url')
291
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
292
+ ip.call_function('Profile:getPictureURL', @interpreter_storage, nil)
293
+
294
+ check_for_error(ip.sandbox.thrown_error)
295
+ return nil || ip.get_parameter(1)
296
+ end
297
+
298
+ def login()
299
+ ServiceCode::Statistics.add_call('MicrosoftLive', 'login')
300
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
301
+ ip.call_function('Authenticating:login', @interpreter_storage)
302
+
303
+ check_for_error(ip.sandbox.thrown_error)
304
+ return nil
305
+ end
306
+
307
+ def logout()
308
+ ServiceCode::Statistics.add_call('MicrosoftLive', 'logout')
309
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
310
+ ip.call_function('Authenticating:logout', @interpreter_storage)
311
+
312
+ check_for_error(ip.sandbox.thrown_error)
313
+ return nil
314
+ end
315
+
316
+ def advanced_request(specification)
317
+ ServiceCode::Statistics.add_call('MicrosoftLive', 'advanced_request')
318
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
319
+ ip.call_function('AdvancedRequestSupporter:advancedRequest', @interpreter_storage, nil, specification)
320
+
321
+ check_for_error(ip.sandbox.thrown_error)
322
+ return nil || ip.get_parameter(1)
323
+ end
324
+
325
+
326
+ def save_as_string
327
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
328
+ return ip.save_as_string()
329
+ end
330
+
331
+ def load_as_string(saved_state)
332
+ sandbox = ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage)
333
+ ip = ServiceCode::Interpreter.new(sandbox)
334
+ ip.load_as_string(saved_state)
335
+ @persistent_storage = sandbox.persistent_storage
336
+ end
337
+
338
+ def resume_login(execution_state, callback)
339
+ sandbox = ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage)
340
+ sandbox.load_state_from_string(execution_state)
341
+ ip = ServiceCode::Interpreter.new(sandbox)
342
+ ip.resume_function('Authenticating:login', @interpreter_storage, callback)
343
+ end
344
+ end
345
+ end
346
+ end