usman 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dcf66615fd081189a376eedeb39266e06d8a2126
4
- data.tar.gz: 311251208365a4a012a4c84ce59b776ef6d70a87
3
+ metadata.gz: 335b7ba4af0e58280de99aa7211b1d91d992de85
4
+ data.tar.gz: ea3bd4266a24ce29aa0b0602f3e7f4f89e820dda
5
5
  SHA512:
6
- metadata.gz: 2d6df82e6a200bf4f973eda91c40888b828c1fe7ec9a4dfdb353e2b4c3665ae9debfd58e462a363530377e3797218f7d431578e5b50a4968cd97096d9e8cef86
7
- data.tar.gz: d059b684edf4e98ff8936483186df1748b87d87e38a4c42a1f288522c516a0523efa8b1708a5d4751f0e53ff9e74fddc7f85aa899c78d089e22d045e3b643255
6
+ metadata.gz: 1f269b5fc47555878826becdcdc0499a56f199ae04e46e016fb3847add390e23e74aab6aec88f9a170bacd9860b4b96acf05329c40e4cd5a49bd5c570bce4325
7
+ data.tar.gz: 3d49ae90104d363911ff1ae39a0ea017020e47b4a8abc504b0cc6ec5522c243a1eae75bb85c27e890893cf866269c8cd2deec66671102883d00f6522795d4010
@@ -26,12 +26,50 @@ module Api
26
26
 
27
27
  def resend_otp
28
28
  proc_code = Proc.new do
29
+
29
30
  end
30
31
  render_json_response(proc_code)
31
32
  end
32
33
 
33
34
  def verify
34
35
  proc_code = Proc.new do
36
+ @device = Device.where("uuid = ?", params[:uuid]).first
37
+ if @device
38
+ if @device.blocked?
39
+ @success = false
40
+ @errors = {
41
+ heading: I18n.translate("mobile_registration.device_blocked.heading"),
42
+ message: I18n.translate("mobile_registration.device_blocked.message"),
43
+ details: {}
44
+ }
45
+ else
46
+ valid, validation_errors = @device.validate_otp(params[:otp], params[:dialing_prefix], params[:mobile_number])
47
+ if valid
48
+ @success = true
49
+ @alert = {
50
+ heading: I18n.translate("mobile_registration.verification_success.heading"),
51
+ message: I18n.translate("mobile_registration.verification_success.message")
52
+ }
53
+ @data = { api_token: @device.api_token }
54
+ else
55
+ @success = false
56
+ @errors = {
57
+ heading: I18n.translate("mobile_registration.otp_not_matching.heading"),
58
+ message: I18n.translate("mobile_registration.otp_not_matching.message"),
59
+ details: validation_errors
60
+ }
61
+ end
62
+ end
63
+ else
64
+ @success = false
65
+ @errors = {
66
+ heading: I18n.translate("general.unexpected_failure.heading"),
67
+ message: I18n.translate("general.unexpected_failure.message"),
68
+ details: {
69
+ uuid: "is invalid"
70
+ }
71
+ }
72
+ end
35
73
  end
36
74
  render_json_response(proc_code)
37
75
  end
@@ -43,7 +43,8 @@ module Usman
43
43
  end
44
44
 
45
45
  def embed_stack_in_json_response?
46
- ["true", "t", "1", "yes"].include?(params[:debug].to_s.downcase.strip) # || Rails.env == "development"
46
+ return true if Rails.env.development?
47
+ Rails.env.production? && ["true", "t", "1", "yes"].include?(params[:debug].to_s.downcase.strip)
47
48
  end
48
49
 
49
50
  ## This method will accept a proc, execute it and render the json
@@ -55,9 +56,9 @@ module Usman
55
56
  rescue Exception => e
56
57
  @success = false
57
58
  @errors = {
58
- heading: I18n.translate("response.unexpected_failure.heading"),
59
- message: e.message.underscore,
60
- details: I18n.translate("response.#{e.message.underscore}.details"),
59
+ heading: I18n.translate("general.unexpected_failure.heading"),
60
+ message: I18n.translate("general.unexpected_failure.message"),
61
+ details: e.message,
61
62
  stacktrace: (embed_stack_in_json_response? ? e.backtrace : nil)
62
63
  }
63
64
  end
data/app/models/device.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  class Device < ApplicationRecord
2
2
 
3
3
  # Constants
4
+ EXCLUDED_JSON_ATTRIBUTES = [:last_accessed_at, :last_accessed_api, :otp, :otp_sent_at, :api_token, :token_created_at, :status, :created_at, :updated_at]
5
+
4
6
  PENDING = "pending"
5
7
  VERIFIED = "verified"
6
8
  BLOCKED = "blocked"
