Class: CloudRailSi::Services::Yahoo

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

Constant Summary collapse

SERVICE_CODE =
{
  
    "AdvancedRequestSupporter:advancedRequest" => [
      [ "create", "$L0", "Object"],
      [ "create", "$L0.url", "String"],
      [ "if!=than", "$P2.appendBaseUrl", 0, 1],
      [ "set", "$L0.url", "https://social.yahooapis.com/v1"],
      [ "string.concat", "$L0.url", "$L0.url", "$P2.url"],
      [ "set", "$L0.requestHeaders", "$P2.headers"],
      [ "set", "$L0.method", "$P2.method"],
      [ "set", "$L0.requestBody", "$P2.body"],
      [ "if!=than", "$P2.appendAuthorization", 0, 2],
      [ "callFunc", "checkAuthentication", "$P0"],
      [ "string.concat", "$L0.requestHeaders.Authorization", "Bearer ", "$S0.accessToken"],
      [ "http.requestCall", "$L1", "$L0"],
      [ "if!=than", "$P2.checkErrors", 0, 1],
      [ "callFunc", "validateResponse", "$P0", "$L1"],
      [ "create", "$P1", "AdvancedRequestResponse"],
      [ "set", "$P1.status", "$L1.code"],
      [ "set", "$P1.headers", "$L1.responseHeaders"],
      [ "set", "$P1.body", "$L1.responseBody"]
      
    ],
  
    "getYUIdentifier" => [
      [ "callFunc", "checkAuthentication", "$P0"],
      [ "if==than", "$P0.cachedObject", nil, 2],
      [ "callFunc", "makeHTTPRequest", "$P0"],
      [ "jumpRel", 1],
      [ "callFunc", "checkExpirationTime", "$P0"],
      [ "string.concat", "$P1", "yahoo-", "$P0.userID"]
      
    ],
  
    "getYUFullName" => [
      [ "callFunc", "checkAuthentication", "$P0"],
      [ "if==than", "$P0.cachedObject", nil, 2],
      [ "callFunc", "makeHTTPRequest", "$P0"],
      [ "jumpRel", 1],
      [ "callFunc", "checkExpirationTime", "$P0"],
      [ "if==than", "$P0.cachedObject.profile.givenName", nil, 2],
      [ "set", "$P1", nil],
      [ "jumpRel", 2],
      [ "string.concat", "$L0", "$P0.cachedObject.profile.givenName", " ", "$P0.cachedObject.profile.familyName"],
      [ "set", "$P1", "$L0"]
      
    ],
  
    "getYUEmail" => [
      [ "callFunc", "checkAuthentication", "$P0"],
      [ "if==than", "$P0.cachedObject", nil, 2],
      [ "callFunc", "makeHTTPRequest", "$P0"],
      [ "jumpRel", 1],
      [ "callFunc", "checkExpirationTime", "$P0"],
      [ "if==than", "$P0.cachedObject.profile.emails.0.handle", nil, 2],
      [ "set", "$P1", nil],
      [ "jumpRel", 1],
      [ "set", "$P1", "$P0.cachedObject.profile.emails.0.handle"]
      
    ],
  
    "getYUGender" => [
      [ "callFunc", "checkAuthentication", "$P0"],
      [ "if==than", "$P0.cachedObject", nil, 2],
      [ "callFunc", "makeHTTPRequest", "$P0"],
      [ "jumpRel", 1],
      [ "callFunc", "checkExpirationTime", "$P0"],
      [ "if==than", "$P0.cachedObject.profile.gender", nil, 2],
      [ "set", "$P1", nil],
      [ "jumpRel", 8],
      [ "if==than", "$P0.cachedObject.profile.gender", "M", 2],
      [ "set", "$P1", "male"],
      [ "jumpRel", 5],
      [ "if==than", "$P0.cachedObject.profile.gender", "F", 2],
      [ "set", "$P1", "female"],
      [ "jumpRel", 2],
      [ "if==than", "$P0.cachedObject.profile.gender", "Unspecified", 1],
      [ "set", "$P1", "other"]
      
    ],
  
    "getYUDescription" => [
      [ "set", "$P1", nil]
      
    ],
  
    "getYUDateOfBirth" => [
      [ "create", "$P1", "DateOfBirth"]
      
    ],
  
    "getYULocale" => [
      [ "callFunc", "checkAuthentication", "$P0"],
      [ "if==than", "$P0.cachedObject", nil, 2],
      [ "callFunc", "makeHTTPRequest", "$P0"],
      [ "jumpRel", 1],
      [ "callFunc", "checkExpirationTime", "$P0"],
      [ "if==than", "$P0.cachedObject.profile.lang", nil, 2],
      [ "set", "$P1", nil],
      [ "jumpRel", 1],
      [ "string.substring", "$P1", "$P0.cachedObject.profile.lang", 0, 2]
      
    ],
  
    "getYUPictureURL" => [
      [ "callFunc", "checkAuthentication", "$P0"],
      [ "if==than", "$P0.cachedObject", nil, 2],
      [ "callFunc", "makeHTTPRequest", "$P0"],
      [ "jumpRel", 1],
      [ "callFunc", "checkExpirationTime", "$P0"],
      [ "if==than", "$P0.cachedObject.profile.image.imageUrl", nil, 2],
      [ "set", "$P1", nil],
      [ "jumpRel", 1],
      [ "set", "$P1", "$P0.cachedObject.profile.image.imageUrl"]
      
    ],
  
    "loginYU" => [
      [ "callFunc", "authenticate", "$P0", "accessToken"]
      
    ],
  
    "logoutYU" => [
      [ "set", "$S0.accessToken", nil],
      [ "set", "$P0.userID", nil],
      [ "set", "$P0.cachedObject", nil]
      
    ],
  
    "getUserID" => [
      [ "create", "$L0", "Object"],
      [ "set", "$L0.method", "GET"],
      [ "set", "$L0.url", "https://social.yahooapis.com/v1/me/guid"],
      [ "create", "$L0.requestHeaders", "Object"],
      [ "string.concat", "$L0.requestHeaders.Authorization", "Bearer ", "$S0.accessToken"],
      [ "set", "$L0.requestHeaders.Accept", "application/json"],
      [ "create", "$L1", "Object"],
      [ "http.requestCall", "$L1", "$L0"],
      [ "callFunc", "validateResponse", "$P0", "$L1"],
      [ "create", "$L2", "Object"],
      [ "json.parse", "$L2", "$L1.responseBody"],
      [ "set", "$P0.userID", "$L2.guid.value"]
      
    ],
  
    "makeHTTPRequest" => [
      [ "callFunc", "getUserID", "$P0"],
      [ "create", "$L0", "Object"],
      [ "set", "$L0.method", "GET"],
      [ "string.concat", "$L0.url", "https://social.yahooapis.com/v1/user/", "$P0.userID", "/profile"],
      [ "create", "$L0.requestHeaders", "Object"],
      [ "string.concat", "$L0.requestHeaders.Authorization", "Bearer ", "$S0.accessToken"],
      [ "set", "$L0.requestHeaders.Accept", "application/json"],
      [ "create", "$L1", "Object"],
      [ "http.requestCall", "$L1", "$L0"],
      [ "callFunc", "validateResponse", "$P0", "$L1"],
      [ "json.parse", "$P0.cachedObject", "$L1.responseBody"],
      [ "create", "$L2", "Date"],
      [ "set", "$P0.readTime", "$L2.time"]
      
    ],
  
    "checkExpirationTime" => [
      [ "create", "$L0", "Date"],
      [ "math.multiply", "$L1", "$P0.readTime", -1],
      [ "math.add", "$L2", "$L0.time", "$L1"],
      [ "if>than", "$L2", 60000, 1],
      [ "callFunc", "makeHTTPRequest", "$P0"]
      
    ],
  
    "checkAuthentication" => [
      [ "create", "$L0", "Date"],
      [ "if==than", "$S0.accessToken", nil, 2],
      [ "callFunc", "authenticate", "$P0", "accessToken"],
      [ "return"],
      [ "create", "$L1", "Date"],
      [ "set", "$L1.time", "$S0.expireIn"],
      [ "if<than", "$L1", "$L0", 1],
      [ "callFunc", "authenticate", "$P0", "refreshToken"]
      
    ],
  
    "authenticate" => [
      [ "if==than", "$P1", "accessToken", 4],
      [ "string.concat", "$L0", "https://api.login.yahoo.com/oauth2/request_auth?client_id=", "$P0.clientId", "&redirect_uri=", "$P0.redirectUri", "&response_type=code&language=en-us"],
      [ "awaitCodeRedirect", "$L1", "$L0"],
      [ "string.concat", "$L2", "redirect_uri=", "$P0.redirectUri", "&code=", "$L1", "&grant_type=authorization_code"],
      [ "jumpRel", 1],
      [ "string.concat", "$L2", "redirect_uri=", "$P0.redirectUri", "&refresh_token=", "$S0.refreshToken", "&grant_type=refresh_token"],
      [ "stream.stringToStream", "$L3", "$L2"],
      [ "create", "$L4", "Object"],
      [ "set", "$L4", "application/x-www-form-urlencoded", "Content-Type"],
      [ "string.concat", "$L15", "$P0.clientId", ":", "$P0.clientSecret"],
      [ "string.base64encode", "$L15", "$L15"],
      [ "string.concat", "$L4.Authorization", "Basic ", "$L15"],
      [ "create", "$L5", "Object"],
      [ "set", "$L5.url", "https://api.login.yahoo.com/oauth2/get_token"],
      [ "set", "$L5.method", "POST"],
      [ "set", "$L5.requestBody", "$L3"],
      [ "set", "$L5.requestHeaders", "$L4"],
      [ "http.requestCall", "$L6", "$L5"],
      [ "callFunc", "validateResponse", "$P0", "$L6"],
      [ "create", "$L7", "String"],
      [ "stream.streamToString", "$L7", "$L6.responseBody"],
      [ "json.parse", "$L8", "$L7"],
      [ "set", "$S0.accessToken", "$L8.access_token"],
      [ "set", "$S0.refreshToken", "$L8.refresh_token"],
      [ "create", "$L10", "Date"],
      [ "math.multiply", "$L9", "$L8.expires_in", 1000],
      [ "math.add", "$L9", "$L9", "$L10.time", -60000],
      [ "set", "$S0.expireIn", "$L9"]
      
    ],
  
    "validateResponse" => [
      [ "if>=than", "$P1.code", 400, 10],
      [ "if==than", "$P1.code", 401, 2],
      [ "create", "$L3", "Error", "Invalid credentials or access rights. Make sure that your application has read and write permission.", "Authentication"],
      [ "throwError", "$L3"],
      [ "if==than", "$P1.code", 503, 2],
      [ "create", "$L3", "Error", "Service unavailable. Try again later.", "ServiceUnavailable"],
      [ "throwError", "$L3"],
      [ "stream.streamToString", "$L0", "$P1.responseBody"],
      [ "string.concat", "$L2", "$P1.code", " - ", "$L0"],
      [ "create", "$L3", "Error", "$L2", "Http"],
      [ "throwError", "$L3"]
      
    ]
  
}

