Skip to content

Instantly share code, notes, and snippets.

View billyyarosh's full-sized avatar

Billy Yarosh billyyarosh

View GitHub Profile
@billyyarosh
billyyarosh / di.rb
Last active December 9, 2021 05:17
DI Example
def initialize(gateway_service: SongGatewayService.new)
@gateway_service = gateway_service
end
@billyyarosh
billyyarosh / song_service_spec.rb
Last active December 9, 2021 15:59
Song Service Spec w/ Spies
RSpec.describe SongService do
require 'spec_helper'
subject { SongService.new(gateway_service: gateway_service) }
let(:song) { Song.new(name: 'Seek Up', artist: 'Dave Matthews Band', genre: 'Rock') }
context 'using spies' do
let(:gateway_service) do
spy = spy("gateway_service")
@billyyarosh
billyyarosh / song_service_spec.rb
Last active December 9, 2021 04:45
Song Service Spec with Double
RSpec.describe SongService do
require 'spec_helper'
subject { SongService.new(gateway_service: gateway_service) }
let(:song) { Song.new(name: 'Seek Up', artist: 'Dave Matthews Band', genre: 'Rock') }
context 'using doubles' do
let(:gateway_service) do
stub = double("gateway_service")
@billyyarosh
billyyarosh / song_library_spec.rb
Last active December 9, 2021 15:58
Dependency Injection w/ Mocks
RSpec.describe SongService do
require 'spec_helper'
subject { SongService.new(gateway_service: gateway_service) }
let(:song) { Song.new(name: 'Seek Up', artist: 'Dave Matthews Band', genre: 'Rock') }
context 'create_song using mocks' do
class MockSongGatewayService < SongGatewayService
def create_song(song)
@billyyarosh
billyyarosh / song_gateway_service.rb
Created December 9, 2021 04:40
Song Gateway Service Class
class SongGatewayService
def create_song(song_json)
uri = URI('https://music.com/songs')
res = Net::HTTP.post(uri, song_json.to_json)
unless res.is_a?(Net::HTTPSuccess)
nil
end
res.body
end
@billyyarosh
billyyarosh / song_service.rb
Created December 9, 2021 04:39
Song Service class
class SongService
attr_reader :gateway_service
def initialize(gateway_service: SongGatewayService.new)
@gateway_service = gateway_service
end
# Creates a song
# @param [Song] song
@billyyarosh
billyyarosh / song.rb
Last active December 9, 2021 05:16
Song class
class Song
include ActiveModel::Model
include ActiveModel::Attributes
include ActiveModel::Serializers::JSON
attr_accessor :name, :artist, :genre, :id
validates_presence_of :name, :artist
def attributes=(hash)
@billyyarosh
billyyarosh / serverless_print_api.js
Last active January 22, 2022 18:01
Example using chrome-aws-lambda layer with serverless to print PDF page.
const chromium = require('chrome-aws-lambda');
module.exports.handler = async (event) => {
let browser = null;
try {
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless
@billyyarosh
billyyarosh / utc_to_localtime.rb
Last active January 3, 2020 17:10
Ruby convert UTC to local time zone
require 'Time'
utc_time_str = '2020-01-02T18:16:35+0000'
# Outputs UTC time.
Time.parse(utc_time_str)
=> 2020-01-02 18:16:35 +0000
# Outputs system timezone. Also can use to_time
Time.parse(utc_time_str).to_localtime
=> 2020-01-02 13:16:35 -0500
@billyyarosh
billyyarosh / EventsReceivingSupervisor.rb
Last active August 29, 2015 14:02
Celluloid Stomp Worker failing due to passing of current actor.
class EventsReceivingSupervisor < Celluloid::SupervisionGroup
include Celluloid::Logger
trap_exit :work_exited
def start_work(queue_name, queue_uri)
supervise_as :event_listener, StompWorker, current_actor, queue_name, queue_uri
Celluloid::Actor[:event_listener].connect
end