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,310 @@
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 FacebookPage
10
+
11
+ SERVICE_CODE = {
12
+
13
+ "init" => [
14
+ [ "set", "$P0.boundary", "Amgrmg43ghg3g39glv0k2ldk"]
15
+
16
+ ],
17
+
18
+ "Social:postUpdate" => [
19
+ [ "if==than", "$P1", nil, 2],
20
+ [ "create", "$L0", "Error", "The content is not allowed to be null.", "IllegalArgument"],
21
+ [ "throwError", "$L0"],
22
+ [ "callFunc", "checkAuthentication", "$P0"],
23
+ [ "create", "$L3", "String"],
24
+ [ "string.urlEncode", "$L3", "$P1"],
25
+ [ "create", "$L0", "Object"],
26
+ [ "set", "$L0.method", "POST"],
27
+ [ "create", "$L1", "String"],
28
+ [ "string.concat", "$L1", "https://graph.facebook.com/v2.8/", "$S0.pageId", "/feed?message=", "$L3"],
29
+ [ "set", "$L0.url", "$L1"],
30
+ [ "create", "$L0.requestHeaders", "Object"],
31
+ [ "string.concat", "$L0.requestHeaders.Authorization", "Bearer ", "$S0.accessToken"],
32
+ [ "create", "$L2", "Object"],
33
+ [ "http.requestCall", "$L2", "$L0"],
34
+ [ "callFunc", "validateResponse", "$P0", "$L2"]
35
+
36
+ ],
37
+
38
+ "Social:postImage" => [
39
+ [ "if==than", "$P1", nil, 2],
40
+ [ "create", "$L0", "Error", "The message is not allowed to be null.", "IllegalArgument"],
41
+ [ "throwError", "$L0"],
42
+ [ "if==than", "$P2", nil, 2],
43
+ [ "create", "$L0", "Error", "The image is not allowed to be null.", "IllegalArgument"],
44
+ [ "throwError", "$L0"],
45
+ [ "callFunc", "checkAuthentication", "$P0"],
46
+ [ "create", "$L0", "Object"],
47
+ [ "set", "$L0.method", "POST"],
48
+ [ "string.concat", "$L0.url", "https://graph.facebook.com/v2.8/", "$S0.pageId", "/photos"],
49
+ [ "create", "$L0.requestHeaders", "Object"],
50
+ [ "string.concat", "$L0.requestHeaders.Content-Type", "multipart/form-data; boundary=", "$P0.boundary"],
51
+ [ "string.concat", "$L0.requestHeaders.Authorization", "Bearer ", "$S0.accessToken"],
52
+ [ "string.concat", "$L1", "--", "$P0.boundary", "\r\n"],
53
+ [ "string.concat", "$L1", "$L1", "Content-Disposition: form-data; name=\"caption\"\r\n\r\n"],
54
+ [ "string.concat", "$L1", "$L1", "$P1", "\r\n"],
55
+ [ "string.concat", "$L1", "$L1", "--", "$P0.boundary", "\r\n"],
56
+ [ "string.concat", "$L1", "$L1", "Content-Disposition: form-data; name=\"source\"; filename=\"somename\"\r\n"],
57
+ [ "string.concat", "$L1", "$L1", "Content-Transfer-Encoding: binary\r\n\r\n"],
58
+ [ "stream.stringToStream", "$L1", "$L1"],
59
+ [ "string.concat", "$L2", "\r\n--", "$P0.boundary", "--"],
60
+ [ "stream.stringToStream", "$L2", "$L2"],
61
+ [ "stream.makeJoinedStream", "$L0.requestBody", "$L1", "$P2", "$L2"],
62
+ [ "http.requestCall", "$L4", "$L0"],
63
+ [ "callFunc", "validateResponse", "$P0", "$L4"]
64
+
65
+ ],
66
+
67
+ "Social:postVideo" => [
68
+ [ "if==than", "$P1", nil, 2],
69
+ [ "create", "$L0", "Error", "The message is not allowed to be null.", "IllegalArgument"],
70
+ [ "throwError", "$L0"],
71
+ [ "if==than", "$P2", nil, 2],
72
+ [ "create", "$L0", "Error", "The image is not allowed to be null.", "IllegalArgument"],
73
+ [ "throwError", "$L0"],
74
+ [ "callFunc", "checkAuthentication", "$P0"],
75
+ [ "create", "$L0", "Object"],
76
+ [ "set", "$L0.method", "POST"],
77
+ [ "string.concat", "$L0.url", "https://graph-video.facebook.com/v2.8/", "$S0.pageId", "/videos"],
78
+ [ "string.indexOf", "$L20", "$P4", "/"],
79
+ [ "math.add", "$L20", "$L20", 1],
80
+ [ "string.substring", "$L21", "$P4", "$L20"],
81
+ [ "create", "$L0.requestHeaders", "Object"],
82
+ [ "string.concat", "$L0.requestHeaders.Content-Type", "multipart/form-data; boundary=", "$P0.boundary"],
83
+ [ "string.concat", "$L0.requestHeaders.Authorization", "Bearer ", "$S0.accessToken"],
84
+ [ "string.concat", "$L1", "--", "$P0.boundary", "\r\n"],
85
+ [ "string.concat", "$L1", "$L1", "Content-Disposition: form-data; name=\"description\"\r\n\r\n"],
86
+ [ "string.concat", "$L1", "$L1", "$P1", "\r\n"],
87
+ [ "string.concat", "$L1", "$L1", "--", "$P0.boundary", "\r\n"],
88
+ [ "string.concat", "$L1", "$L1", "Content-Disposition: form-data; name=\"source\"; filename=\"somename.", "$L21", "\"\r\n"],
89
+ [ "string.concat", "$L1", "$L1", "Content-Transfer-Encoding: binary\r\n\r\n"],
90
+ [ "stream.stringToStream", "$L1", "$L1"],
91
+ [ "string.concat", "$L2", "\r\n--", "$P0.boundary", "--"],
92
+ [ "stream.stringToStream", "$L2", "$L2"],
93
+ [ "stream.makeJoinedStream", "$L0.requestBody", "$L1", "$P2", "$L2"],
94
+ [ "http.requestCall", "$L4", "$L0"],
95
+ [ "callFunc", "validateResponse", "$P0", "$L4"]
96
+
97
+ ],
98
+
99
+ "Social:getConnections" => [
100
+
101
+ ],
102
+
103
+ "Authenticating:login" => [
104
+ [ "callFunc", "checkAuthentication", "$P0"]
105
+
106
+ ],
107
+
108
+ "Authenticating:logout" => [
109
+ [ "set", "$P0.userInfo", nil],
110
+ [ "if!=than", "$S0.accessToken", nil, 10],
111
+ [ "create", "$L0", "Object"],
112
+ [ "set", "$L0.method", "DELETE"],
113
+ [ "create", "$L1", "String"],
114
+ [ "string.concat", "$L1", "https://graph.facebook.com/v2.8/me/permissions"],
115
+ [ "set", "$L0.url", "$L1"],
116
+ [ "create", "$L0.requestHeaders", "Object"],
117
+ [ "string.concat", "$L0.requestHeaders.Authorization", "Bearer ", "$S0.accessToken"],
118
+ [ "create", "$L2", "Object"],
119
+ [ "http.requestCall", "$L2", "$L0"],
120
+ [ "callFunc", "validateResponse", "$P0", "$L2"]
121
+
122
+ ],
123
+
124
+ "checkAuthentication" => [
125
+ [ "if==than", "$S0.accessToken", nil, 1],
126
+ [ "callFunc", "authenticate", "$P0"]
127
+
128
+ ],
129
+
130
+ "authenticate" => [
131
+ [ "create", "$L0", "String"],
132
+ [ "create", "$L1", "String"],
133
+ [ "string.urlEncode", "$L3", "$P0.redirectUri"],
134
+ [ "string.concat", "$L0", "https://www.facebook.com/dialog/oauth?response_type=code&client_id=", "$P0.clientID", "&redirect_uri=", "$L3", "&state=", "$P0.state", "&scope=manage_pages%2Cpublish_pages%2Cpages_show_list"],
135
+ [ "awaitCodeRedirect", "$L2", "$L0"],
136
+ [ "string.concat", "$L1", "https://graph.facebook.com/v2.8/oauth/access_token?client_id=", "$P0.clientID", "&redirect_uri=", "$L3", "&client_secret=", "$P0.clientSecret", "&code=", "$L2"],
137
+ [ "create", "$L5", "Object"],
138
+ [ "set", "$L5.url", "$L1"],
139
+ [ "set", "$L5.method", "GET"],
140
+ [ "http.requestCall", "$L6", "$L5"],
141
+ [ "callFunc", "validateResponse", "$P0", "$L6"],
142
+ [ "stream.streamToString", "$L7", "$L6.responseBody"],
143
+ [ "json.parse", "$L8", "$L7"],
144
+ [ "set", "$S0.accessToken", "$L8.access_token"],
145
+ [ "create", "$L0", "Object"],
146
+ [ "set", "$L0.method", "GET"],
147
+ [ "string.concat", "$L0.url", "https://graph.facebook.com/v2.8/oauth/access_token?client_id=", "$P0.clientID", "&client_secret=", "$P0.clientSecret", "&grant_type=fb_exchange_token&fb_exchange_token=", "$S0.accessToken"],
148
+ [ "http.requestCall", "$L1", "$L0"],
149
+ [ "callFunc", "validateResponse", "$P0", "$L1"],
150
+ [ "json.parse", "$L2", "$L1.responseBody"],
151
+ [ "callFunc", "getPageCredentials", "$P0", "$L2.access_token"]
152
+
153
+ ],
154
+
155
+ "getPageCredentials" => [
156
+ [ "create", "$L0", "Object"],
157
+ [ "set", "$L0.method", "GET"],
158
+ [ "string.concat", "$L0.url", "https://graph.facebook.com/v2.8/me/accounts", "?access_token=", "$P1"],
159
+ [ "http.requestCall", "$L1", "$L0"],
160
+ [ "callFunc", "validateResponse", "$P0", "$L1"],
161
+ [ "json.parse", "$L2", "$L1.responseBody"],
162
+ [ "set", "$L3", "$L2.data"],
163
+ [ "size", "$L4", "$L3"],
164
+ [ "create", "$L5", "Number", 0],
165
+ [ "if<than", "$L5", "$L4", 7],
166
+ [ "get", "$L6", "$L3", "$L5"],
167
+ [ "if==than", "$L6.name", "$P0.pageName", 3],
168
+ [ "set", "$S0.accessToken", "$L6.access_token"],
169
+ [ "set", "$S0.pageId", "$L6.id"],
170
+ [ "jumpRel", 2],
171
+ [ "math.add", "$L5", "$L5", 1],
172
+ [ "jumpRel", -8],
173
+ [ "if==than", "$S0.accessToken", nil, 2],
174
+ [ "create", "$L7", "Error", "The page was not found or the signed in user is not an admin of that page.", "IllegalArgument"],
175
+ [ "throwError", "$L7"]
176
+
177
+ ],
178
+
179
+ "validateResponse" => [
180
+ [ "if>=than", "$P1.code", 400, 3],
181
+ [ "stream.streamToString", "$L2", "$P1.responseBody"],
182
+ [ "create", "$L3", "Error", "$L2", "Http"],
183
+ [ "throwError", "$L3"]
184
+
185
+ ]
186
+
187
+ }
188
+
189
+ def initialize(redirect_receiver, page_name, 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('FacebookPage')
197
+
198
+ @interpreter_storage['pageName'] = page_name
199
+ @interpreter_storage['clientID'] = client_id
200
+ @interpreter_storage['clientSecret'] = client_secret
201
+ @interpreter_storage['redirectUri'] = redirect_uri
202
+ @interpreter_storage['state'] = state
203
+
204
+
205
+
206
+
207
+ # call init servicecode function
208
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
209
+ # check and call the initialization function if is available
210
+
211
+ ip.call_function_sync('init', @interpreter_storage) if (SERVICE_CODE['init'])
212
+ end
213
+
214
+ def check_for_error(error)
215
+ if (error)
216
+ ServiceCode::Statistics.add_error('FacebookPage', '')
217
+ case (error.type)
218
+ when 'IllegalArgument'
219
+ raise Errors::IllegalArgumentError.new(error.message)
220
+ when 'Authentication'
221
+ raise Errors::AuthenticationError.new(error.message)
222
+ when 'NotFound'
223
+ raise Errors::NotFoundError.new(error.message)
224
+ when 'Http'
225
+ raise Errors::HttpError.new(error.message)
226
+ when 'ServiceUnavailable'
227
+ raise Errors::ServiceUnavailableError.new(error.message)
228
+ else
229
+ raise Errors::StandardError.new(error.message)
230
+ end
231
+ end
232
+ end
233
+
234
+
235
+ def post_update(content)
236
+ ServiceCode::Statistics.add_call('FacebookPage', 'post_update')
237
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
238
+ ip.call_function('Social:postUpdate', @interpreter_storage, content)
239
+
240
+ check_for_error(ip.sandbox.thrown_error)
241
+ return nil
242
+ end
243
+
244
+ def post_image(message, image)
245
+ ServiceCode::Statistics.add_call('FacebookPage', 'post_image')
246
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
247
+ ip.call_function('Social:postImage', @interpreter_storage, message, image)
248
+
249
+ check_for_error(ip.sandbox.thrown_error)
250
+ return nil
251
+ end
252
+
253
+ def post_video(message, video, size, mime_type)
254
+ ServiceCode::Statistics.add_call('FacebookPage', 'post_video')
255
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
256
+ ip.call_function('Social:postVideo', @interpreter_storage, message, video, size, mime_type)
257
+
258
+ check_for_error(ip.sandbox.thrown_error)
259
+ return nil
260
+ end
261
+
262
+ def get_connections()
263
+ ServiceCode::Statistics.add_call('FacebookPage', 'get_connections')
264
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
265
+ ip.call_function('Social:getConnections', @interpreter_storage, nil)
266
+
267
+ check_for_error(ip.sandbox.thrown_error)
268
+ return nil || ip.get_parameter(1)
269
+ end
270
+
271
+ def login()
272
+ ServiceCode::Statistics.add_call('FacebookPage', 'login')
273
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
274
+ ip.call_function('Authenticating:login', @interpreter_storage)
275
+
276
+ check_for_error(ip.sandbox.thrown_error)
277
+ return nil
278
+ end
279
+
280
+ def logout()
281
+ ServiceCode::Statistics.add_call('FacebookPage', 'logout')
282
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
283
+ ip.call_function('Authenticating:logout', @interpreter_storage)
284
+
285
+ check_for_error(ip.sandbox.thrown_error)
286
+ return nil
287
+ end
288
+
289
+
290
+ def save_as_string
291
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
292
+ return ip.save_as_string()
293
+ end
294
+
295
+ def load_as_string(saved_state)
296
+ sandbox = ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage)
297
+ ip = ServiceCode::Interpreter.new(sandbox)
298
+ ip.load_as_string(saved_state)
299
+ @persistent_storage = sandbox.persistent_storage
300
+ end
301
+
302
+ def resume_login(execution_state, callback)
303
+ sandbox = ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage)
304
+ sandbox.load_state_from_string(execution_state)
305
+ ip = ServiceCode::Interpreter.new(sandbox)
306
+ ip.resume_function('Authenticating:login', @interpreter_storage, callback)
307
+ end
308
+ end
309
+ end
310
+ end
@@ -0,0 +1,314 @@
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 Foursquare
10
+
11
+ SERVICE_CODE = {
12
+
13
+ "init" => [
14
+ [ "create", "$P0.crToFoursquare", "Object"],
15
+ [ "create", "$P0.foursquareToCr", "Object"],
16
+ [ "callFunc", "addCategory", "$P0", "airport", "4bf58dd8d48988d1ed931735"],
17
+ [ "callFunc", "addCategory", "$P0", "amusement_park", "4bf58dd8d48988d182941735"],
18
+ [ "callFunc", "addCategory", "$P0", "aquarium", "4fceea171983d5d06c3e9823"],
19
+ [ "callFunc", "addCategory", "$P0", "art_gallery", "4bf58dd8d48988d1e2931735"],
20
+ [ "callFunc", "addCategory", "$P0", "bakery", "4bf58dd8d48988d16a941735 "],
21
+ [ "callFunc", "addCategory", "$P0", "bank", "4bf58dd8d48988d10a951735"],
22
+ [ "callFunc", "addCategory", "$P0", "bar", "4bf58dd8d48988d116941735"],
23
+ [ "callFunc", "addCategory", "$P0", "beauty_salon", "54541900498ea6ccd0202697"],
24
+ [ "callFunc", "addCategory", "$P0", "bicycle_store", "4bf58dd8d48988d115951735"],
25
+ [ "callFunc", "addCategory", "$P0", "book_store", "4bf58dd8d48988d114951735"],
26
+ [ "callFunc", "addCategory", "$P0", "bowling_alley", "4bf58dd8d48988d1e4931735"],
27
+ [ "callFunc", "addCategory", "$P0", "bus_station", "4bf58dd8d48988d1fe931735"],
28
+ [ "callFunc", "addCategory", "$P0", "cafe", "4bf58dd8d48988d16d941735"],
29
+ [ "callFunc", "addCategory", "$P0", "car_dealer", "4eb1c1623b7b52c0e1adc2ec"],
30
+ [ "callFunc", "addCategory", "$P0", "car_rental", "4bf58dd8d48988d1ef941735"],
31
+ [ "callFunc", "addCategory", "$P0", "car_repair", "56aa371be4b08b9a8d5734d3"],
32
+ [ "callFunc", "addCategory", "$P0", "car_wash", "4f04ae1f2fb6e1c99f3db0ba"],
33
+ [ "callFunc", "addCategory", "$P0", "casino", "4bf58dd8d48988d17c941735"],
34
+ [ "callFunc", "addCategory", "$P0", "cemetery", "4bf58dd8d48988d15c941735"],
35
+ [ "callFunc", "addCategory", "$P0", "church", "4bf58dd8d48988d132941735"],
36
+ [ "callFunc", "addCategory", "$P0", "clothing_store", "4bf58dd8d48988d103951735"],
37
+ [ "callFunc", "addCategory", "$P0", "convenience_store", "4d954b0ea243a5684a65b473"],
38
+ [ "callFunc", "addCategory", "$P0", "courthouse", "4bf58dd8d48988d12b941735"],
39
+ [ "callFunc", "addCategory", "$P0", "dentist", "4bf58dd8d48988d178941735"],
40
+ [ "callFunc", "addCategory", "$P0", "department_store", "4bf58dd8d48988d1f6941735"],
41
+ [ "callFunc", "addCategory", "$P0", "doctor", "4bf58dd8d48988d177941735"],
42
+ [ "callFunc", "addCategory", "$P0", "electronics_store", "4bf58dd8d48988d122951735"],
43
+ [ "callFunc", "addCategory", "$P0", "embassy", "4bf58dd8d48988d12c951735"],
44
+ [ "callFunc", "addCategory", "$P0", "finance", "503287a291d4c4b30a586d65"],
45
+ [ "callFunc", "addCategory", "$P0", "fire_station", "4bf58dd8d48988d12c941735"],
46
+ [ "callFunc", "addCategory", "$P0", "florist", "4bf58dd8d48988d11b951735"],
47
+ [ "callFunc", "addCategory", "$P0", "food", "4d4b7105d754a06374d81259"],
48
+ [ "callFunc", "addCategory", "$P0", "funeral_home", "4f4534884b9074f6e4fb0174"],
49
+ [ "callFunc", "addCategory", "$P0", "furniture_store", "4bf58dd8d48988d1f8941735"],
50
+ [ "callFunc", "addCategory", "$P0", "gas_station", "4bf58dd8d48988d113951735"],
51
+ [ "callFunc", "addCategory", "$P0", "grocery_or_supermarket", "4bf58dd8d48988d118951735"],
52
+ [ "callFunc", "addCategory", "$P0", "gym", "4bf58dd8d48988d175941735"],
53
+ [ "callFunc", "addCategory", "$P0", "hardware_store", "4bf58dd8d48988d112951735"],
54
+ [ "callFunc", "addCategory", "$P0", "health", "54541900498ea6ccd0202697"],
55
+ [ "callFunc", "addCategory", "$P0", "hindu_temple", "52e81612bcbc57f1066b7a3f"],
56
+ [ "callFunc", "addCategory", "$P0", "hospital", "4bf58dd8d48988d196941735"],
57
+ [ "callFunc", "addCategory", "$P0", "jewelry_store", "4bf58dd8d48988d111951735"],
58
+ [ "callFunc", "addCategory", "$P0", "laundry", "4bf58dd8d48988d1fc941735"],
59
+ [ "callFunc", "addCategory", "$P0", "lawyer", "52f2ab2ebcbc57f1066b8b3f"],
60
+ [ "callFunc", "addCategory", "$P0", "library", "4bf58dd8d48988d12f941735"],
61
+ [ "callFunc", "addCategory", "$P0", "locksmith", "52f2ab2ebcbc57f1066b8b1e"],
62
+ [ "callFunc", "addCategory", "$P0", "mosque", "4bf58dd8d48988d138941735"],
63
+ [ "callFunc", "addCategory", "$P0", "movie_theater", "4bf58dd8d48988d17f941735"],
64
+ [ "callFunc", "addCategory", "$P0", "museum", "4bf58dd8d48988d181941735"],
65
+ [ "callFunc", "addCategory", "$P0", "night_club", "4bf58dd8d48988d11f941735"],
66
+ [ "callFunc", "addCategory", "$P0", "parks", "4bf58dd8d48988d163941735"],
67
+ [ "callFunc", "addCategory", "$P0", "parking", "4c38df4de52ce0d596b336e1"],
68
+ [ "callFunc", "addCategory", "$P0", "pet_store", "4bf58dd8d48988d100951735"],
69
+ [ "callFunc", "addCategory", "$P0", "pharmacy", "4bf58dd8d48988d10f951735"],
70
+ [ "callFunc", "addCategory", "$P0", "physiotherapist", "5744ccdfe4b0c0459246b4af"],
71
+ [ "callFunc", "addCategory", "$P0", "police", "4bf58dd8d48988d12e941735"],
72
+ [ "callFunc", "addCategory", "$P0", "post_office", "4bf58dd8d48988d172941735"],
73
+ [ "callFunc", "addCategory", "$P0", "real_estate_agency", "5032885091d4c4b30a586d66"],
74
+ [ "callFunc", "addCategory", "$P0", "restaurant", "4d4b7105d754a06374d81259"],
75
+ [ "callFunc", "addCategory", "$P0", "rv_park", "52f2ab2ebcbc57f1066b8b53"],
76
+ [ "callFunc", "addCategory", "$P0", "school", "4bf58dd8d48988d13b941735"],
77
+ [ "callFunc", "addCategory", "$P0", "shoe_store", "4bf58dd8d48988d107951735"],
78
+ [ "callFunc", "addCategory", "$P0", "shopping_mall", "4bf58dd8d48988d1fd941735"],
79
+ [ "callFunc", "addCategory", "$P0", "spa", "4bf58dd8d48988d1ed941735"],
80
+ [ "callFunc", "addCategory", "$P0", "stadium", "4bf58dd8d48988d184941735"],
81
+ [ "callFunc", "addCategory", "$P0", "synagogue", "4bf58dd8d48988d139941735"],
82
+ [ "callFunc", "addCategory", "$P0", "taxi_stand", "53fca564498e1a175f32528b"],
83
+ [ "callFunc", "addCategory", "$P0", "train_station", "4bf58dd8d48988d129951735"],
84
+ [ "callFunc", "addCategory", "$P0", "travel_agency", "4f04b08c2fb6e1c99f3db0bd"],
85
+ [ "callFunc", "addCategory", "$P0", "university", "4d4b7105d754a06372d81259"],
86
+ [ "callFunc", "addCategory", "$P0", "veterinary_care", "4d954af4a243a5684765b473"],
87
+ [ "callFunc", "addCategory", "$P0", "zoo", "4bf58dd8d48988d17b941735"]
88
+
89
+ ],
90
+
91
+ "getNearbyPOIs" => [
92
+ [ "callFunc", "checkNull", "$P0", "$P2", "Latitude"],
93
+ [ "callFunc", "checkNull", "$P0", "$P3", "Longitude"],
94
+ [ "callFunc", "checkNull", "$P0", "$P4", "Radius"],
95
+ [ "callFunc", "checkLessThan", "$P0", "$P2", -90, "Latitude"],
96
+ [ "callFunc", "checkLessThan", "$P0", "$P3", -180, "Longitude"],
97
+ [ "callFunc", "checkLessThan", "$P0", "$P4", 0, "Radius"],
98
+ [ "callFunc", "checkGreaterThan", "$P0", "$P2", 90, "Latitude"],
99
+ [ "callFunc", "checkGreaterThan", "$P0", "$P3", 180, "Longitude"],
100
+ [ "callFunc", "checkGreaterThan", "$P0", "$P4", 40000, "Radius"],
101
+ [ "if!=than", "$P6", nil, 1],
102
+ [ "callFunc", "checkIsEmpty", "$P0", "$P6", "Categories"],
103
+ [ "create", "$L0", "Object"],
104
+ [ "set", "$L0.method", "GET"],
105
+ [ "set", "$L0.url", "https://api.foursquare.com/v2/venues/search?v=20160614&m=foursquare"],
106
+ [ "string.concat", "$L1", "$P2", ",", "$P3"],
107
+ [ "string.urlEncode", "$L1", "$L1"],
108
+ [ "string.concat", "$L0.url", "$L0.url", "&ll=", "$L1"],
109
+ [ "string.concat", "$L0.url", "$L0.url", "&radius=", "$P4"],
110
+ [ "if!=than", "$P5", nil, 2],
111
+ [ "string.urlEncode", "$L1", "$P5"],
112
+ [ "string.concat", "$L0.url", "$L0.url", "&query=", "$L1"],
113
+ [ "if!=than", "$P6", nil, 3],
114
+ [ "callFunc", "getCategoriesString", "$P0", "$L2", "$P6"],
115
+ [ "string.urlEncode", "$L2", "$L2"],
116
+ [ "string.concat", "$L0.url", "$L0.url", "&categoryId=", "$L2"],
117
+ [ "string.concat", "$L0.url", "$L0.url", "&client_id=", "$P0.clientID", "&client_secret=", "$P0.clientSecret"],
118
+ [ "http.requestCall", "$L2", "$L0"],
119
+ [ "callFunc", "checkHttpResponse", "$P0", "$L2"],
120
+ [ "json.parse", "$L3", "$L2.responseBody"],
121
+ [ "set", "$L3", "$L3.response.venues"],
122
+ [ "create", "$P1", "Array"],
123
+ [ "create", "$L4", "Number", 0],
124
+ [ "size", "$L5", "$L3"],
125
+ [ "if<than", "$L4", "$L5", 5],
126
+ [ "get", "$L6", "$L3", "$L4"],
127
+ [ "callFunc", "extractPOI", "$P0", "$L7", "$L6"],
128
+ [ "push", "$P1", "$L7"],
129
+ [ "math.add", "$L4", "$L4", 1],
130
+ [ "jumpRel", -6]
131
+
132
+ ],
133
+
134
+ "checkNull" => [
135
+ [ "if==than", "$P1", nil, 3],
136
+ [ "string.concat", "$L0", "$P2", " is not allowed to be null."],
137
+ [ "create", "$L1", "Error", "$L0", "IllegalArgument"],
138
+ [ "throwError", "$L1"]
139
+
140
+ ],
141
+
142
+ "checkLessThan" => [
143
+ [ "if<than", "$P1", "$P2", 3],
144
+ [ "string.concat", "$L0", "$P3", " is not allowed to be less than ", "$P2", "."],
145
+ [ "create", "$L1", "Error", "$L0", "IllegalArgument"],
146
+ [ "throwError", "$L1"]
147
+
148
+ ],
149
+
150
+ "checkGreaterThan" => [
151
+ [ "if>than", "$P1", "$P2", 3],
152
+ [ "string.concat", "$L0", "$P3", " is not allowed to be greater than ", "$P2", "."],
153
+ [ "create", "$L1", "Error", "$L0", "IllegalArgument"],
154
+ [ "throwError", "$L1"]
155
+
156
+ ],
157
+
158
+ "checkIsEmpty" => [
159
+ [ "size", "$L0", "$P2"],
160
+ [ "if==than", "$L0", 0, 3],
161
+ [ "string.concat", "$L0", "$P3", " is not allowed to be empty."],
162
+ [ "create", "$L1", "Error", "$L0", "IllegalArgument"],
163
+ [ "throwError", "$L1"]
164
+
165
+ ],
166
+
167
+ "checkHttpResponse" => [
168
+ [ "if>=than", "$P1.code", 400, 9],
169
+ [ "if==than", "$P1.code", 401, 2],
170
+ [ "create", "$L1", "Error", "Invalid credentials or access rights.", "Authentication"],
171
+ [ "throwError", "$L1"],
172
+ [ "if==than", "$P1.code", 503, 2],
173
+ [ "create", "$L1", "Error", "Service unavailable. Try again later.", "ServiceUnavailable"],
174
+ [ "throwError", "$L1"],
175
+ [ "json.parse", "$L0", "$P1.responseBody"],
176
+ [ "create", "$L1", "Error", "$L0.meta.errorDetail", "Http"],
177
+ [ "throwError", "$L1"]
178
+
179
+ ],
180
+
181
+ "getCategoriesString" => [
182
+ [ "create", "$P1", "String"],
183
+ [ "create", "$L0", "Number", 0],
184
+ [ "size", "$L1", "$P2"],
185
+ [ "if<than", "$L0", "$L1", 10],
186
+ [ "get", "$L2", "$P2", "$L0"],
187
+ [ "get", "$L2", "$P0.crToFoursquare", "$L2"],
188
+ [ "if==than", "$L2", nil, 2],
189
+ [ "create", "$L3", "Error", "Unknown category.", "IllegalArgument"],
190
+ [ "throwError", "$L3"],
191
+ [ "if!=than", "$L0", 0, 1],
192
+ [ "string.concat", "$P1", "$P1", ","],
193
+ [ "string.concat", "$P1", "$P1", "$L2"],
194
+ [ "math.add", "$L0", "$L0", 1],
195
+ [ "jumpRel", -11]
196
+
197
+ ],
198
+
199
+ "extractPOI" => [
200
+ [ "create", "$L0", "Array"],
201
+ [ "create", "$L1", "Number", 0],
202
+ [ "size", "$L2", "$P2.categories"],
203
+ [ "if<than", "$L1", "$L2", 7],
204
+ [ "get", "$L3", "$P2.categories", "$L1"],
205
+ [ "set", "$L3", "$L3.id"],
206
+ [ "get", "$L4", "$P0.foursquareToCr", "$L3"],
207
+ [ "if!=than", "$L4", nil, 1],
208
+ [ "push", "$L0", "$L4"],
209
+ [ "math.add", "$L1", "$L1", 1],
210
+ [ "jumpRel", -8],
211
+ [ "create", "$L1", "Location"],
212
+ [ "set", "$L1.latitude", "$P2.location.lat"],
213
+ [ "set", "$L1.longitude", "$P2.location.lng"],
214
+ [ "create", "$L2", "Object"],
215
+ [ "set", "$L2.method", "GET"],
216
+ [ "string.concat", "$L2.url", "https://api.foursquare.com/v2/venues/", "$P2.id", "/photos"],
217
+ [ "string.concat", "$L2.url", "$L2.url", "?client_id=", "$P0.clientID", "&client_secret=", "$P0.clientSecret"],
218
+ [ "string.concat", "$L2.url", "$L2.url", "&limit=1&v=20160614&m=foursquare"],
219
+ [ "http.requestCall", "$L3", "$L2"],
220
+ [ "callFunc", "checkHttpResponse", "$P0", "$L3"],
221
+ [ "json.parse", "$L4", "$L3.responseBody"],
222
+ [ "get", "$L5", "$L4.response.photos.items"],
223
+ [ "size", "$L6", "$L5"],
224
+ [ "if==than", "$L6", 0, 2],
225
+ [ "set", "$L6", nil],
226
+ [ "jumpRel", 2],
227
+ [ "get", "$L5", "$L5", 0],
228
+ [ "string.concat", "$L6", "$L5.prefix", "original", "$L5.suffix"],
229
+ [ "create", "$P1", "POI", "$L0", "$L6", "$L1", "$P2.name", "$P2.contact.phone"]
230
+
231
+ ],
232
+
233
+ "addCategory" => [
234
+ [ "set", "$P0.crToFoursquare", "$P2", "$P1"],
235
+ [ "set", "$P0.foursquareToCr", "$P1", "$P2"]
236
+
237
+ ]
238
+
239
+ }
240
+
241
+ def initialize(redirect_receiver, client_id, client_secret)
242
+ @interpreter_storage = {}
243
+ @persistent_storage = [{}]
244
+ @instance_dependency_storage = {
245
+ redirect_receiver: redirect_receiver
246
+ }
247
+
248
+ ServiceCode::InitSelfTest.init_test('Foursquare')
249
+
250
+ @interpreter_storage['clientID'] = client_id
251
+ @interpreter_storage['clientSecret'] = client_secret
252
+
253
+
254
+
255
+
256
+ # call init servicecode function
257
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
258
+ # check and call the initialization function if is available
259
+
260
+ ip.call_function_sync('init', @interpreter_storage) if (SERVICE_CODE['init'])
261
+ end
262
+
263
+ def check_for_error(error)
264
+ if (error)
265
+ ServiceCode::Statistics.add_error('Foursquare', '')
266
+ case (error.type)
267
+ when 'IllegalArgument'
268
+ raise Errors::IllegalArgumentError.new(error.message)
269
+ when 'Authentication'
270
+ raise Errors::AuthenticationError.new(error.message)
271
+ when 'NotFound'
272
+ raise Errors::NotFoundError.new(error.message)
273
+ when 'Http'
274
+ raise Errors::HttpError.new(error.message)
275
+ when 'ServiceUnavailable'
276
+ raise Errors::ServiceUnavailableError.new(error.message)
277
+ else
278
+ raise Errors::StandardError.new(error.message)
279
+ end
280
+ end
281
+ end
282
+
283
+
284
+ def get_nearby_pois(latitude, longitude, radius, search_term, categories)
285
+ ServiceCode::Statistics.add_call('Foursquare', 'get_nearby_pois')
286
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
287
+ ip.call_function('getNearbyPOIs', @interpreter_storage, nil, latitude, longitude, radius, search_term, categories)
288
+
289
+ check_for_error(ip.sandbox.thrown_error)
290
+ return nil || ip.get_parameter(1)
291
+ end
292
+
293
+
294
+ def save_as_string
295
+ ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
296
+ return ip.save_as_string()
297
+ end
298
+
299
+ def load_as_string(saved_state)
300
+ sandbox = ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage)
301
+ ip = ServiceCode::Interpreter.new(sandbox)
302
+ ip.load_as_string(saved_state)
303
+ @persistent_storage = sandbox.persistent_storage
304
+ end
305
+
306
+ def resume_login(execution_state, callback)
307
+ sandbox = ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage)
308
+ sandbox.load_state_from_string(execution_state)
309
+ ip = ServiceCode::Interpreter.new(sandbox)
310
+ ip.resume_function('Authenticating:login', @interpreter_storage, callback)
311
+ end
312
+ end
313
+ end
314
+ end