Class: CloudRailSi::Services::GooglePlaces

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

Constant Summary collapse

SERVICE_CODE =
{
  
    "init" => [
      [ "create", "$P0.crToPlaces", "Object"],
      [ "create", "$P0.placesToCr", "Object"],
      [ "callFunc", "addCategory", "$P0", "airport", "airport"],
      [ "callFunc", "addCategory", "$P0", "amusement_park", "amusement_park"],
      [ "callFunc", "addCategory", "$P0", "aquarium", "aquarium"],
      [ "callFunc", "addCategory", "$P0", "art_gallery", "art_gallery"],
      [ "callFunc", "addCategory", "$P0", "bakery", "bakery"],
      [ "callFunc", "addCategory", "$P0", "bank", "bank"],
      [ "callFunc", "addCategory", "$P0", "bar", "bar"],
      [ "callFunc", "addCategory", "$P0", "beauty_salon", "beauty_salon"],
      [ "callFunc", "addCategory", "$P0", "bicycle_store", "bicycle_store"],
      [ "callFunc", "addCategory", "$P0", "book_store", "book_store"],
      [ "callFunc", "addCategory", "$P0", "bowling_alley", "bowling_alley"],
      [ "callFunc", "addCategory", "$P0", "bus_station", "bus_station"],
      [ "callFunc", "addCategory", "$P0", "cafe", "cafe"],
      [ "callFunc", "addCategory", "$P0", "car_dealer", "car_dealer"],
      [ "callFunc", "addCategory", "$P0", "car_rental", "car_rental"],
      [ "callFunc", "addCategory", "$P0", "car_wash", "car_wash"],
      [ "callFunc", "addCategory", "$P0", "casino", "casino"],
      [ "callFunc", "addCategory", "$P0", "cemetery", "cemetery"],
      [ "callFunc", "addCategory", "$P0", "church", "church"],
      [ "callFunc", "addCategory", "$P0", "clothing_store", "clothing_store"],
      [ "callFunc", "addCategory", "$P0", "convenience_store", "convenience_store"],
      [ "callFunc", "addCategory", "$P0", "courthouse", "courthouse"],
      [ "callFunc", "addCategory", "$P0", "dentist", "dentist"],
      [ "callFunc", "addCategory", "$P0", "department_store", "department_store"],
      [ "callFunc", "addCategory", "$P0", "doctor", "doctor"],
      [ "callFunc", "addCategory", "$P0", "electronics_store", "electronics_store"],
      [ "callFunc", "addCategory", "$P0", "embassy", "embassy"],
      [ "callFunc", "addCategory", "$P0", "finance", "finance"],
      [ "callFunc", "addCategory", "$P0", "fire_station", "fire_station"],
      [ "callFunc", "addCategory", "$P0", "florist", "florist"],
      [ "callFunc", "addCategory", "$P0", "food", "food"],
      [ "callFunc", "addCategory", "$P0", "funeral_home", "funeral_home"],
      [ "callFunc", "addCategory", "$P0", "furniture_store", "furniture_store"],
      [ "callFunc", "addCategory", "$P0", "gas_station", "gas_station"],
      [ "callFunc", "addCategory", "$P0", "grocery_or_supermarket", "grocery_or_supermarket"],
      [ "callFunc", "addCategory", "$P0", "gym", "gym"],
      [ "callFunc", "addCategory", "$P0", "hardware_store", "hardware_store"],
      [ "callFunc", "addCategory", "$P0", "health", "health"],
      [ "callFunc", "addCategory", "$P0", "hindu_temple", "hindu_temple"],
      [ "callFunc", "addCategory", "$P0", "hospital", "hospital"],
      [ "callFunc", "addCategory", "$P0", "jewelry_store", "jewelry_store"],
      [ "callFunc", "addCategory", "$P0", "laundry", "laundry"],
      [ "callFunc", "addCategory", "$P0", "lawyer", "lawyer"],
      [ "callFunc", "addCategory", "$P0", "library", "library"],
      [ "callFunc", "addCategory", "$P0", "locksmith", "locksmith"],
      [ "callFunc", "addCategory", "$P0", "mosque", "mosque"],
      [ "callFunc", "addCategory", "$P0", "movie_theater", "movie_theater"],
      [ "callFunc", "addCategory", "$P0", "museum", "museum"],
      [ "callFunc", "addCategory", "$P0", "night_club", "night_club"],
      [ "callFunc", "addCategory", "$P0", "parks", "parks"],
      [ "callFunc", "addCategory", "$P0", "parking", "parking"],
      [ "callFunc", "addCategory", "$P0", "pet_store", "pet_store"],
      [ "callFunc", "addCategory", "$P0", "pharmacy", "pharmacy"],
      [ "callFunc", "addCategory", "$P0", "physiotherapist", "physiotherapist"],
      [ "callFunc", "addCategory", "$P0", "police", "police"],
      [ "callFunc", "addCategory", "$P0", "post_office", "post_office"],
      [ "callFunc", "addCategory", "$P0", "real_estate_agency", "real_estate_agency"],
      [ "callFunc", "addCategory", "$P0", "restaurant", "restaurant"],
      [ "callFunc", "addCategory", "$P0", "rv_park", "rv_park"],
      [ "callFunc", "addCategory", "$P0", "school", "school"],
      [ "callFunc", "addCategory", "$P0", "shoe_store", "shoe_store"],
      [ "callFunc", "addCategory", "$P0", "shopping_mall", "shopping_mall"],
      [ "callFunc", "addCategory", "$P0", "spa", "spa"],
      [ "callFunc", "addCategory", "$P0", "stadium", "stadium"],
      [ "callFunc", "addCategory", "$P0", "synagogue", "synagogue"],
      [ "callFunc", "addCategory", "$P0", "taxi_stand", "taxi_stand"],
      [ "callFunc", "addCategory", "$P0", "train_station", "train_station"],
      [ "callFunc", "addCategory", "$P0", "travel_agency", "travel_agency"],
      [ "callFunc", "addCategory", "$P0", "university", "university"],
      [ "callFunc", "addCategory", "$P0", "veterinary_care", "veterinary_care"],
      [ "callFunc", "addCategory", "$P0", "zoo", "zoo"]
      
    ],
  
    "getNearbyPOIs" => [
      [ "callFunc", "checkNull", "$P0", "$P2", "Latitude"],
      [ "callFunc", "checkNull", "$P0", "$P3", "Longitude"],
      [ "callFunc", "checkNull", "$P0", "$P4", "Radius"],
      [ "callFunc", "checkLessThan", "$P0", "$P2", -90, "Latitude"],
      [ "callFunc", "checkLessThan", "$P0", "$P3", -180, "Longitude"],
      [ "callFunc", "checkLessThan", "$P0", "$P4", 0, "Radius"],
      [ "callFunc", "checkGreaterThan", "$P0", "$P2", 90, "Latitude"],
      [ "callFunc", "checkGreaterThan", "$P0", "$P3", 180, "Longitude"],
      [ "callFunc", "checkGreaterThan", "$P0", "$P4", 40000, "Radius"],
      [ "if!=than", "$P6", nil, 1],
      [ "callFunc", "checkIsEmpty", "$P0", "$P6", "Categories"],
      [ "create", "$L0", "Object"],
      [ "set", "$L0.method", "GET"],
      [ "create", "$L1", "String", "https://maps.googleapis.com/maps/api/place/nearbysearch/json?"],
      [ "string.concat", "$L1", "$L1", "key=", "$P0.apiKey"],
      [ "string.concat", "$L2", "$P2", ",", "$P3"],
      [ "string.urlEncode", "$L2", "$L2"],
      [ "string.concat", "$L1", "$L1", "&location=", "$L2"],
      [ "string.concat", "$L1", "$L1", "&radius=", "$P4"],
      [ "if!=than", "$P5", nil, 2],
      [ "string.urlEncode", "$L2", "$P5"],
      [ "string.concat", "$L1", "$L1", "&keyword=", "$L2"],
      [ "if!=than", "$P6", nil, 3],
      [ "callFunc", "getCategoriesString", "$P0", "$L2", "$P6"],
      [ "string.urlEncode", "$L2", "$L2"],
      [ "string.concat", "$L1", "$L1", "&types=", "$L2"],
      [ "set", "$L0.url", "$L1"],
      [ "http.requestCall", "$L2", "$L0"],
      [ "callFunc", "checkHttpResponse", "$P0", "$L3", "$L2"],
      [ "create", "$P1", "Array"],
      [ "create", "$L4", "Number", 0],
      [ "size", "$L5", "$L3.results"],
      [ "if<than", "$L4", "$L5", 5],
      [ "get", "$L6", "$L3.results", "$L4"],
      [ "callFunc", "extractPOI", "$P0", "$L7", "$L6"],
      [ "push", "$P1", "$L7"],
      [ "math.add", "$L4", "$L4", 1],
      [ "jumpRel", -6]
      
    ],
  
    "checkNull" => [
      [ "if==than", "$P1", nil, 3],
      [ "string.concat", "$L0", "$P2", " is not allowed to be null."],
      [ "create", "$L1", "Error", "$L0", "IllegalArgument"],
      [ "throwError", "$L1"]
      
    ],
  
    "checkLessThan" => [
      [ "if<than", "$P1", "$P2", 3],
      [ "string.concat", "$L0", "$P3", " is not allowed to be less than ", "$P2", "."],
      [ "create", "$L1", "Error", "$L0", "IllegalArgument"],
      [ "throwError", "$L1"]
      
    ],
  
    "checkGreaterThan" => [
      [ "if>than", "$P1", "$P2", 3],
      [ "string.concat", "$L0", "$P3", " is not allowed to be greater than ", "$P2", "."],
      [ "create", "$L1", "Error", "$L0", "IllegalArgument"],
      [ "throwError", "$L1"]
      
    ],
  
    "checkIsEmpty" => [
      [ "size", "$L0", "$P2"],
      [ "if==than", "$L0", 0, 3],
      [ "string.concat", "$L0", "$P3", " is not allowed to be empty."],
      [ "create", "$L1", "Error", "$L0", "IllegalArgument"],
      [ "throwError", "$L1"]
      
    ],
  
    "checkHttpResponse" => [
      [ "if>=than", "$P2.code", 400, 10],
      [ "if==than", "$P2.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", "$P2.code", 503, 2],
      [ "create", "$L3", "Error", "Service unavailable. Try again later.", "ServiceUnavailable"],
      [ "throwError", "$L3"],
      [ "json.parse", "$L0", "$P2.responseBody"],
      [ "string.concat", "$L2", "$P2.code", " - ", "$L0.status"],
      [ "create", "$L3", "Error", "$L2", "Http"],
      [ "throwError", "$L3"],
      [ "json.parse", "$L0", "$P2.responseBody"],
      [ "if!=than", "$L0.status", "OK", 6],
      [ "if!=than", "$L0.error_message", nil, 2],
      [ "create", "$L3", "Error", "$L0.error_message", "Http"],
      [ "throwError", "$L3"],
      [ "if==than", "$L0.error_message", nil, 2],
      [ "create", "$L3", "Error", "$L0.status", "Http"],
      [ "throwError", "$L3"],
      [ "set", "$P1", "$L0"]
      
    ],
  
    "getCategoriesString" => [
      [ "create", "$P1", "String"],
      [ "create", "$L0", "Number", 0],
      [ "size", "$L1", "$P2"],
      [ "if<than", "$L0", "$L1", 10],
      [ "get", "$L2", "$P2", "$L0"],
      [ "get", "$L2", "$P0.crToPlaces", "$L2"],
      [ "if==than", "$L2", nil, 2],
      [ "create", "$L3", "Error", "Unknown category.", "IllegalArgument"],
      [ "throwError", "$L3"],
      [ "if!=than", "$L0", 0, 1],
      [ "string.concat", "$P1", "$P1", "|"],
      [ "string.concat", "$P1", "$P1", "$L2"],
      [ "math.add", "$L0", "$L0", 1],
      [ "jumpRel", -11]
      
    ],
  
    "extractPOI" => [
      [ "create", "$L0", "Array"],
      [ "create", "$L1", "Number", 0],
      [ "size", "$L2", "$P2.types"],
      [ "if<than", "$L1", "$L2", 6],
      [ "get", "$L3", "$P2.types", "$L1"],
      [ "get", "$L4", "$P0.placesToCr", "$L3"],
      [ "if!=than", "$L4", nil, 1],
      [ "push", "$L0", "$L4"],
      [ "math.add", "$L1", "$L1", 1],
      [ "jumpRel", -7],
      [ "create", "$L1", "Location"],
      [ "set", "$L1.latitude", "$P2.geometry.location.lat"],
      [ "set", "$L1.longitude", "$P2.geometry.location.lng"],
      [ "if!=than", "$P2.photos", nil, 3],
      [ "get", "$L2", "$P2.photos", 0],
      [ "get", "$L3", "$L2.photo_reference"],
      [ "string.concat", "$L4", "https://maps.googleapis.com/maps/api/place/photo?key=", "$P0.apiKey", "&photoreference=", "$L3", "&maxheight=", "$L2.height"],
      [ "create", "$P1", "POI", "$L0", "$L4", "$L1", "$P2.name", nil]
      
    ],
  
    "addCategory" => [
      [ "set", "$P0.crToPlaces", "$P2", "$P1"],
      [ "set", "$P0.placesToCr", "$P1", "$P2"]
      
    ]
  
}