@@ -63,6 +65,15 @@ class Device < ApplicationRecord
63
65
  # Instance Methods
64
66
  # ------------------
65
67
 
68
+ # Exclude some attributes info from json output.
69
+ def as_json(options={})
70
+ options[:except] ||= EXCLUDED_JSON_ATTRIBUTES
71
+ #options[:include] ||= []
72
+ #options[:methods] = []
73
+ #options[:methods] << :profile_image
74
+ super(options)
75
+ end
76
+
66
77
  # Status Methods
67
78
  # --------------
68
79
 
@@ -135,6 +146,41 @@ class Device < ApplicationRecord
135
146
  self.otp = rand(10000..99999)
136
147
  end
137
148
 
149
+ def validate_otp(otp, dialing_prefix, mobile_number)
150
+
151
+ # Validate OTP and other parameters
152
+ validation_errors = {}
153
+
154
+ # TODO - remove 11111 after implementing Twilio
155
+ validation_errors[:otp] = "doesn't match with our database" unless (self.otp.to_s == otp.to_s || self.otp.to_s == "11111")
156
+ validation_errors[:mobile_number] = "doesn't match with our database" unless self.registration.mobile_number.to_s == mobile_number.to_s
157
+ validation_errors[:dialing_prefix] = "doesn't match with our database" unless self.registration.dialing_prefix.to_s == dialing_prefix.to_s
158
+
159
+ if validation_errors.empty?
160
+ if self.otp_verified_at.blank?
161
+
162
+ # Create API Token if OTP is verified
163
+ self.otp_verified_at = Time.now
164
+ self.api_token = SecureRandom.hex
165
+ self.token_created_at = Time.now
166
+ self.save
167
+
168
+ self.verify!
169
+ self.registration.verify!
170
+
171
+ return true, {}
172
+ else
173
+
174
+ # Check if this OTP was already verified
175
+ validation_errors[:otp_verified_at] = "This OTP was already used."
176
+
177
+ return false, validation_errors
178
+ end
179
+ else
180
+ return false, validation_errors
181
+ end
182
+ end
183
+
138
184
  # Other Methods
139
185
  # -------------
140
186
 
@@ -1,6 +1,8 @@
1
1
  class Registration < ApplicationRecord
2
2
 
3
3
  # Constants
4
+ EXCLUDED_JSON_ATTRIBUTES = [:status, :created_at, :updated_at]
5
+
4
6
  PENDING = "pending"
5
7
  VERIFIED = "verified"
6
8
 
@@ -15,7 +17,7 @@ class Registration < ApplicationRecord
15
17
  }
16
18
 
17
19
  # Associations
18
- has_one :user
20
+ belongs_to :user, optional: true
19
21
  belongs_to :country
20
22
  belongs_to :city, optional: true
21
23
  has_many :devices
@@ -46,6 +48,15 @@ class Registration < ApplicationRecord
46
48
  # Instance Methods
47
49
  # ------------------
48
50
 
51
+ # Exclude some attributes info from json output.
52
+ def as_json(options={})
53
+ options[:except] ||= EXCLUDED_JSON_ATTRIBUTES
54
+ #options[:include] ||= []
55
+ #options[:methods] = []
56
+ #options[:methods] << :profile_image
57
+ super(options)
58
+ end
59
+
49
60
  # Status Methods
50
61
  # --------------
51
62
 
data/app/models/user.rb CHANGED
@@ -4,6 +4,9 @@ class User < Usman::ApplicationRecord
4
4
  has_secure_password
5
5
 
6
6
  # Constants
7
+
8
+ EXCLUDED_JSON_ATTRIBUTES = [:confirmation_token, :password_digest, :reset_password_token, :unlock_token, :status, :reset_password_sent_at, :remember_created_at, :sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip, :confirmed_at, :confirmation_sent_at, :unconfirmed_email, :failed_attempts, :locked_at, :created_at, :updated_at]
9
+
7
10
  PENDING = "pending"
8
11
  APPROVED = "approved"
9
12
  SUSPENDED = "suspended"
@@ -48,6 +51,15 @@ class User < Usman::ApplicationRecord
48
51
  # Class Methods
49
52
  # ------------------
50
53
 
54
+ # Exclude some attributes info from json output.
55
+ def as_json(options={})
56
+ options[:except] ||= EXCLUDED_JSON_ATTRIBUTES
57
+ #options[:include] ||= []
58
+ #options[:methods] = []
59
+ #options[:methods] << :profile_image
60
+ super(options)
61
+ end
62
+
51
63
  # Scopes Methods
52
64
 
53
65
  # return an active record relation object with the search query in its where clause
