Skip to content

Instantly share code, notes, and snippets.

View amolpujari's full-sized avatar
💭
I may be slow to respond.

Amol Pujari amolpujari

💭
I may be slow to respond.
  • Pune
View GitHub Profile
@amolpujari
amolpujari / text_sanitizer.rb
Last active April 18, 2020 06:56
rails 5 application record text sanitizer
# frozen_string_literal: true
module TextSanitizer
extend ActiveSupport::Concern
SKIP_COLUMNS = %w{encrypted_password confirmation_token current_sign_in_ip expires_at image image_url last_sign_in_ip refresh_token}
included do
include ::SanitizerHelper
extend ::SanitizerHelper
@amolpujari
amolpujari / sanitizer_helper.rb
Created April 18, 2020 06:54
Sanitizer Helper
module SanitizerHelper
def sanitize text
text = text.to_s.
gsub("?xml","").
gsub(/DOCTYPE|json|CDATA/,"").
gsub("<p><br></p>", "<br/>").
gsub(/\<(script|iframe|xml|applet|body|code|em|form|html|head|header|footer|input|textarea|map|nav|pre|template|tab|var|video|canvas|audio|ruby|wbrxlink|xs|binding|soap|rdf|channel|rss)/,"").
gsub(/ +/," ").
gsub(/(\<br\>)+/,"<br>").
gsub(/\n+/,"\n").
@amolpujari
amolpujari / serachable.rb
Last active April 18, 2020 06:57
rails 5 elastic search kicksearch wrapper
# frozen_string_literal: true
module Searchable
module ApplicationRecordAddition
def search_on *method_names, &import_block
send :include, ::Searchable
scope :search_import, import_block if import_block
@amolpujari
amolpujari / application_datatable.rb
Last active April 18, 2020 06:57
rails 5 datatable
# frozen_string_literal: true
class ApplicationDatatable
delegate :params, :link_to, :strip_tags, :local_time_ago, :local_date, :current_user, :avatar,
:avatar_link, :local_time, :admin?, to: :@view
def initialize view, options={}
@view = view
@options = options
@amolpujari
amolpujari / json_response.rb
Last active April 18, 2020 06:58
rails 5 fast_jsonapi oj serializer
# frozen_string_literal: true
require 'time_difference'
module JsonResponse
extend ActiveSupport::Concern
LOGGER = Logger.new("#{Rails.root}/log/fast_jsonapi.log")
included do
@amolpujari
amolpujari / json_response_example_for_so.rb
Last active April 18, 2020 06:58
rails 5 fast_jsonapi oj serializer api action
# frozen_string_literal: true
module JsonResponse
extend ActiveSupport::Concern
included do
end
module ClassMethods
def api_action action_name, &block
@amolpujari
amolpujari / application_job.rb
Last active April 18, 2020 06:58
rails 5 application job
class ApplicationJob < ActiveJob::Base
include ::PrivateInspector
include ::SanitizerHelper
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked
# Most jobs are safe to ignore if the underlying records are no longer available
# discard_on ActiveJob::DeserializationError
@amolpujari
amolpujari / private_inspector.rb
Created September 13, 2019 07:28
inspect ruby object in private
Object.class_eval do
def private_inspect
memo = {}
self.instance_variables.inject(memo) do |memo, var|
memo[var] = self.instance_variable_get(var).to_s
memo
end
except = [:__, :_, :_ex_, :_pry_, :_out_, :_in_, :_dir_, :_file_]
@amolpujari
amolpujari / application_serializer.rb
Last active April 18, 2020 06:58
rails 5 fast_jsonapi oj serializer
class ApplicationSerializer
include FastJsonapi::ObjectSerializer
#cache_options enabled: true, cache_length: 1.hour
def to_h
data = serializable_hash
if data[:data].is_a? Hash
data[:data][:attributes]
@amolpujari
amolpujari / skype_interviews_api_ruby_example.rb
Created October 1, 2018 11:46
skype interviews api ruby example
require 'jwt'
require 'json'
API_KEY = "key"
API_SECRET = "secret"
token = JWT.encode({
jti: SecureRandom.hex,
iss: API_KEY,
iat: Time.now.to_i,