Skip to content

Instantly share code, notes, and snippets.

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

Chandan Jhunjhunwal indyarocks

💭
I may be slow to respond.
View GitHub Profile
@indyarocks
indyarocks / active_record_logger.rb
Last active December 25, 2015 08:38
ActiveRecord log in console
> rails c
Loading development environment (Rails 4.0.0)
2.0.0p247 :001 > ActiveRecord::Base.logger = Logger.new(STDOUT)
=> #<Logger:0x007f816e1c35a8 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007f816e1c3580 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x007f816e1c3530 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x007f816e1c3508 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007f816e1c34b8>>>>
2.0.0p247 :002 > user = User.first
D, [2013-10-12T14:46:23.337060 #25919] DEBUG -- : User Load (0.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> #<User id: 1, name: "Chandan Kumar", email: "chandan.jhun@gmail.com", created_at: "2013-09-16 20:01:15", updated_at: "2013-09-16 20:34:20", password_digest: "$2a$10$qoNP6rM.89XSOJEXIsXig.9LKM2jcvY0Y4bFhCECCJ4X...", remember_token: "66302290bbc685a193b5d2ab5c8d3fc67e56318e", admin: true>
2.0.0p247 :003 > user.microposts
@indyarocks
indyarocks / rack_experiment.rb
Created October 13, 2013 15:09
Exploring Rack.
# config.ru
require 'rack/lobster'
require 'logger'
infinity = Rack::Builder.new do
use Rack::CommonLogger
Logger.new('rack.log')
@indyarocks
indyarocks / 0002_sidekiq_initial.config
Created September 23, 2016 17:36 — forked from bnhansn/0002_sidekiq_initial.config
AWS Sidekiq Restart Configuration
---
restart_sidekiq: &RESTART_SIDEKIQ
mode: "000755"
content: |
#!/bin/bash
initctl restart sidekiq || initctl start sidekiq
ln -sf /var/app/current/log/sidekiq.log /var/app/containerfiles/logs/sidekiq.log
mute_sidekiq: &MUTE_SIDEKIQ
mode: "000755"
content: |
@indyarocks
indyarocks / 0002_sidekiq_initial.config
Created September 23, 2016 20:43
AWS Beanstalk Sidekiq config file
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/50_restart_sidekiq":
mode: "000755"
content: |
#!/bin/bash
initctl restart sidekiq || initctl start sidekiq
#ln -sf /var/app/current/log/sidekiq.log /var/app/containerfiles/logs/sidekiq.log
"/opt/elasticbeanstalk/hooks/appdeploy/pre/03_mute_sidekiq":
@indyarocks
indyarocks / auth.rb
Created March 16, 2017 04:35
Auth Module for JWT Authentication
require 'jwt'
class Auth
ALGORITHM = 'HS256'
class << self
def issue(payload, exp = 24.hours.from_now)
payload[:exp] = exp.to_i
JWT.encode(payload, auth_secret, ALGORITHM)
end
@indyarocks
indyarocks / main_controller.rb
Created March 16, 2017 20:18
API Main Controller with
module API
class MainController < ApplicationController
protect_from_forgery with: :null_session
before_action :authenticate
attr_accessor :current_user
def logged_in?
set_current_user
!!@current_user
@indyarocks
indyarocks / sessions_controller.rb
Last active March 16, 2017 20:25
Action to issue JWT token
module API
module V1
class SessionsController < ::API::MainController
skip_before_action :authenticate, only: [:create]
def create
# In users.rb
# def self.by_email_or_username(email:, username:)
# User.where('email = ? OR username = ?', email, username).first
# end
@indyarocks
indyarocks / server.md
Created April 11, 2017 18:59 — forked from jtadeulopes/server.md
Server setup with ubuntu, nginx and puma for rails app.

Update and upgrade the system

sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade && sudo apt-get autoremove
sudo reboot

Configure timezone

require 'final_redirect_url/version'
require 'net/http'
require 'logger'
module FinalRedirectUrl
def self.final_redirect_url(url, options={})
final_url = ''
if is_valid_url?(url)
begin
chandan@~/Workspace/Faodail/OpenSource ○ ➜ irb
2.4.0 :001 > require 'final_redirect_url'
=> true
2.4.0 :002 > FinalRedirectUrl.final_redirect_url('')
=> ""
2.4.0 :003 > FinalRedirectUrl.final_redirect_url('http://google.com')
redirected to http://www.google.co.in/?gfe_rd=cr&ei=e-oPWaSoHaX98wfuqYuoCw
=> "http://www.google.co.in/?gfe_rd=cr&ei=e-oPWaSoHaX98wfuqYuoCw"