Skip to content

Instantly share code, notes, and snippets.

View StevenJL's full-sized avatar

Steven Li StevenJL

  • Fountain Inc.
  • SF Bay Area
  • 18:33 (UTC -07:00)
View GitHub Profile
class App extends React.Component {
render() {
return (
<div>
<button onClick={this.props.async_update}>Click To Update State After Five Seconds</button>
</div>
);
}
}
@StevenJL
StevenJL / test.rb
Created August 24, 2018 05:02
bullet_gem_demo.rb
# config/environments/test.rb
config.after_initialize do
Bullet.enable = true
Bullet.bullet_logger = true
Bullet.raise = true # raise an error if n+1 query occurs
end
@StevenJL
StevenJL / rapwjj.rb
Last active May 16, 2018 20:37
Replay Attack prevention with JWT JTI
###### Upon successful authentication, generate auth_token and send back to user
jti = Digest::MD5.hexidigest(user.id, rand.to_s, "SUPER_SECRET_KEY")
payload = {
iat: current_time,
sub: current_user.id,
jti: jti,
}
auth_token = JWT.encoded(payload, "SUPER_SECRET_KEY", "HS256")
$redis.sadd("UserValidAuthTokenJTI:#{current_user.id}", jti)
render { auth_token: token }
require "pry"
class Ship
def self.find_by_id(id)
SHIPS.select {|ship| ship.id == id }.first
end
attr_reader :id, :length
def initialize(id:, length:)
@length = length
@StevenJL
StevenJL / roman_numeral.rb
Created April 5, 2018 03:16
Roman Numeral
NUMERAL_MAP = {
'M' => 1000,
'CM' => 900,
'D' => 500,
'CD' => 400,
'C' => 100,
'XC' => 90,
'L' => 50,
'XL' => 40,
'X' => 10,
@StevenJL
StevenJL / two_colorable.rb
Created March 21, 2018 21:50
Two colorable
def alternate(color)
if color == :red
return :blue
else
return :red
end
end
class SameNeighborColorError < StandardError; end
# Suppose the AccountHolder parent class is used
# to simply have an association with an account so
# both corporations and people can have accounts.
class Account < ActiveRecord::Base
belongs_to :account_holder
end
class AccountHolder < ActiveRecord::Base
has_one :account
class Animal < ActiveRecord::Base
def eat
end
end
class Bird < Animal
set_table_name "birds"
def fly
end
class Animal < ActiveRecord::Base
def eat
end
end
class Bird < Animal
def fly
end
end
# Under STI, find the top 10 accounts with the greatest balances
# with just one query.
top_accounts = Account.order(balance: :desc).limit(10)
# Under MTI, we need to query all accounts and sort them in memory:
top_corporate_accts = CorporateAccount.order(balance: :desc).limit(10)
top_sb_accts = SmallBusinessAccount.order(balance: :desc).limit(10)
top_personal_accts = PersonalAccount.order(balance: :desc).limit(10)