Skip to content

Instantly share code, notes, and snippets.

View abhionlyone's full-sized avatar
:octocat:
Focusing

Abhilash Reddy abhionlyone

:octocat:
Focusing
View GitHub Profile
@abhionlyone
abhionlyone / api_controller.rb
Created November 29, 2017 05:28 — forked from a14m/api_controller.rb
Rails JWT authentication
# app/controllers/api/v1/api_controller.rb
# Base API controller class
class Api::V1::ApiController < ApplicationController
before_action :http_authorization_header?, :authenticate_request, :set_current_user
protected
# Bad Request if http authorization header missing
def http_authorization_header?
fail BadRequestError, 'errors.missing_auth_header' unless authorization_header
@abhionlyone
abhionlyone / facebook.rb
Created November 29, 2017 05:48 — forked from a14m/facebook.rb
Gist for manually OAuth2 facebook for Rails APIs
# lib/omniauth/facebook.rb
require 'httparty'
module Omniauth
class Facebook
include HTTParty
# The base uri for facebook graph API
base_uri 'https://graph.facebook.com/v2.3'
@abhionlyone
abhionlyone / Fix mysql2 gem installation error on mac
Created April 22, 2018 15:12
Fix mysql2 gem installation error on mac
brew install openssl
bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"
@abhionlyone
abhionlyone / rails-jsonb-queries
Created May 15, 2018 08:37 — forked from mankind/rails-jsonb-queries
Rails-5 postgresql-9.6 jsonb queries
http://stackoverflow.com/questions/22667401/postgres-json-data-type-rails-query
http://stackoverflow.com/questions/40702813/query-on-postgres-json-array-field-in-rails
#payload: [{"kind"=>"person"}]
Segment.where("payload @> ?", [{kind: "person"}].to_json)
#data: {"interest"=>["music", "movies", "programming"]}
Segment.where("data @> ?", {"interest": ["music", "movies", "programming"]}.to_json)
Segment.where("data #>> '{interest, 1}' = 'movies' ")
Segment.where("jsonb_array_length(data->'interest') > 1")
@abhionlyone
abhionlyone / deploy.rb
Created May 22, 2018 07:00 — forked from ryancheung/deploy.rb
Capistrano config file example with Resque and Resque Scheduler
require "rvm/capistrano" # Load RVM's capistrano plugin.
require "bundler/capistrano"
set :rvm_ruby_string, '1.9.3'
set :rvm_type, :user # Literal ":user"
set :application, "blog_test"
set :repository, "git@github.com:ryancheung/blog.git"
set :scm, :git
@abhionlyone
abhionlyone / Capfile
Last active April 20, 2022 19:53
create-react-app with capistrano for automated deployments
require "capistrano/setup"
require "capistrano/deploy"
require "capistrano/scm/git"
require 'capistrano/npm'
install_plugin Capistrano::SCM::Git
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
require 'httparty'
require 'jwt'
require 'digest'
require 'securerandom'
require 'json'
class Skype
def initialize
@key = 'b97dfb72-1504-fadd-1b04-05efc186aaeb'
@abhionlyone
abhionlyone / gist:129ed753031bdb534016cf1be5a5bb10
Created August 14, 2018 08:12 — forked from cpjolicoeur/gist:3590737
Ordering a query result set by an arbitrary list in PostgreSQL

I'm hunting for the best solution on how to handle keeping large sets of DB records "sorted" in a performant manner.

Problem Description

Most of us have work on projects at some point where we have needed to have ordered lists of objects. Whether it be a to-do list sorted by priority, or a list of documents that a user can sort in whatever order they want.

A traditional approach for this on a Rails project is to use something like the acts_as_list gem, or something similar. These systems typically add some sort of "postion" or "sort order" column to each record, which is then used when querying out the records in a traditional order by position SQL query.

This approach seems to work fine for smaller datasets, but can be hard to manage on large data sets with hundreds (or thousands) of records needing to be sorted. Changing the sort position of even a single object will require updating every single record in the database that is in the same sort group. This requires potentially thousands of wri

@abhionlyone
abhionlyone / letsencrypt_2018.md
Created August 18, 2018 13:50 — forked from cecilemuller/letsencrypt_2020.md
How to setup Let's Encrypt for Nginx on Ubuntu 18.04 (including IPv6, HTTP/2 and A+ SSL rating)

How to setup Let's Encrypt for Nginx on Ubuntu 18.04 (including IPv6, HTTP/2 and A+ SLL rating)


Virtual hosts

Let's say you want to host domains first.com and second.com.

Create folders for their files:

@abhionlyone
abhionlyone / active_record_initializers.rb
Created August 31, 2018 11:08
Activerecord name starts with
# initializers/active_record_initializers.rb
class ActiveRecord::Base
# do not accept a column_name from the outside without sanitizing it
# as this can be prone to sql injection
def self.starts_with(column_name, prefix)
where("lower(#{column_name}) like ?", "#{prefix.downcase}%")
end
end
# User.starts_with('name', 'ab').limit(1)