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 / 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
@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 / 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 / dynamodb_client.rb
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 / 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 / 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 / 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-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-hash.rb
Created May 21, 2017 19:05
Redis HASH data type
127.0.0.1:6379> HSET my_hash key1 1 # Initializes a HASH my_hash with key-value key1: 1
(integer) 1
127.0.0.1:6379> HSET my_hash key2 value2 # Sets another key-value pair key2: value2 in my_hash
(integer) 1
127.0.0.1:6379> HGET my_hash key1 # GET the data for key1 in my_hash
"1"
127.0.0.1:6379> HGET my_hash key2 # GET the data for key2 in my_hash
"value2"
127.0.0.1:6379> HGETALL my_hash # GET the complete data in my_hash
1) "key1"
@indyarocks
indyarocks / redis-zset.rb
Created May 21, 2017 19:37
Redis ZSET Data type
127.0.0.1:6379> ZADD class1:math 98 Tithi 50 Raju 80 Andrew # Create a new class 1 math score SET.
(integer) 3 # Add three members with their respective scores
127.0.0.1:6379> ZRANGEBYSCORE class1:math 0 10 # Fetch students scored between 0 to 10. No Student scored between 0 to 10
(empty list or set)
127.0.0.1:6379> ZRANGEBYSCORE class1:math 0 60 # Fetch students scored between 0 to 60. Raju
1) "Raju"
127.0.0.1:6379> ZRANGEBYSCORE class1:math 0 60 WITHSCORES # Fetch students with scores who scored between 0 to 60. Raju
1) "Raju"
2) "50"
127.0.0.1:6379> ZRANGE class1:math 0 10 WITHSCORES # Fetch students from ZSET class1:math with position 0 to 10.