Skip to content

Instantly share code, notes, and snippets.

@dsasse07
dsasse07 / send_example.rb
Created December 28, 2020 23:19
Basic send method examples
“hello”.capitalize #=> “Hello”
“hello”.send(“capitalize”) #=> “Hello”
@dsasse07
dsasse07 / abstraction_to_module.rb
Created December 28, 2020 23:28
Refactoring and abstracting a method to a module for use between multiple classes
module GenericHelpers
def make_hash_by_attribute(array, attribute)
array.each_with_object({}) do |array_item, new_hash|
new_hash[array_item.send("#{attribute}")].nil? ? new_hash[array_item.send("#{attribute}")] = 1 : new_hash[array_item.send("#{attribute}")] += 1
end
end
end
@dsasse07
dsasse07 / module_inclusion.rb
Last active December 28, 2020 23:32
Calling refactored method from a module
class Vhs
include GenericHelpers
extend GenericHelpers
def self.count_vhs_by_rentals
make_hash_by_attribute(Rental.all, "vhs")
end
def self.count_vhs_by_client
make_hash_by_attribute(Rental.all, "client")
@dsasse07
dsasse07 / AR_group_and_count.rb
Created December 28, 2020 23:35
ActiveRecord group by count
Rental.group(:vhs).count #=>
#{<Vhs:0x00007f8d5e2bf0f0 id: 17, serial_number: "MAX-luqovgdk8e7tl", movie_id: 9 => 1,
#<Vhs:0x00007f8d4f81a4a0 id: 29, serial_number: "BABA-t6k24krzqclts", movie_id: 13 =>2,
#<Vhs:0x00007f8d4f854538 id: 11, serial_number: "BABA-begvxe7ai0rc6", movie_id: 13 =>3}
@dsasse07
dsasse07 / literally_defined_methods.rb
Last active December 29, 2020 22:24
Example of literally defined aggregate methods for use in blog post.
def self.count_vhs_by_rentals
Rental.all.each_with_object({}) do |rental, vhs_hash|
vhs_hash[rental.vhs].nil? ? vhs_hash[rental.vhs] = 1 : vhs_hash[rental.vhs] += 1
end
end
#=> {<Vhs:0x00007f8d5e2bf0f0 id: 17, serial_number: "MAX-luqovgdk8e7tl", movie_id: 9 => 1,
#<Vhs:0x00007f8d4f81a4a0 id: 29, serial_number: "BABA-t6k24krzqclts", movie_id: 13 =>2,
#<Vhs:0x00007f8d4f854538 id: 11, serial_number: "BABA-begvxe7ai0rc6", movie_id: 13 =>3}
def self.count_vhs_by_client
class Client
include GenericHelpers
extend GenericHelpers
def self.count_clients_by_age
make_hash_by_attribute(Client.all, "age")
end
end
@dsasse07
dsasse07 / twilio_sms_snippet.rb
Created January 10, 2021 19:54
Twilio Send SMS code snippet from Twilio CodeExchange
require 'twilio-ruby'
account_sid = 'ACcd207c4be71151a3f50ef05c79e865d5'
auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyy'
client = Twilio::REST::Client.new(account_sid, auth_token)
from = '+15551234567' # Your Twilio number
to = '+15555555555' # The mobile number the text will be sent to
client.messages.create(
@dsasse07
dsasse07 / Twilio_module.rb
Created January 10, 2021 20:24
Incorporating Twilio send SMS code snippet into a module for inclusion in necessary classes.
require 'dotenv/load' #This ruby gem will is what we will be using to secure our Twilio credentials
require 'twilio-ruby'
module TwilioControls
# To set up environmental variables, see http://twil.io/secure
@@account_sid = ENV['TWILIO_ACCOUNT_SID'] #This is used in conjuction with the 'dotenv/load' gem
@@auth_token = ENV['TWILIO_AUTH_TOKEN'] #This is used in conjuction with the 'dotenv/load' gem
@@client = Twilio::REST::Client.new(@@account_sid, @@auth_token)
@dsasse07
dsasse07 / .env
Created January 10, 2021 20:44
Setting up .env for Twilio integration
export TWILIO_ACCOUNT_SID='AC###################################'
export TWILIO_AUTH_TOKEN='################################'
#your account_sid and auth_token can be found on your Twilio console at twilio.com/console
@dsasse07
dsasse07 / password.rb
Created January 10, 2021 21:02
Share password using inherited send_sms module
class Password
belongs_to :group_service
include TwilioControls
def share_password(user, to_phone_number)
body = "#{user.display_full_name} has shared a password!\n
Service Name: #{self.group_service.service.name}
Username: #{self.group_service.service_username}
Password: #{self.password}"
send_sms(to_phone_number, body)