Class: CloudRailSi::Services::FacebookPage

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudrail_si/services/FacebookPage.rb

Constant Summary collapse

SERVICE_CODE =
{
  
    "init" => [
      [ "set", "$P0.boundary", "Amgrmg43ghg3g39glv0k2ldk"]
      
    ],
  
    "Social:postUpdate" => [
      [ "if==than", "$P1", nil, 2],
      [ "create", "$L0", "Error", "The content is not allowed to be null.", "IllegalArgument"],
      [ "throwError", "$L0"],
      [ "callFunc", "checkAuthentication", "$P0"],
      [ "create", "$L3", "String"],
      [ "string.urlEncode", "$L3", "$P1"],
      [ "create", "$L0", "Object"],
      [ "set", "$L0.method", "POST"],
      [ "create", "$L1", "String"],
      [ "string.concat", "$L1", "https://graph.facebook.com/v2.8/", "$S0.pageId", "/feed?message=", "$L3"],
      [ "set", "$L0.url", "$L1"],
      [ "create", "$L0.requestHeaders", "Object"],
      [ "string.concat", "$L0.requestHeaders.Authorization", "Bearer ", "$S0.accessToken"],
      [ "create", "$L2", "Object"],
      [ "http.requestCall", "$L2", "$L0"],
      [ "callFunc", "validateResponse", "$P0", "$L2"]
      
    ],
  
    "Social:postImage" => [
      [ "if==than", "$P1", nil, 2],
      [ "create", "$L0", "Error", "The message is not allowed to be null.", "IllegalArgument"],
      [ "throwError", "$L0"],
      [ "if==than", "$P2", nil, 2],
      [ "create", "$L0", "Error", "The image is not allowed to be null.", "IllegalArgument"],
      [ "throwError", "$L0"],
      [ "callFunc", "checkAuthentication", "$P0"],
      [ "create", "$L0", "Object"],
      [ "set", "$L0.method", "POST"],
      [ "string.concat", "$L0.url", "https://graph.facebook.com/v2.8/", "$S0.pageId", "/photos"],
      [ "create", "$L0.requestHeaders", "Object"],
      [ "string.concat", "$L0.requestHeaders.Content-Type", "multipart/form-data; boundary=", "$P0.boundary"],
      [ "string.concat", "$L0.requestHeaders.Authorization", "Bearer ", "$S0.accessToken"],
      [ "string.concat", "$L1", "--", "$P0.boundary", "\r\n"],
      [ "string.concat", "$L1", "$L1", "Content-Disposition: form-data; name=\"caption\"\r\n\r\n"],
      [ "string.concat", "$L1", "$L1", "$P1", "\r\n"],
      [ "string.concat", "$L1", "$L1", "--", "$P0.boundary", "\r\n"],
      [ "string.concat", "$L1", "$L1", "Content-Disposition: form-data; name=\"source\"; filename=\"somename\"\r\n"],
      [ "string.concat", "$L1", "$L1", "Content-Transfer-Encoding: binary\r\n\r\n"],
      [ "stream.stringToStream", "$L1", "$L1"],
      [ "string.concat", "$L2", "\r\n--", "$P0.boundary", "--"],
      [ "stream.stringToStream", "$L2", "$L2"],
      [ "stream.makeJoinedStream", "$L0.requestBody", "$L1", "$P2", "$L2"],
      [ "http.requestCall", "$L4", "$L0"],
      [ "callFunc", "validateResponse", "$P0", "$L4"]
      
    ],
  
    "Social:postVideo" => [
      [ "if==than", "$P1", nil, 2],
      [ "create", "$L0", "Error", "The message is not allowed to be null.", "IllegalArgument"],
      [ "throwError", "$L0"],
      [ "if==than", "$P2", nil, 2],
      [ "create", "$L0", "Error", "The image is not allowed to be null.", "IllegalArgument"],
      [ "throwError", "$L0"],
      [ "callFunc", "checkAuthentication", "$P0"],
      [ "create", "$L0", "Object"],
      [ "set", "$L0.method", "POST"],
      [ "string.concat", "$L0.url", "https://graph-video.facebook.com/v2.8/", "$S0.pageId", "/videos"],
      [ "string.indexOf", "$L20", "$P4", "/"],
      [ "math.add", "$L20", "$L20", 1],
      [ "string.substring", "$L21", "$P4", "$L20"],
      [ "create", "$L0.requestHeaders", "Object"],
      [ "string.concat", "$L0.requestHeaders.Content-Type", "multipart/form-data; boundary=", "$P0.boundary"],
      [ "string.concat", "$L0.requestHeaders.Authorization", "Bearer ", "$S0.accessToken"],
      [ "string.concat", "$L1", "--", "$P0.boundary", "\r\n"],
      [ "string.concat", "$L1", "$L1", "Content-Disposition: form-data; name=\"description\"\r\n\r\n"],
      [ "string.concat", "$L1", "$L1", "$P1", "\r\n"],
      [ "string.concat", "$L1", "$L1", "--", "$P0.boundary", "\r\n"],
      [ "string.concat", "$L1", "$L1", "Content-Disposition: form-data; name=\"source\"; filename=\"somename.", "$L21", "\"\r\n"],
      [ "string.concat", "$L1", "$L1", "Content-Transfer-Encoding: binary\r\n\r\n"],
      [ "stream.stringToStream", "$L1", "$L1"],
      [ "string.concat", "$L2", "\r\n--", "$P0.boundary", "--"],
      [ "stream.stringToStream", "$L2", "$L2"],
      [ "stream.makeJoinedStream", "$L0.requestBody", "$L1", "$P2", "$L2"],
      [ "http.requestCall", "$L4", "$L0"],
      [ "callFunc", "validateResponse", "$P0", "$L4"]
      
    ],
  
    "Social:getConnections" => [
      
    ],
  
    "Authenticating:login" => [
      [ "callFunc", "checkAuthentication", "$P0"]
      
    ],
  
    "Authenticating:logout" => [
      [ "set", "$P0.userInfo", nil],
      [ "if!=than", "$S0.accessToken", nil, 10],
      [ "create", "$L0", "Object"],
      [ "set", "$L0.method", "DELETE"],
      [ "create", "$L1", "String"],
      [ "string.concat", "$L1", "https://graph.facebook.com/v2.8/me/permissions"],
      [ "set", "$L0.url", "$L1"],
      [ "create", "$L0.requestHeaders", "Object"],
      [ "string.concat", "$L0.requestHeaders.Authorization", "Bearer ", "$S0.accessToken"],
      [ "create", "$L2", "Object"],
      [ "http.requestCall", "$L2", "$L0"],
      [ "callFunc", "validateResponse", "$P0", "$L2"]
      
    ],
  
    "checkAuthentication" => [
      [ "if==than", "$S0.accessToken", nil, 1],
      [ "callFunc", "authenticate", "$P0"]
      
    ],
  
    "authenticate" => [
      [ "create", "$L0", "String"],
      [ "create", "$L1", "String"],
      [ "string.urlEncode", "$L3", "$P0.redirectUri"],
      [ "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"],
      [ "awaitCodeRedirect", "$L2", "$L0"],
      [ "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"],
      [ "create", "$L5", "Object"],
      [ "set", "$L5.url", "$L1"],
      [ "set", "$L5.method", "GET"],
      [ "http.requestCall", "$L6", "$L5"],
      [ "callFunc", "validateResponse", "$P0", "$L6"],
      [ "stream.streamToString", "$L7", "$L6.responseBody"],
      [ "json.parse", "$L8", "$L7"],
      [ "set", "$S0.accessToken", "$L8.access_token"],
      [ "create", "$L0", "Object"],
      [ "set", "$L0.method", "GET"],
      [ "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"],
      [ "http.requestCall", "$L1", "$L0"],
      [ "callFunc", "validateResponse", "$P0", "$L1"],
      [ "json.parse", "$L2", "$L1.responseBody"],
      [ "callFunc", "getPageCredentials", "$P0", "$L2.access_token"]
      
    ],
  
    "getPageCredentials" => [
      [ "create", "$L0", "Object"],
      [ "set", "$L0.method", "GET"],
      [ "string.concat", "$L0.url", "https://graph.facebook.com/v2.8/me/accounts", "?access_token=", "$P1"],
      [ "http.requestCall", "$L1", "$L0"],
      [ "callFunc", "validateResponse", "$P0", "$L1"],
      [ "json.parse", "$L2", "$L1.responseBody"],
      [ "set", "$L3", "$L2.data"],
      [ "size", "$L4", "$L3"],
      [ "create", "$L5", "Number", 0],
      [ "if<than", "$L5", "$L4", 7],
      [ "get", "$L6", "$L3", "$L5"],
      [ "if==than", "$L6.name", "$P0.pageName", 3],
      [ "set", "$S0.accessToken", "$L6.access_token"],
      [ "set", "$S0.pageId", "$L6.id"],
      [ "jumpRel", 2],
      [ "math.add", "$L5", "$L5", 1],
      [ "jumpRel", -8],
      [ "if==than", "$S0.accessToken", nil, 2],
      [ "create", "$L7", "Error", "The page was not found or the signed in user is not an admin of that page.", "IllegalArgument"],
      [ "throwError", "$L7"]
      
    ],
  
    "validateResponse" => [
      [ "if>=than", "$P1.code", 400, 3],
      [ "stream.streamToString", "$L2", "$P1.responseBody"],
      [ "create", "$L3", "Error", "$L2", "Http"],
      [ "throwError", "$L3"]
      
    ]
  
}