Instance Method Summary collapse

Constructor Details

#initialize(redirect_receiver, client_id, client_secret, redirect_uri, state) ⇒ Yahoo

Returns a new instance of Yahoo.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/cloudrail_si/services/Yahoo.rb', line 243

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

    ServiceCode::InitSelfTest.init_test('Yahoo')

    @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

#advanced_request(specification) ⇒ Object



378
379
380
381
382
383
384
385
# File 'lib/cloudrail_si/services/Yahoo.rb', line 378

def advanced_request(specification)
  ServiceCode::Statistics.add_call('Yahoo', 'advanced_request')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('AdvancedRequestSupporter:advancedRequest', @interpreter_storage, nil, specification)

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

#check_for_error(error) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/cloudrail_si/services/Yahoo.rb', line 267

def check_for_error(error)
  if (error)
    ServiceCode::Statistics.add_error('Yahoo', '')
    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_date_of_birthObject



333
334
335
336
337
338
339
340
# File 'lib/cloudrail_si/services/Yahoo.rb', line 333

def get_date_of_birth()
  ServiceCode::Statistics.add_call('Yahoo', 'get_date_of_birth')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('getYUDateOfBirth', @interpreter_storage, nil)

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

#get_descriptionObject



324
325
326
327
328
329
330
331
# File 'lib/cloudrail_si/services/Yahoo.rb', line 324

