Skip to content

Instantly share code, notes, and snippets.

@apneadiving
apneadiving / bing_locations.csv
Last active Sep 22, 2020
bing_locations.csv
View bing_locations.csv
We can't make this file beautiful and searchable because it's too large.
Location Id,Bing Display Name,Location Type,Replaces,Status,AdWords Location Id
1,Albanie,Country,,Active,2008
2,Antarctique,Country,,Active,2010
3,Algérie,Country,,Active,2012
4,Samoa américaines,Country,,Active,2016
5,Andorre,Country,,Active,2020
6,Angola,Country,,Active,2024
7,Antigua-et-Barbuda,Country,,Active,2028
8,Argentine,Country,,Active,2032
View refactored_version.rb
class ApplyProductStateOrder
def initialize(status_change_order)
@status_change_order = status_change_order
end
def call
with_valid_state_change do
ActiveRecord::Base.transaction do
product.send(transition)
product.save!
View initial_implementation.rb
class ApplyProductStateOrder
def initialize(status_change_order)
@status_change_order = status_change_order
end
def call
ActiveRecord::Base.transaction do
state_change = update_product
status_change_order.update_timestamps(state_change)
View 1_Gemfile
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem 'twilio-ruby'
gem 'dotenv'
gem 'waterfall'
@apneadiving
apneadiving / example.md
Last active Mar 1, 2018
Date range operations
View example.md
range1 = ::Scheduling::DateRange.new(1.day.ago..Time.now)
range2 = ::Scheduling::DateRange.new(2.days.ago..Time.now)

range1.exclusion(range2) => []
range2.exclusion(range1) => [::Scheduling::DateRange.new(2.days.ago..1.day.ago)]
View params_slicer.rb
class ParamsSlicer
def initialize(params, *whitelist)
@params = params
@nested_whitelist = whitelist.extract_options!
@whitelist = whitelist
end
def call
params.slice(*whitelist).tap do |result|
nested_whitelist.each do |k, v|
View custom_flatten.rb
# test utility for the script
def assert(actual, expected)
if actual == expected
puts "ok #{expected}"
else
puts "ko, you expected: #{expected} but got #{actual}"
end
end
# first implementation, more functional programming friendly, pure method
View credit_user.rb
class CreditUser
include Waterfall
def initialize(user:, cents:)
@user, @cents = user, cents
end
def call
# credit logic goes here and dams on error
end
View create_user_from_invitation.rb
class CreateUserFromInvitation
include Waterfall
def initialize(invitation)
@invitation = invitation
end
def call
chain { build_user }
when_falsy { user.save }
View accept_invitation.rb
class AcceptInvitation
include Waterfall
include ActiveModel::Validations
CENTS_PAID_FOR_AFFILIATION = 100
validate :ensure_invitation_not_accepted
def initialize(invitation)
@invitation = invitation