Instance Method Summary collapse

Constructor Details

#initialize(redirect_receiver, page_name, client_id, client_secret, redirect_uri, state) ⇒ FacebookPage

Returns a new instance of FacebookPage.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/cloudrail_si/services/FacebookPage.rb', line 189

def initialize(redirect_receiver, page_name, client_id, client_secret, redirect_uri, state)
    @interpreter_storage = {}
    @persistent_storage = [{}]
    @instance_dependency_storage = {
        redirect_receiver: redirect_receiver
    }

    ServiceCode::InitSelfTest.init_test('FacebookPage')

    @interpreter_storage['pageName'] = page_name
    @interpreter_storage['clientID'] = client_id
    @interpreter_storage['clientSecret'] = client_secret
    @interpreter_storage['redirectUri'] = redirect_uri
    @interpreter_storage['state'] = state
    

    

    # call init servicecode function
    ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
    # check and call the initialization function if is available

    ip.call_function_sync('init', @interpreter_storage) if (SERVICE_CODE['init'])
end

Instance Method Details

#check_for_error(error) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/cloudrail_si/services/FacebookPage.rb', line 214

def check_for_error(error)
  if (error)
    ServiceCode::Statistics.add_error('FacebookPage', '')
    case (error.type)
    when 'IllegalArgument'
      raise Errors::IllegalArgumentError.new(error.message)
    when 'Authentication'
      raise Errors::AuthenticationError.new(error.message)
    when 'NotFound'
      raise Errors::NotFoundError.new(error.message)
    when 'Http'
      raise Errors::HttpError.new(error.message)
    when 'ServiceUnavailable'
      raise Errors::ServiceUnavailableError.new(error.message)
    else
      raise Errors::StandardError.new(error.message)
    end
  end
