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 / redis-set.rb
Created May 21, 2017 18:43
Redis SET Data type
127.0.0.1:6379> SADD set-key item # Initializes a SET with key set-key. adds item and returns 1 as success
(integer) 1
127.0.0.1:6379> SADD set-key item # item already exists, thus returns 0 as failure
(integer) 0
127.0.0.1:6379> SADD set-key item1 # add item1 to the set with key set-key
(integer) 1
127.0.0.1:6379> SADD set-key item2 # add item2 to the set with key set-key
(integer) 1
127.0.0.1:6379> SMEMBERS set-key # Fetches all items in the set with key set-key
1) "item1"
@indyarocks
indyarocks / redis-list.rb
Created May 21, 2017 18:04
Redis LIST data type
127.0.0.1:6379> RPUSH list-key item1 # Initializing list by pushing an item for key list-key
(integer) 1
127.0.0.1:6379> RPUSH list-key item2 # Pushing item2 on list
(integer) 2
127.0.0.1:6379> RPUSH list-key item3 # Pushing item3 on list
(integer) 3
127.0.0.1:6379> RPUSH list-key1 item # Initializing list by pushing an item for key list-key
(integer) 1
127.0.0.1:6379> RPUSH list-key1 item1 # Pushing item1 on list
(integer) 2
@indyarocks
indyarocks / redis-string.rb
Created May 21, 2017 17:36
Redis STRING data type
127.0.0.1:6379> SET foo bar # Setting foo with value bar
OK
127.0.0.1:6379> GET foo # Getting value at key foo
"bar"
127.0.0.1:6379> GET chandan # Getting a non-set value. Returns nil
(nil)
127.0.0.1:6379> DEL chandan # Deleting a non-set key. Returns nil
(integer) 0
127.0.0.1:6379> SET chandan faodail # Setting a new key-value (Note: All downcased letters in key)
OK
@indyarocks
indyarocks / create_activities_table.rake
Created May 18, 2017 19:27
Rake task to create a DynamoDB table in Rails application
# lib/tasks/dynamodb_tables_v1/create_activity_table.rake
# Rake task to create activities table
namespace :dynamodb_tables_v1 do
desc "bundle exec rake dynamodb_tables_v1:create_activity_table RAILS_ENV=<ENV>"
task :create_activity_table => :environment do
puts "Creating activities table in #{Rails.env}\n"
create_activity_table
puts "Completed task\n"
end
@indyarocks
indyarocks / gist:3a6439604fe2f4e1331aa7fa98afb930
Last active July 15, 2020 09:43
DynamoDB Client for Rails Application
## /config/initializers/dynamodb_client.rb
module DynamodbClient
def self.initialize!
client_config = if Rails.env.development?
{
region: 'us-west-2',
endpoint: 'http://localhost:8000'
}
else
{
@indyarocks
indyarocks / secrets.yml
Last active May 18, 2017 18:28
Secrets.yml for Rails DynamoDB Configuration
# Common configuration for local as well as remote environment
common: &common
asset_host: <%= ENV["RAILS_ASSET_HOST"] %>
# Configuration specific to local environment
local: &local
<<: *common
redis_url: 'redis://localhost:6379'
perform_caching: true # switch to false caching should be disabled in development environment
@indyarocks
indyarocks / railtie.rb
Created May 18, 2017 08:14
Railtie.rb snippet from Rails 4.2.5.2(master)
module Rails
class Railtie
# Removed some code
class << self
private :new
delegate :config, to: :instance
def inherited(base)
unless base.abstract_railtie?
subclasses << base
@indyarocks
indyarocks / mapper.rb
Created May 18, 2017 08:13
Snippet from ActionDispatch::Routing::Mapper from Rails 4.2.5.2
module ActionDispatch
module Routing
class Mapper
URL_OPTIONS = [:protocol, :subdomain, :domain, :host, :port]
class Constraints
# code here
end
class Mapping
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"
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