Class: Visor::Auth::Backends::Base
- Inherits:
-
Object
- Object
- Visor::Auth::Backends::Base
- Defined in:
- lib/auth/backends/base.rb
Overview
This is the Base super class for all Backends. Each new backend inherits from Base, which contains the model and all validations for the users metadata.
Implementing a new backend is as simple as create a new backend class which inherits from Base and them implement the specific methods for querying the underlying database.
Constant Summary collapse
- MANDATORY =
Keys validation
Mandatory attributes
[:access_key, :email]
- READONLY =
Read-only attributes
[:_id, :secret_key, :created_at, :updated_at]
- ALL =
All attributes
MANDATORY + READONLY
Instance Attribute Summary collapse
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize(opts) ⇒ Base
constructor
Initializes a Backend instance.
-
#set_protected_post(info) ⇒ Hash
Set protected fields value from a post operation.
-
#set_protected_put(info) ⇒ Hash
Set protected field value from a get operation.
-
#to_sql_insert(h) ⇒ String
Generates a compatible SQL INSERT string from a hash.
-
#to_sql_update(h) ⇒ String
Generates a compatible SQL UPDATE string from a hash.
-
#to_sql_where(h) ⇒ String
Generates a compatible SQL WHERE string from a hash.
-
#validate_data_post(info) ⇒ Object
Validates the user information for a post operation, based on possible keys and values.
-
#validate_data_put(info) ⇒ Object
Validates the user information for a put operation, based on possible keys and values.
-
#validate_query_filters(filters) ⇒ Object
Validates that incoming query filters fields are valid.
Constructor Details
#initialize(opts) ⇒ Base
Initializes a Backend instance.
37 38 39 40 41 42 43 44 |
# File 'lib/auth/backends/base.rb', line 37 def initialize(opts) @host = opts[:host] @port = opts[:port] @db = opts[:db] @user = opts[:user] @password = opts[:password] @conn = opts[:conn] end |
Instance Attribute Details
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
24 25 26 |
# File 'lib/auth/backends/base.rb', line 24 def conn @conn end |
#db ⇒ Object (readonly)
Returns the value of attribute db.
24 25 26 |
# File 'lib/auth/backends/base.rb', line 24 def db @db end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
24 25 26 |
# File 'lib/auth/backends/base.rb', line 24 def host @host end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
24 25 26 |
# File 'lib/auth/backends/base.rb', line 24 def password @password end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
24 25 26 |
# File 'lib/auth/backends/base.rb', line 24 def port @port end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
24 25 26 |
# File 'lib/auth/backends/base.rb', line 24 def user @user end |
Instance Method Details
#set_protected_post(info) ⇒ Hash
Set protected fields value from a post operation. Being them the _id and created_at.
80 81 82 |
# File 'lib/auth/backends/base.rb', line 80 def set_protected_post(info) info.merge!(_id: SecureRandom.uuid, secret_key: SecureRandom.base64(30), created_at: Time.now) end |
#set_protected_put(info) ⇒ Hash
Set protected field value from a get operation. Being it the updated_at.
91 92 93 |
# File 'lib/auth/backends/base.rb', line 91 def set_protected_put(info) info.merge!(updated_at: Time.now) end |
#to_sql_insert(h) ⇒ String
Generates a compatible SQL INSERT string from a hash.
124 125 126 127 |
# File 'lib/auth/backends/base.rb', line 124 def to_sql_insert(h) surround = h.values.map { |v| string_time_or_hash?(v) ? "'#{v}'" : v } %W{(#{h.keys.join(', ')}) (#{surround.join(', ')})} end |
#to_sql_update(h) ⇒ String
Generates a compatible SQL UPDATE string from a hash.
113 114 115 |
# File 'lib/auth/backends/base.rb', line 113 def to_sql_update(h) h.map { |k, v| string_time_or_hash?(v) ? "#{k}='#{v}'" : "#{k}=#{v}" }.join(', ') end |
#to_sql_where(h) ⇒ String
Generates a compatible SQL WHERE string from a hash.
102 103 104 |
# File 'lib/auth/backends/base.rb', line 102 def to_sql_where(h) h.map { |k, v| string_time_or_hash?(v) ? "#{k}='#{v}'" : "#{k}=#{v}" }.join(' AND ') end |
#validate_data_post(info) ⇒ Object
Validates the user information for a post operation, based on possible keys and values.
@raise If some of the information fields do not respect the
possible values, contains any read-only or misses any mandatory field.
53 54 55 56 57 |
# File 'lib/auth/backends/base.rb', line 53 def validate_data_post(info) info.assert_exclusion_keys(READONLY) info.assert_inclusion_keys(MANDATORY) validate_email(info[:email]) end |
#validate_data_put(info) ⇒ Object
Validates the user information for a put operation, based on possible keys and values.
@raise If some of the metadata fields do not respect the
possible values, contains any read-only or misses any mandatory field.
66 67 68 69 |
# File 'lib/auth/backends/base.rb', line 66 def validate_data_put(info) info.assert_exclusion_keys(READONLY) validate_email(info[:email]) if info[:email] end |
#validate_query_filters(filters) ⇒ Object
Validates that incoming query filters fields are valid.
@raise If some of the query filter fields do not respect the
possible values.
136 137 138 139 |
# File 'lib/auth/backends/base.rb', line 136 def validate_query_filters(filters) filters.symbolize_keys! filters.assert_valid_keys(ALL) end |