end

#get_connectionsObject



262
263
264
265
266
267
268
269
# File 'lib/cloudrail_si/services/FacebookPage.rb', line 262

def get_connections()
  ServiceCode::Statistics.add_call('FacebookPage', 'get_connections')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('Social:getConnections', @interpreter_storage, nil)

  check_for_error(ip.sandbox.thrown_error)
  return nil || ip.get_parameter(1)
end

#load_as_string(saved_state) ⇒ Object



295
296
297
298
299
300
# File 'lib/cloudrail_si/services/FacebookPage.rb', line 295

def load_as_string(saved_state)
	sandbox = ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage)
	ip = ServiceCode::Interpreter.new(sandbox)
	ip.load_as_string(saved_state)
	@persistent_storage = sandbox.persistent_storage
end

#loginObject



271
272
273
274
275
276
277
278
# File 'lib/cloudrail_si/services/FacebookPage.rb', line 271

def ()
  ServiceCode::Statistics.add_call('FacebookPage', 'login')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('Authenticating:login', @interpreter_storage)

  check_for_error(ip.sandbox.thrown_error)
  return nil
end

#logoutObject



280
281
282
283
284
285
286
287
# File 'lib/cloudrail_si/services/FacebookPage.rb', line 280

def logout()
  ServiceCode::Statistics.add_call('FacebookPage', 'logout')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('Authenticating:logout', @interpreter_storage)

  check_for_error(ip.sandbox.thrown_error)
  return nil
end

#post_image(message, image) ⇒ Object



244
245
246
247
248
249
250
251
# File 'lib/cloudrail_si/services/FacebookPage.rb', line 244

def post_image(message, image)
  ServiceCode::Statistics.add_call('FacebookPage', 'post_image')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('Social:postImage', @interpreter_storage, message, image)

  check_for_error(ip.sandbox.thrown_error)
  return nil
end

#post_update(content) ⇒ Object



235
236
237
238
239
240
241
242
# File 'lib/cloudrail_si/services/FacebookPage.rb', line 235

def post_update(content)
  ServiceCode::Statistics.add_call('FacebookPage', 'post_update')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('Social:postUpdate', @interpreter_storage, content)

  check_for_error(ip.sandbox.thrown_error)
  return nil
end

#post_video(message, video, size, mime_type) ⇒ Object



253
254
255
256
257
258
259
260
# File 'lib/cloudrail_si/services/FacebookPage.rb', line 253

def post_video(message, video, size, mime_type)
  ServiceCode::Statistics.add_call('FacebookPage', 'post_video')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('Social:postVideo', @interpreter_storage, message, video, size, mime_type)

  check_for_error(ip.sandbox.thrown_error)
  return nil
end

#resume_login(execution_state, callback) ⇒ Object



302
303
304
305
306
307
# File 'lib/cloudrail_si/services/FacebookPage.rb', line 302

def (execution_state, callback)
  sandbox = ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage)
  sandbox.load_state_from_string(execution_state)
  ip = ServiceCode::Interpreter.new(sandbox)
  ip.resume_function('Authenticating:login', @interpreter_storage, callback)
end

#save_as_stringObject



290
291
292
293
# File 'lib/cloudrail_si/services/FacebookPage.rb', line 290

def save_as_string
	ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
	return ip.save_as_string()
end