Instance Method Summary collapse

Constructor Details

#initialize(redirect_receiver, api_key) ⇒ GooglePlaces

Returns a new instance of GooglePlaces.



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/cloudrail_si/services/GooglePlaces.rb', line 237

def initialize(redirect_receiver, api_key)
    @interpreter_storage = {}
    @persistent_storage = [{}]
    @instance_dependency_storage = {
        redirect_receiver: redirect_receiver
    }

    ServiceCode::InitSelfTest.init_test('GooglePlaces')

    @interpreter_storage['apiKey'] = api_key
    

    

    # 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



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/cloudrail_si/services/GooglePlaces.rb', line 258

def check_for_error(error)
  if (error)
    ServiceCode::Statistics.add_error('GooglePlaces', '')
    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_nearby_pois(latitude, longitude, radius, search_term, categories) ⇒ Object



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

def get_nearby_pois(latitude, longitude, radius, search_term, categories)
  ServiceCode::Statistics.add_call('GooglePlaces', 'get_nearby_pois')
  ip = ServiceCode::Interpreter.new(ServiceCode::Sandbox.new(SERVICE_CODE, @persistent_storage, @instance_dependency_storage))
  ip.call_function('getNearbyPOIs', @interpreter_storage, nil, latitude, longitude, radius, search_term, categories)

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

#load_as_string(saved_state) ⇒ Object



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

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

#resume_login(execution_state, callback) ⇒ Object



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

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



289
290
291
292
# File 'lib/cloudrail_si/services/GooglePlaces.rb', line 289

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