housing.misc.gem

Installation

gem 'housing_misc'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install housing_misc

Functions Provided

get_request_attribute_value(headers, params, key)

Parameters:

  1. headers (data_type: ActionDispatch::Http::Headers)
  2. params (data_type: ActionController::Parameters)
  3. key (data_type: String, description: Name of the attribute whose value is needed)

Description: Returns value against the key present in headers or params, first checks in headers if it's not there then checks in params

 

is_request_internal?(request)

Parameters:

  1. request (data_type: ActionDispatch::Request)

Description: Checks if the request passes internal host validation or not based on Rails environment and request's host and returns bool value

 

is_request_from_mobile?(request, params, velocity_salt)

Parameters:

  1. request (data_type: ActionDispatch::Request)
  2. params (data_type: ActionController::Parameters)
  3. velocity_salt (data_type: String, description: Salt to verify signed param with time stamp)

Description: Checks if the request is from mobile or not based on source, time stamp, signed param and salt and returns bool value

 

is_request_csrf_valid?(request, cookies, params, csrf_encryption_key, velocity_salt)

Parameters:

  1. request (data_type: ActionDispatch::Request)
  2. cookies (data_type: ActionDispatch::Cookies::CookieJar)
  3. params (data_type: ActionController::Parameters)
  4. csrf_encryption_key (data_type: String, length: 64 characters (0-9, a-f) i.e. 32 bytes, description: Encryption key to generate csrf token. This key should be kept secret between servers.)
  5. velocity_salt (data_type: String, description: Salt to verify signed param with time stamp)

Description: Checks if the request passes csrf validation or not and returns bool value

 

internal_host_check

Description: Checks if the request passes internal host validation or not. If the validation fails then logs information in /log/unauthorized_api_requests.log and renders :json => => "You are not authorized to make this call", status: 401

Note: It can only be called from controller as it uses request (ActionDispatch::Request)

Usage: In your controller:

include HousingMisc
before_action :internal_host_check, :only => [:method_1, :method_2, ...]

 

send_generic_mail(template_slug, template_version, users, base_vars, merge_vars, high_priority=false, attachments=[], deferred=false, send_time=nil, google_analytics_campaign=nil)

Parameters:

  1. template_slug (data_type:string, descriptinon:contains name of the template)
  2. template_version (data_type: string, description:version of mail_template e.g. "1")
  3. users (data_type: [=> "xyz", "name" => "m"], description: array of email ids)
  4. base_vars (data_type: Hash, desciption: contains subject and callback_url)
  5. merge_vars (data_type: Hash, description: includes vars that are embedded in mail)
  6. high_priority (data_type : boolean, description:decides the priority of mail)
  7. attachments (:data_type : [], deacription: contains attachments to mail)
  8. deferred (data_type : boolean, description:decides if mail is to be sent later)
  9. send_time (data_tpe: Time, desciption: e.g. 2018-04-06 15:55:33 +0530, nil=send_now)
  10. google_analytics_campaign(data_type: string, description: Name of campaign)

Description: Sends mail for a given template and its version to mentioned users.

 

csrf_check(csrf_encryption_key, velocity_salt)

Parameters:

  1. csrf_encryption_key (data_type: String, length: 64 characters (0-9, a-f) i.e. 32 bytes, description: Encryption key to generate csrf token. This key should be kept secret between servers.)
  1. velocity_salt (data_type: String, description: Salt to verify signed param with time stamp)

Description: Checks if the request passes csrf validation or not. If the validation fails then logs information in /log/unauthorized_api_requests.log and renders :json => => "You are not authorized to make this call", status: 401

Note: It can only be called from controller as it uses request (ActionDispatch::Request), cookies (ActionDispatch::Cookies::CookieJar) and params (ActionController::Parameters)

Usage: In your controller:

include HousingMisc
before_action :only => [:method_1, :method_2, ...] do
    csrf_check(Housing.csrf_encryption_key)
end