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,354 @@
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 GitHub
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.github.com"],
18
+ [ "string.concat", "$L0.url", "$L0.url", "$P2.url"],
19
+ [ "set", "$L0.requestHeaders", "$P2.headers"],
20
+ [ "if==than", "$L0.requestHeaders.User-Agent", nil, 1],
21
+ [ "set", "$L0.requestHeaders.User-Agent", "CloudRailSI"],
22
+ [ "set", "$L0.method", "$P2.method"],
23
+ [ "set", "$L0.requestBody", "$P2.body"],
24
+ [ "if!=than", "$P2.appendAuthorization", 0, 2],
25
+ [ "callFunc", "checkAuthentication", "$P0"],
26
+ [ "string.concat", "$L0.requestHeaders.Authorization", "Bearer ", "$S0.accessToken"],
27
+ [ "http.requestCall", "$L1", "$L0"],
28
+ [ "if!=than", "$P2.checkErrors", 0, 1],
29
+ [ "callFunc", "validateResponse", "$P0", "$L1"],
30
+ [ "create", "$P1", "AdvancedRequestResponse"],
31
+ [ "set", "$P1.status", "$L1.code"],
32
+ [ "set", "$P1.headers", "$L1.responseHeaders"],
33
+ [ "set", "$P1.body", "$L1.responseBody"]
34
+
35
+ ],
36
+
37
+ "getGHIdentifier" => [
38
+ [ "callFunc", "checkAuthentication", "$P0"],
39
+ [ "if==than", "$P0.cachedObject", nil, 2],
40
+ [ "callFunc", "makeHTTPRequest", "$P0"],
41
+ [ "jumpRel", 1],
42
+ [ "callFunc", "checkExpirationTime", "$P0"],
43
+ [ "string.concat", "$L2", "$P0.cachedObject.id", ""],
44
+ [ "string.concat", "$P1", "github-", "$L2"]
45
+
46
+ ],
47
+
48
+ "getGHFullName" => [
49
+ [ "callFunc", "checkAuthentication", "$P0"],
50
+ [ "if==than", "$P0.cachedObject", nil, 2],
51
+ [ "callFunc", "makeHTTPRequest", "$P0"],
52
+ [ "jumpRel", 1],
53
+ [ "callFunc", "checkExpirationTime", "$P0"],
54
+ [ "set", "$P1", "$P0.cachedObject.name"]
55
+
56
+ ],
57
+
58
+ "getGHEmail" => [
59
+ [ "callFunc", "checkAuthentication", "$P0"],
60
+ [ "if==than", "$P0.cachedObject", nil, 2],
61
+ [ "callFunc", "makeHTTPRequest", "$P0"],
62
+ [ "jumpRel", 1],
63
+ [ "callFunc", "checkExpirationTime", "$P0"],
64
+ [ "set", "$P1", "$P0.cachedObject.email"]
65
+
66
+ ],
67
+
68
+ "getGHGender" => [
69
+ [ "set", "$P1", nil]
70
+
71
+ ],
72
+
73
+ "getGHDescription" => [
74
+ [ "callFunc", "checkAuthentication", "$P0"],
75
+ [ "if==than", "$P0.cachedObject", nil, 2],
76
+ [ "callFunc", "makeHTTPRequest", "$P0"],
77
+ [ "jumpRel", 1],
78
+ [ "callFunc", "checkExpirationTime", "$P0"],
79
+ [ "set", "$P1", "$P0.cachedObject.bio"]
80
+
81
+ ],
82
+
83
+ "getGHDateOfBirth" => [
84
+ [ "create", "$P1", "DateOfBirth"]
85
+
86
+ ],
87
+
88
+ "getGHLocale" => [
89
+ [ "set", "$P1", nil]
90
+
91
+ ],
92
+
93
+ "getGHPictureURL" => [
94
+ [ "callFunc", "checkAuthentication", "$P0"],
95
+ [ "if==than", "$P0.cachedObject", nil, 2],
96
+ [ "callFunc", "makeHTTPRequest", "$P0"],
97
+ [ "jumpRel", 1],
98
+ [ "callFunc", "checkExpirationTime", "$P0"],
99
+ [ "set", "$P1", "$P0.cachedObject.avatar_url"]
100
+
101
+ ],
102
+
103
+ "loginGH" => [
104
+ [ "callFunc", "checkAuthentication", "$P0"]
105
+
106
+ ],
107
+
108
+ "logoutGH" => [
109
+ [ "set", "$S0.accessToken", nil],
110
+ [ "set", "$P0.cachedObject", nil]
111
+
112
+ ],
113
+
114
+ "makeHTTPRequest" => [
115
+ [ "create", "$L0", "Object"],
116
+ [ "set", "$L0.method", "GET"],
117
+ [ "set", "$L0.url", "https://api.github.com/user"],
118
+ [ "create", "$L0.requestHeaders", "Object"],
119
+ [ "string.concat", "$L0.requestHeaders.Authorization", "token ", "$S0.accessToken"],
120
+ [ "set", "$L0.requestHeaders.User-Agent", "CloudRailSI"],
121
+ [ "create", "$L1", "Object"],
122
+ [ "http.requestCall", "$L1", "$L0"],
123
+ [ "callFunc", "validateResponse", "$P0", "$L1"],
124
+ [ "json.parse", "$P0.cachedObject", "$L1.responseBody"],
125
+ [ "create", "$L2", "Date"],
126
+ [ "set", "$P0.readTime", "$L2.time"]
127
+
128
+ ],
129
+
130
+ "checkExpirationTime" => [
131
+ [ "create", "$L0", "Date"],
132
+ [ "math.multiply", "$L1", "$P0.readTime", -1],
133
+ [ "math.add", "$L2", "$L0.time", "$L1"],
134
+ [ "if>than", "$L2", 60000, 1],
135
+ [ "callFunc", "makeHTTPRequest", "$P0"]
136
+
137
+ ],
138
+
139
+ "checkAuthentication" => [
140
+ [ "if==than", "$S0.accessToken", nil, 2],
141
+ [ "callFunc", "authenticate", "$P0"],
142
+ [ "return"]
143
+
144
+ ],
145
+
146
+ "authenticate" => [
147
+ [ "create", "$L0", "String"],
148
+ [ "string.concat", "$L0", "https://github.com/login/oauth/authorize?client_id=", "$P0.clientId", "&redirect_uri=", "$P0.redirectUri", "&state=", "$P0.state"],
149
+ [ "awaitCodeRedirect", "$L1", "$L0"],
150
+ [ "string.concat", "$L2", "client_id=", "$P0.clientId", "&client_secret=", "$P0.clientSecret", "&code=", "$L1"],
151
+ [ "stream.stringToStream", "$L3", "$L2"],
152
+ [ "create", "$L4", "Object"],
153
+ [ "set", "$L4.Accept", "application/json"],
154
+ [ "set", "$L4", "application/x-www-form-urlencoded", "Content-Type"],
155
+ [ "create", "$L5", "Object"],
156
+ [ "set", "$L5.url", "https://github.com/login/oauth/access_token"],
157
+ [ "set", "$L5.method", "POST"],
158
+ [ "set", "$L5.requestBody", "$L3"],
159
+ [ "set", "$L5.requestHeaders", "$L4"],
160
+ [ "http.requestCall", "$L6", "$L5"],
161
+ [ "callFunc", "validateResponse", "$P0", "$L6"],
162
+ [ "create", "$L7", "String"],
163
+ [ "stream.streamToString", "$L7", "$L6.responseBody"],
164
+ [ "json.parse", "$L8", "$L7"],
165
+ [ "set", "$S0.accessToken", "$L8.access_token"]
166
+
167
+ ],
168
+
169
+ "validateResponse" => [
170
+ [ "if>=than", "$P1.code", 400, 13],
171
+ [ "json.parse", "$L0", "$P1.responseBody"],
172
+ [ "set", "$L2", "$L0.message"],
173
+ [ "if==than", "$P1.code", 401, 2],
174
+ [ "create", "$L3", "Error", "$L2", "Authentication"],
175
+ [ "throwError", "$L3"],
176
+ [ "if==than", "$P1.code", 404, 2],
177
+ [ "create", "$L3", "Error", "$L2", "NotFound"],
178
+ [ "throwError", "$L3"],
179
+ [ "if==than", "$P1.code", 503, 2],
180
+ [ "create", "$L3", "Error", "$L2", "ServiceUnavailable"],
181
+ [ "throwError", "$L3"],
182
+ [ "create", "$L3", "Error", "$L2", "Http"],
183
+ [ "throwError", "$L3"]
184
+
185
+ ]
186
+
187
+ }
188
+
189
+ def initialize(redirect_receiver, client_id, client_secret, redirect_uri, state)
190
+ @interpreter_storage = {}
191
+ @persistent_storage = [{}]
192
+ @instance_dependency_storage = {
193
+ redirect_receiver: redirect_receiver
194
+ }
195
+
196
+ ServiceCode::InitSelfTest.init_test('GitHub')
197
+
198
+ @interpreter_storage['clientId'] = client_id
199
+ @interpreter_storage['clientSecret'] = client_secret
200
+ @interpreter_storage['redirectUri'] = redirect_uri
201
+ @interpreter_storage['state'] = state
202
+
203
+
204
+
205
+
206
+ # call init servicecode function
207
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
208
+ # check and call the initialization function if is available
209
+
210
+ ip.call_function_sync('init', @interpreter_storage) if (SERVICE_CODE['init'])
211
+ end
212
+
213
+ def check_for_error(error)
214
+ if (error)
215
+ ServiceCode::Statistics.add_error('GitHub', '')
216
+ case (error.type)
217
+ when 'IllegalArgument'
218
+ raise Errors::IllegalArgumentError.new(error.message)
219
+ when 'Authentication'
220
+ raise Errors::AuthenticationError.new(error.message)
221
+ when 'NotFound'
222
+ raise Errors::NotFoundError.new(error.message)
223
+ when 'Http'
224
+ raise Errors::HttpError.new(error.message)
225
+ when 'ServiceUnavailable'
226
+ raise Errors::ServiceUnavailableError.new(error.message)
227
+ else
228
+ raise Errors::StandardError.new(error.message)
229
+ end
230
+ end
231
+ end
232
+
233
+
234
+ def get_identifier()
235
+ ServiceCode::Statistics.add_call('GitHub', 'get_identifier')
236
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
237
+ ip.call_function('getGHIdentifier', @interpreter_storage, nil)
238
+
239
+ check_for_error(ip.sandbox.thrown_error)
240
+ return nil || ip.get_parameter(1)
241
+ end
242
+
243
+ def get_full_name()
244
+ ServiceCode::Statistics.add_call('GitHub', 'get_full_name')
245
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
246
+ ip.call_function('getGHFullName', @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_email()
253
+ ServiceCode::Statistics.add_call('GitHub', 'get_email')
254
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
255
+ ip.call_function('getGHEmail', @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_gender()
262
+ ServiceCode::Statistics.add_call('GitHub', 'get_gender')
263
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
264
+ ip.call_function('getGHGender', @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_description()
271
+ ServiceCode::Statistics.add_call('GitHub', 'get_description')
272
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
273
+ ip.call_function('getGHDescription', @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_date_of_birth()
280
+ ServiceCode::Statistics.add_call('GitHub', 'get_date_of_birth')
281
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
282
+ ip.call_function('getGHDateOfBirth', @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_locale()
289
+ ServiceCode::Statistics.add_call('GitHub', 'get_locale')
290
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
291
+ ip.call_function('getGHLocale', @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_picture_url()
298
+ ServiceCode::Statistics.add_call('GitHub', 'get_picture_url')
299
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
300
+ ip.call_function('getGHPictureURL', @interpreter_storage, nil)
301
+
302
+ check_for_error(ip.sandbox.thrown_error)
303
+ return nil || ip.get_parameter(1)
304
+ end
305
+
306
+ def login()
307
+ ServiceCode::Statistics.add_call('GitHub', 'login')
308
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
309
+ ip.call_function('loginGH', @interpreter_storage)
310
+
311
+ check_for_error(ip.sandbox.thrown_error)
312
+ return nil
313
+ end
314
+
315
+ def logout()
316
+ ServiceCode::Statistics.add_call('GitHub', 'logout')
317
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
318
+ ip.call_function('logoutGH', @interpreter_storage)
319
+
320
+ check_for_error(ip.sandbox.thrown_error)
321
+ return nil
322
+ end
323
+
324
+ def advanced_request(specification)
325
+ ServiceCode::Statistics.add_call('GitHub', 'advanced_request')
326
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
327
+ ip.call_function('AdvancedRequestSupporter:advancedRequest', @interpreter_storage, nil, specification)
328
+
329
+ check_for_error(ip.sandbox.thrown_error)
330
+ return nil || ip.get_parameter(1)
331
+ end
332
+
333
+
334
+ def save_as_string
335
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
336
+ return ip.save_as_string()
337
+ end
338
+
339
+ def load_as_string(saved_state)
340
+ sandbox = ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage)
341
+ ip = ServiceCode::Interpreter.new(sandbox)
342
+ ip.load_as_string(saved_state)
343
+ @persistent_storage = sandbox.persistent_storage
344
+ end
345
+
346
+ def resume_login(execution_state, callback)
347
+ sandbox = ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage)
348
+ sandbox.load_state_from_string(execution_state)
349
+ ip = ServiceCode::Interpreter.new(sandbox)
350
+ ip.resume_function('Authenticating:login', @interpreter_storage, callback)
351
+ end
352
+ end
353
+ end
354
+ end
@@ -0,0 +1,309 @@
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 GooglePlaces
10
+
11
+ SERVICE_CODE = {
12
+
13
+ "init" => [
14
+ [ "create", "$P0.crToPlaces", "Object"],
15
+ [ "create", "$P0.placesToCr", "Object"],
16
+ [ "callFunc", "addCategory", "$P0", "airport", "airport"],
17
+ [ "callFunc", "addCategory", "$P0", "amusement_park", "amusement_park"],
18
+ [ "callFunc", "addCategory", "$P0", "aquarium", "aquarium"],
19
+ [ "callFunc", "addCategory", "$P0", "art_gallery", "art_gallery"],
20
+ [ "callFunc", "addCategory", "$P0", "bakery", "bakery"],
21
+ [ "callFunc", "addCategory", "$P0", "bank", "bank"],
22
+ [ "callFunc", "addCategory", "$P0", "bar", "bar"],
23
+ [ "callFunc", "addCategory", "$P0", "beauty_salon", "beauty_salon"],
24
+ [ "callFunc", "addCategory", "$P0", "bicycle_store", "bicycle_store"],
25
+ [ "callFunc", "addCategory", "$P0", "book_store", "book_store"],
26
+ [ "callFunc", "addCategory", "$P0", "bowling_alley", "bowling_alley"],
27
+ [ "callFunc", "addCategory", "$P0", "bus_station", "bus_station"],
28
+ [ "callFunc", "addCategory", "$P0", "cafe", "cafe"],
29
+ [ "callFunc", "addCategory", "$P0", "car_dealer", "car_dealer"],
30
+ [ "callFunc", "addCategory", "$P0", "car_rental", "car_rental"],
31
+ [ "callFunc", "addCategory", "$P0", "car_wash", "car_wash"],
32
+ [ "callFunc", "addCategory", "$P0", "casino", "casino"],
33
+ [ "callFunc", "addCategory", "$P0", "cemetery", "cemetery"],
34
+ [ "callFunc", "addCategory", "$P0", "church", "church"],
35
+ [ "callFunc", "addCategory", "$P0", "clothing_store", "clothing_store"],
36
+ [ "callFunc", "addCategory", "$P0", "convenience_store", "convenience_store"],
37
+ [ "callFunc", "addCategory", "$P0", "courthouse", "courthouse"],
38
+ [ "callFunc", "addCategory", "$P0", "dentist", "dentist"],
39
+ [ "callFunc", "addCategory", "$P0", "department_store", "department_store"],
40
+ [ "callFunc", "addCategory", "$P0", "doctor", "doctor"],
41
+ [ "callFunc", "addCategory", "$P0", "electronics_store", "electronics_store"],
42
+ [ "callFunc", "addCategory", "$P0", "embassy", "embassy"],
43
+ [ "callFunc", "addCategory", "$P0", "finance", "finance"],
44
+ [ "callFunc", "addCategory", "$P0", "fire_station", "fire_station"],
45
+ [ "callFunc", "addCategory", "$P0", "florist", "florist"],
46
+ [ "callFunc", "addCategory", "$P0", "food", "food"],
47
+ [ "callFunc", "addCategory", "$P0", "funeral_home", "funeral_home"],
48
+ [ "callFunc", "addCategory", "$P0", "furniture_store", "furniture_store"],
49
+ [ "callFunc", "addCategory", "$P0", "gas_station", "gas_station"],
50
+ [ "callFunc", "addCategory", "$P0", "grocery_or_supermarket", "grocery_or_supermarket"],
51
+ [ "callFunc", "addCategory", "$P0", "gym", "gym"],
52
+ [ "callFunc", "addCategory", "$P0", "hardware_store", "hardware_store"],
53
+ [ "callFunc", "addCategory", "$P0", "health", "health"],
54
+ [ "callFunc", "addCategory", "$P0", "hindu_temple", "hindu_temple"],
55
+ [ "callFunc", "addCategory", "$P0", "hospital", "hospital"],
56
+ [ "callFunc", "addCategory", "$P0", "jewelry_store", "jewelry_store"],
57
+ [ "callFunc", "addCategory", "$P0", "laundry", "laundry"],
58
+ [ "callFunc", "addCategory", "$P0", "lawyer", "lawyer"],
59
+ [ "callFunc", "addCategory", "$P0", "library", "library"],
60
+ [ "callFunc", "addCategory", "$P0", "locksmith", "locksmith"],
61
+ [ "callFunc", "addCategory", "$P0", "mosque", "mosque"],
62
+ [ "callFunc", "addCategory", "$P0", "movie_theater", "movie_theater"],
63
+ [ "callFunc", "addCategory", "$P0", "museum", "museum"],
64
+ [ "callFunc", "addCategory", "$P0", "night_club", "night_club"],
65
+ [ "callFunc", "addCategory", "$P0", "parks", "parks"],
66
+ [ "callFunc", "addCategory", "$P0", "parking", "parking"],
67
+ [ "callFunc", "addCategory", "$P0", "pet_store", "pet_store"],
68
+ [ "callFunc", "addCategory", "$P0", "pharmacy", "pharmacy"],
69
+ [ "callFunc", "addCategory", "$P0", "physiotherapist", "physiotherapist"],
70
+ [ "callFunc", "addCategory", "$P0", "police", "police"],
71
+ [ "callFunc", "addCategory", "$P0", "post_office", "post_office"],
72
+ [ "callFunc", "addCategory", "$P0", "real_estate_agency", "real_estate_agency"],
73
+ [ "callFunc", "addCategory", "$P0", "restaurant", "restaurant"],
74
+ [ "callFunc", "addCategory", "$P0", "rv_park", "rv_park"],
75
+ [ "callFunc", "addCategory", "$P0", "school", "school"],
76
+ [ "callFunc", "addCategory", "$P0", "shoe_store", "shoe_store"],
77
+ [ "callFunc", "addCategory", "$P0", "shopping_mall", "shopping_mall"],
78
+ [ "callFunc", "addCategory", "$P0", "spa", "spa"],
79
+ [ "callFunc", "addCategory", "$P0", "stadium", "stadium"],
80
+ [ "callFunc", "addCategory", "$P0", "synagogue", "synagogue"],
81
+ [ "callFunc", "addCategory", "$P0", "taxi_stand", "taxi_stand"],
82
+ [ "callFunc", "addCategory", "$P0", "train_station", "train_station"],
83
+ [ "callFunc", "addCategory", "$P0", "travel_agency", "travel_agency"],
84
+ [ "callFunc", "addCategory", "$P0", "university", "university"],
85
+ [ "callFunc", "addCategory", "$P0", "veterinary_care", "veterinary_care"],
86
+ [ "callFunc", "addCategory", "$P0", "zoo", "zoo"]
87
+
88
+ ],
89
+
90
+ "getNearbyPOIs" => [
91
+ [ "callFunc", "checkNull", "$P0", "$P2", "Latitude"],
92
+ [ "callFunc", "checkNull", "$P0", "$P3", "Longitude"],
93
+ [ "callFunc", "checkNull", "$P0", "$P4", "Radius"],
94
+ [ "callFunc", "checkLessThan", "$P0", "$P2", -90, "Latitude"],
95
+ [ "callFunc", "checkLessThan", "$P0", "$P3", -180, "Longitude"],
96
+ [ "callFunc", "checkLessThan", "$P0", "$P4", 0, "Radius"],
97
+ [ "callFunc", "checkGreaterThan", "$P0", "$P2", 90, "Latitude"],
98
+ [ "callFunc", "checkGreaterThan", "$P0", "$P3", 180, "Longitude"],
99
+ [ "callFunc", "checkGreaterThan", "$P0", "$P4", 40000, "Radius"],
100
+ [ "if!=than", "$P6", nil, 1],
101
+ [ "callFunc", "checkIsEmpty", "$P0", "$P6", "Categories"],
102
+ [ "create", "$L0", "Object"],
103
+ [ "set", "$L0.method", "GET"],
104
+ [ "create", "$L1", "String", "https://maps.googleapis.com/maps/api/place/nearbysearch/json?"],
105
+ [ "string.concat", "$L1", "$L1", "key=", "$P0.apiKey"],
106
+ [ "string.concat", "$L2", "$P2", ",", "$P3"],
107
+ [ "string.urlEncode", "$L2", "$L2"],
108
+ [ "string.concat", "$L1", "$L1", "&location=", "$L2"],
109
+ [ "string.concat", "$L1", "$L1", "&radius=", "$P4"],
110
+ [ "if!=than", "$P5", nil, 2],
111
+ [ "string.urlEncode", "$L2", "$P5"],
112
+ [ "string.concat", "$L1", "$L1", "&keyword=", "$L2"],
113
+ [ "if!=than", "$P6", nil, 3],
114
+ [ "callFunc", "getCategoriesString", "$P0", "$L2", "$P6"],
115
+ [ "string.urlEncode", "$L2", "$L2"],
116
+ [ "string.concat", "$L1", "$L1", "&types=", "$L2"],
117
+ [ "set", "$L0.url", "$L1"],
118
+ [ "http.requestCall", "$L2", "$L0"],
119
+ [ "callFunc", "checkHttpResponse", "$P0", "$L3", "$L2"],
120
+ [ "create", "$P1", "Array"],
121
+ [ "create", "$L4", "Number", 0],
122
+ [ "size", "$L5", "$L3.results"],
123
+ [ "if<than", "$L4", "$L5", 5],
124
+ [ "get", "$L6", "$L3.results", "$L4"],
125
+ [ "callFunc", "extractPOI", "$P0", "$L7", "$L6"],
126
+ [ "push", "$P1", "$L7"],
127
+ [ "math.add", "$L4", "$L4", 1],
128
+ [ "jumpRel", -6]
129
+
130
+ ],
131
+
132
+ "checkNull" => [
133
+ [ "if==than", "$P1", nil, 3],
134
+ [ "string.concat", "$L0", "$P2", " is not allowed to be null."],
135
+ [ "create", "$L1", "Error", "$L0", "IllegalArgument"],
136
+ [ "throwError", "$L1"]
137
+
138
+ ],
139
+
140
+ "checkLessThan" => [
141
+ [ "if<than", "$P1", "$P2", 3],
142
+ [ "string.concat", "$L0", "$P3", " is not allowed to be less than ", "$P2", "."],
143
+ [ "create", "$L1", "Error", "$L0", "IllegalArgument"],
144
+ [ "throwError", "$L1"]
145
+
146
+ ],
147
+
148
+ "checkGreaterThan" => [
149
+ [ "if>than", "$P1", "$P2", 3],
150
+ [ "string.concat", "$L0", "$P3", " is not allowed to be greater than ", "$P2", "."],
151
+ [ "create", "$L1", "Error", "$L0", "IllegalArgument"],
152
+ [ "throwError", "$L1"]
153
+
154
+ ],
155
+
156
+ "checkIsEmpty" => [
157
+ [ "size", "$L0", "$P2"],
158
+ [ "if==than", "$L0", 0, 3],
159
+ [ "string.concat", "$L0", "$P3", " is not allowed to be empty."],
160
+ [ "create", "$L1", "Error", "$L0", "IllegalArgument"],
161
+ [ "throwError", "$L1"]
162
+
163
+ ],
164
+
165
+ "checkHttpResponse" => [
166
+ [ "if>=than", "$P2.code", 400, 10],
167
+ [ "if==than", "$P2.code", 401, 2],
168
+ [ "create", "$L3", "Error", "Invalid credentials or access rights. Make sure that your application has read and write permission.", "Authentication"],
169
+ [ "throwError", "$L3"],
170
+ [ "if==than", "$P2.code", 503, 2],
171
+ [ "create", "$L3", "Error", "Service unavailable. Try again later.", "ServiceUnavailable"],
172
+ [ "throwError", "$L3"],
173
+ [ "json.parse", "$L0", "$P2.responseBody"],
174
+ [ "string.concat", "$L2", "$P2.code", " - ", "$L0.status"],
175
+ [ "create", "$L3", "Error", "$L2", "Http"],
176
+ [ "throwError", "$L3"],
177
+ [ "json.parse", "$L0", "$P2.responseBody"],
178
+ [ "if!=than", "$L0.status", "OK", 6],
179
+ [ "if!=than", "$L0.error_message", nil, 2],
180
+ [ "create", "$L3", "Error", "$L0.error_message", "Http"],
181
+ [ "throwError", "$L3"],
182
+ [ "if==than", "$L0.error_message", nil, 2],
183
+ [ "create", "$L3", "Error", "$L0.status", "Http"],
184
+ [ "throwError", "$L3"],
185
+ [ "set", "$P1", "$L0"]
186
+
187
+ ],
188
+
189
+ "getCategoriesString" => [
190
+ [ "create", "$P1", "String"],
191
+ [ "create", "$L0", "Number", 0],
192
+ [ "size", "$L1", "$P2"],
193
+ [ "if<than", "$L0", "$L1", 10],
194
+ [ "get", "$L2", "$P2", "$L0"],
195
+ [ "get", "$L2", "$P0.crToPlaces", "$L2"],
196
+ [ "if==than", "$L2", nil, 2],
197
+ [ "create", "$L3", "Error", "Unknown category.", "IllegalArgument"],
198
+ [ "throwError", "$L3"],
199
+ [ "if!=than", "$L0", 0, 1],
200
+ [ "string.concat", "$P1", "$P1", "|"],
201
+ [ "string.concat", "$P1", "$P1", "$L2"],
202
+ [ "math.add", "$L0", "$L0", 1],
203
+ [ "jumpRel", -11]
204
+
205
+ ],
206
+
207
+ "extractPOI" => [
208
+ [ "create", "$L0", "Array"],
209
+ [ "create", "$L1", "Number", 0],
210
+ [ "size", "$L2", "$P2.types"],
211
+ [ "if<than", "$L1", "$L2", 6],
212
+ [ "get", "$L3", "$P2.types", "$L1"],
213
+ [ "get", "$L4", "$P0.placesToCr", "$L3"],
214
+ [ "if!=than", "$L4", nil, 1],
215
+ [ "push", "$L0", "$L4"],
216
+ [ "math.add", "$L1", "$L1", 1],
217
+ [ "jumpRel", -7],
218
+ [ "create", "$L1", "Location"],
219
+ [ "set", "$L1.latitude", "$P2.geometry.location.lat"],
220
+ [ "set", "$L1.longitude", "$P2.geometry.location.lng"],
221
+ [ "if!=than", "$P2.photos", nil, 3],
222
+ [ "get", "$L2", "$P2.photos", 0],
223
+ [ "get", "$L3", "$L2.photo_reference"],
224
+ [ "string.concat", "$L4", "https://maps.googleapis.com/maps/api/place/photo?key=", "$P0.apiKey", "&photoreference=", "$L3", "&maxheight=", "$L2.height"],
225
+ [ "create", "$P1", "POI", "$L0", "$L4", "$L1", "$P2.name", nil]
226
+
227
+ ],
228
+
229
+ "addCategory" => [
230
+ [ "set", "$P0.crToPlaces", "$P2", "$P1"],
231
+ [ "set", "$P0.placesToCr", "$P1", "$P2"]
232
+
233
+ ]
234
+
235
+ }
236
+
237
+ def initialize(redirect_receiver, api_key)
238
+ @interpreter_storage = {}
239
+ @persistent_storage = [{}]
240
+ @instance_dependency_storage = {
241
+ redirect_receiver: redirect_receiver
242
+ }
243
+
244
+ ServiceCode::InitSelfTest.init_test('GooglePlaces')
245
+
246
+ @interpreter_storage['apiKey'] = api_key
247
+
248
+
249
+
250
+
251
+ # call init servicecode function
252
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
253
+ # check and call the initialization function if is available
254
+
255
+ ip.call_function_sync('init', @interpreter_storage) if (SERVICE_CODE['init'])
256
+ end
257
+
258
+ def check_for_error(error)
259
+ if (error)
260
+ ServiceCode::Statistics.add_error('GooglePlaces', '')
261
+ case (error.type)
262
+ when 'IllegalArgument'
263
+ raise Errors::IllegalArgumentError.new(error.message)
264
+ when 'Authentication'
265
+ raise Errors::AuthenticationError.new(error.message)
266
+ when 'NotFound'
267
+ raise Errors::NotFoundError.new(error.message)
268
+ when 'Http'
269
+ raise Errors::HttpError.new(error.message)
270
+ when 'ServiceUnavailable'
271
+ raise Errors::ServiceUnavailableError.new(error.message)
272
+ else
273
+ raise Errors::StandardError.new(error.message)
274
+ end
275
+ end
276
+ end
277
+
278
+
279
+ def get_nearby_pois(latitude, longitude, radius, search_term, categories)
280
+ ServiceCode::Statistics.add_call('GooglePlaces', 'get_nearby_pois')
281
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
282
+ ip.call_function('getNearbyPOIs', @interpreter_storage, nil, latitude, longitude, radius, search_term, categories)
283
+
284
+ check_for_error(ip.sandbox.thrown_error)
285
+ return nil || ip.get_parameter(1)
286
+ end
287
+
288
+
289
+ def save_as_string
290
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
291
+ return ip.save_as_string()
292
+ end
293
+
294
+ def load_as_string(saved_state)
295
+ sandbox = ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage)
296
+ ip = ServiceCode::Interpreter.new(sandbox)
297
+ ip.load_as_string(saved_state)
298
+ @persistent_storage = sandbox.persistent_storage
299
+ end
300
+
301
+ def resume_login(execution_state, callback)
302
+ sandbox = ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage)
303
+ sandbox.load_state_from_string(execution_state)
304
+ ip = ServiceCode::Interpreter.new(sandbox)
305
+ ip.resume_function('Authenticating:login', @interpreter_storage, callback)
306
+ end
307
+ end
308
+ end
309
+ end