Class: Usman::MobileRegistrationService
- Inherits:
-
Object
- Object
- Usman::MobileRegistrationService
- Defined in:
- app/services/usman/mobile_registration_service.rb
Instance Attribute Summary collapse
-
#city ⇒ Object
readonly
Returns the value of attribute city.
-
#country ⇒ Object
readonly
Returns the value of attribute country.
-
#device ⇒ Object
readonly
Returns the value of attribute device.
-
#device_name ⇒ Object
readonly
Returns the value of attribute device_name.
-
#device_token ⇒ Object
readonly
Returns the value of attribute device_token.
-
#device_type ⇒ Object
readonly
Returns the value of attribute device_type.
-
#dialing_prefix ⇒ Object
readonly
Returns the value of attribute dialing_prefix.
-
#error_details ⇒ Object
readonly
Returns the value of attribute error_details.
-
#error_heading ⇒ Object
readonly
Returns the value of attribute error_heading.
-
#error_message ⇒ Object
readonly
Returns the value of attribute error_message.
-
#mobile_number ⇒ Object
readonly
Returns the value of attribute mobile_number.
-
#operating_system ⇒ Object
readonly
Returns the value of attribute operating_system.
-
#registration ⇒ Object
readonly
Returns the value of attribute registration.
-
#remote_ip ⇒ Object
readonly
Returns the value of attribute remote_ip.
-
#software_version ⇒ Object
readonly
Returns the value of attribute software_version.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
Instance Method Summary collapse
- #check_if_device_is_already_registered ⇒ Object
- #create_a_dummy_user ⇒ Object
- #errors ⇒ Object
- #generate_new_otp ⇒ Object
-
#initialize(params) ⇒ MobileRegistrationService
constructor
A new instance of MobileRegistrationService.
- #register_new_device ⇒ Object
- #set_error(key, hsh = {}) ⇒ Object
Constructor Details
#initialize(params) ⇒ MobileRegistrationService
Returns a new instance of MobileRegistrationService.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/services/usman/mobile_registration_service.rb', line 12 def initialize(params) @dialing_prefix = params[:dialing_prefix] @mobile_number = params[:mobile_number] @country = Country.find_by_id(params[:country_id]) @city = City.find_by_id(params[:city_id]) # Edge case to catch city selected that of a different country @city = nil unless @city.country == @country if @city @uuid = params[:uuid] @device_token = params[:device_token] @device_name = params[:device_name] @device_type = params[:device_type] @operating_system = params[:operating_system] @software_version = params[:software_version] @remote_ip = params[:remote_ip] @error_message = nil @error_details = {} # @registration and @device will be initiated by the # below method if the device is already registered check_if_device_is_already_registered register_new_device # Validate the inputs @registration.valid? @device.valid? # Create a dummy user if @registration.user_id.blank? create_a_dummy_user end if @registration.errors.any? or @device.errors.any? errors = @registration.errors.to_hash.merge(@device.errors.to_hash) set_error("api.register.invalid_inputs", errors) else @registration.save @device.save end end |
Instance Attribute Details
#city ⇒ Object (readonly)
Returns the value of attribute city.
4 5 6 |
# File 'app/services/usman/mobile_registration_service.rb', line 4 def city @city end |
#country ⇒ Object (readonly)
Returns the value of attribute country.
4 5 6 |
# File 'app/services/usman/mobile_registration_service.rb', line 4 def country @country end |
#device ⇒ Object (readonly)
Returns the value of attribute device.
4 5 6 |
# File 'app/services/usman/mobile_registration_service.rb', line 4 def device @device end |
#device_name ⇒ Object (readonly)
Returns the value of attribute device_name.
4 5 6 |
# File 'app/services/usman/mobile_registration_service.rb', line 4 def device_name @device_name end |
#device_token ⇒ Object (readonly)
Returns the value of attribute device_token.
4 5 6 |
# File 'app/services/usman/mobile_registration_service.rb', line 4 def device_token @device_token end |
#device_type ⇒ Object (readonly)
Returns the value of attribute device_type.
4 5 6 |
# File 'app/services/usman/mobile_registration_service.rb', line 4 def device_type @device_type end |
#dialing_prefix ⇒ Object (readonly)
Returns the value of attribute dialing_prefix.
4 5 6 |
# File 'app/services/usman/mobile_registration_service.rb', line 4 def dialing_prefix @dialing_prefix end |
#error_details ⇒ Object (readonly)
Returns the value of attribute error_details.
4 5 6 |
# File 'app/services/usman/mobile_registration_service.rb', line 4 def error_details @error_details end |
#error_heading ⇒ Object (readonly)
Returns the value of attribute error_heading.
4 5 6 |
# File 'app/services/usman/mobile_registration_service.rb', line 4 def error_heading @error_heading end |
#error_message ⇒ Object (readonly)
Returns the value of attribute error_message.
4 5 6 |
# File 'app/services/usman/mobile_registration_service.rb', line 4 def @error_message end |
#mobile_number ⇒ Object (readonly)
Returns the value of attribute mobile_number.
4 5 6 |
# File 'app/services/usman/mobile_registration_service.rb', line 4 def mobile_number @mobile_number end |
#operating_system ⇒ Object (readonly)
Returns the value of attribute operating_system.
4 5 6 |
# File 'app/services/usman/mobile_registration_service.rb', line 4 def @operating_system end |
#registration ⇒ Object (readonly)
Returns the value of attribute registration.
4 5 6 |
# File 'app/services/usman/mobile_registration_service.rb', line 4 def registration @registration end |
#remote_ip ⇒ Object (readonly)
Returns the value of attribute remote_ip.
4 5 6 |
# File 'app/services/usman/mobile_registration_service.rb', line 4 def remote_ip @remote_ip end |
#software_version ⇒ Object (readonly)
Returns the value of attribute software_version.
4 5 6 |
# File 'app/services/usman/mobile_registration_service.rb', line 4 def software_version @software_version end |
#uuid ⇒ Object (readonly)
Returns the value of attribute uuid.
4 5 6 |
# File 'app/services/usman/mobile_registration_service.rb', line 4 def uuid @uuid end |
Instance Method Details
#check_if_device_is_already_registered ⇒ Object
58 59 60 61 62 |
# File 'app/services/usman/mobile_registration_service.rb', line 58 def check_if_device_is_already_registered @registration = Registration.where("LOWER(mobile_number) = LOWER('#{@mobile_number}') AND LOWER(dialing_prefix) = LOWER('#{@dialing_prefix}')").first @device = Device.where("LOWER(uuid) = LOWER('#{@uuid}')").first if @registration @device.generate_otp if @device end |
#create_a_dummy_user ⇒ Object
117 118 119 120 121 122 123 |
# File 'app/services/usman/mobile_registration_service.rb', line 117 def create_a_dummy_user @user = User.new @user.generate_dummy_data(@registration) @user.save @registration.user = @user @device.user = @user if @device end |
#errors ⇒ Object
131 132 133 134 135 136 137 |
# File 'app/services/usman/mobile_registration_service.rb', line 131 def errors { heading: @error_heading, message: @error_message, details: @error_details } end |
#generate_new_otp ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'app/services/usman/mobile_registration_service.rb', line 108 def generate_new_otp @device.generate_otp if @device.send_otp @device.update_attribute(:otp_sent_at, Time.now) else set_error("api.register.otp_not_sent") end end |
#register_new_device ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/services/usman/mobile_registration_service.rb', line 64 def register_new_device if @device && @device.blocked? set_error("api.general.device_blocked") return end ActiveRecord::Base.transaction do # Create a new registration if it doesn't exist @registration = Registration.new unless @registration @user = @registration.user @registration.country = @country @registration.city = @city @registration.dialing_prefix = @dialing_prefix @registration.mobile_number = @mobile_number # Create device entry if it doesn't exist @device = Device.new unless @device @device.registration = @registration @device.user = @registration.user @device.uuid = @uuid @device.api_token = SecureRandom.hex @device.device_token = @device_token @device.device_name = @device_name @device.device_type = @device_type @device. = @operating_system @device.software_version = @software_version @registration.valid? @device.valid? if @registration.errors.blank? && @device.errors.blank? @registration.save # Saving User to device table @device.save generate_new_otp else raise ActiveRecord::Rollback end end end |
#set_error(key, hsh = {}) ⇒ Object
125 126 127 128 129 |
# File 'app/services/usman/mobile_registration_service.rb', line 125 def set_error(key, hsh={}) @error_heading = I18n.t("#{key}.heading") @error_message = I18n.t("#{key}.message") @error_details = hsh if hsh.is_a?(Hash) end |