@@ -13,6 +13,8 @@ module Usman
13
13
  @dialing_prefix = params[:dialing_prefix]
14
14
  @mobile_number = params[:mobile_number]
15
15
 
16
+ @country = nil
17
+ @city = nil
16
18
  @country = Country.find_by_id(params[:country_id])
17
19
  @city = City.find_by_id(params[:city_id])
18
20
 
@@ -50,8 +52,8 @@ module Usman
50
52
  def check_if_device_is_already_registered
51
53
  @registration = Registration.where("LOWER(mobile_number) = LOWER('#{@mobile_number}')").first
52
54
  if @registration
53
- @country = @registration.country unless @country
54
- @city = @registration.city unless @city
55
+ @registration.country = @country
56
+ @registration.city = @city
55
57
  end
56
58
  @device = Device.where("LOWER(uuid) = LOWER('#{@uuid}')").first if @registration
57
59
  end
@@ -0,0 +1,5 @@
1
+ en:
2
+ general:
3
+ unexpected_failure:
4
+ heading: "Unexpected Failure"
5
+ message: "We're sorry, but something went wrong (500)"
@@ -0,0 +1,5 @@
1
+ en:
2
+ general:
3
+ unexpected_failure:
4
+ heading: "Unexpected Failure"
5
+ message: "We're sorry, but something went wrong (500)"
@@ -11,4 +11,13 @@ en:
11
11
  message: "Check your mobile for new message from us."
12
12
  otp_not_sent:
13
13
  heading: "OTP was not sent"
14
- message: "There was some technical glitch and OTP was not sent. Try after some time."
14
+ message: "There was some technical glitch and OTP was not sent. Try after some time."
15
+ otp_not_matching:
16
+ heading: "OTP verification was failed"
17
+ message: "Make sure that you enter the OTP correctly."
18
+ verification_failed:
19
+ heading: "OTP verification was failed"
20
+ message: "Check if you have properly given the OTP along with dialing prefix, mobile number & UUID"
21
+ verification_success:
22
+ heading: "OTP was verified succesfully"
23
+ message: "Store and use the API token for further communication"
@@ -11,4 +11,13 @@ en:
11
11
  message: "Check your mobile for new message from us."
12
12
  otp_not_sent:
13
13
  heading: "OTP was not sent"
14
- message: "There was some technical glitch and OTP was not sent. Try after some time."
14
+ message: "There was some technical glitch and OTP was not sent. Try after some time."
15
+ otp_not_matching:
16
+ heading: "OTP verification was failed"
17
+ message: "Make sure that you enter the OTP correctly."
18
+ verification_failed:
19
+ heading: "OTP verification was failed"
20
+ message: "Check if you have properly given the OTP along with dialing prefix, mobile number & UUID"
21
+ verification_success:
22
+ heading: "OTP was verified succesfully"
23
+ message: "Store and use the API token for further communication"
data/config/routes.rb CHANGED
@@ -38,8 +38,8 @@ Usman::Engine.routes.draw do
38
38
  namespace :api do
39
39
  namespace :v1 do
40
40
  post :register, :controller => "/api/v1/registrations"
41
- #post :resend_otp, :controller => "/api/v1/registrations"
42
- #post :verify, :controller => "/api/v1/registrations"
41
+ post :resend_otp, :controller => "/api/v1/registrations"
42
+ post :verify, :controller => "/api/v1/registrations"
43
43
  end
44
44
  end
45
45
 
@@ -0,0 +1,5 @@
1
+ class AddOtpVerifiedAtToDevices < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_column :devices, :otp_verified_at, :datetime
4
+ end
5
+ end
data/lib/usman/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Usman
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - kpvarma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-24 00:00:00.000000000 Z
11
+ date: 2017-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -413,6 +413,8 @@ files:
413
413
  - app/views/usman/users/index.html.erb
414
414
  - config/locales/usman/authentication.ar.yml
415
415
  - config/locales/usman/authentication.en.yml
416
+ - config/locales/usman/general.ar.yml
417
+ - config/locales/usman/general.en.yml
416
418
  - config/locales/usman/mobile_registration.ar.yml
417
419
  - config/locales/usman/mobile_registration.en.yml
418
420
  - config/routes.rb
@@ -431,6 +433,7 @@ files:
431
433
  - db/migrate/20170819113217_create_registrations.rb
432
434
  - db/migrate/20170819113218_add_registration_id_to_users.rb
433
435
  - db/migrate/20170819113219_create_devices.rb
436
+ - db/migrate/20170825020624_add_otp_verified_at_to_devices.rb
434
437
  - lib/tasks/usman/data.rake
435
438
  - lib/tasks/usman/master_data.rake
436
439
  - lib/temp/features.rake