def get_description()
  ServiceCode::Statistics.add_call('Yahoo', 'get_description')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('getYUDescription', @interpreter_storage, nil)

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

#get_emailObject



306
307
308
309
310
311
312
313
# File 'lib/cloudrail_si/services/Yahoo.rb', line 306

def get_email()
  ServiceCode::Statistics.add_call('Yahoo', 'get_email')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('getYUEmail', @interpreter_storage, nil)

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

#get_full_nameObject



297
298
299
300
301
302
303
304
# File 'lib/cloudrail_si/services/Yahoo.rb', line 297

def get_full_name()
  ServiceCode::Statistics.add_call('Yahoo', 'get_full_name')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('getYUFullName', @interpreter_storage, nil)

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

#get_genderObject



315
316
317
318
319
320
321
322
# File 'lib/cloudrail_si/services/Yahoo.rb', line 315

def get_gender()
  ServiceCode::Statistics.add_call('Yahoo', 'get_gender')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('getYUGender', @interpreter_storage, nil)

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

#get_identifierObject



288
289
290
291
292
293
294
295
# File 'lib/cloudrail_si/services/Yahoo.rb', line 288

def get_identifier()
  ServiceCode::Statistics.add_call('Yahoo', 'get_identifier')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('getYUIdentifier', @interpreter_storage, nil)

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

#get_localeObject



342
343
344
345
346
347
348
349
# File 'lib/cloudrail_si/services/Yahoo.rb', line 342

def get_locale()
  ServiceCode::Statistics.add_call('Yahoo', 'get_locale')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('getYULocale', @interpreter_storage, nil)

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

#get_picture_urlObject



351
352
353
354
355
356
357
358
# File 'lib/cloudrail_si/services/Yahoo.rb', line 351

def get_picture_url()
  ServiceCode::Statistics.add_call('Yahoo', 'get_picture_url')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('getYUPictureURL', @interpreter_storage, nil)

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

#load_as_string(saved_state) ⇒ Object



393
394
395
396
397
398
# File 'lib/cloudrail_si/services/Yahoo.rb', line 393

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



360
361
362
363
364
365
366
367
# File 'lib/cloudrail_si/services/Yahoo.rb', line 360

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

  check_for_error(ip.sandbox.thrown_error)
  return nil
end

#logoutObject



369
370
371
372
373
374
375
376
# File 'lib/cloudrail_si/services/Yahoo.rb', line 369

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

  check_for_error(ip.sandbox.thrown_error)
  return nil
end

#resume_login(execution_state, callback) ⇒ Object



400
401
402
403
404
405
# File 'lib/cloudrail_si/services/Yahoo.rb', line 400

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



388
389
390
391
# File 'lib/cloudrail_si/services/Yahoo.rb', line 388

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