Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View gusrub's full-sized avatar
🏠
Working from home

Gustavo Rubio gusrub

🏠
Working from home
View GitHub Profile
@gusrub
gusrub / test.rb
Last active August 29, 2015 14:13
class Person
attr_accessor :first_name
attr_accessor :last_name
attr_accessor :dob
attr_reader :age
def initialize(first_name, last_name, dob)
self.first_name = first_name
@gusrub
gusrub / rubodiff.sh
Last active March 13, 2017 23:09
Check git changed files for ruby linter (rubocop)
#!/bin/bash
CUR_PATH=$1
if [ -z $CUR_PATH ]
then
CUR_PATH=`pwd`
fi
cd $CUR_PATH
git diff --name-only | xargs rubocop --format simple
@gusrub
gusrub / mysql_bak.sh
Created March 27, 2017 22:31
MySql quick dump
#!/bin/bash
while getopts ":h:u:w:d:p:" opt; do
case $opt in
h) DB_HOST="$OPTARG"
;;
u) DB_USER="$OPTARG"
;;
w) DB_PASSWORD="$OPTARG"
;;
@gusrub
gusrub / mvc_controller.rb
Created March 4, 2018 02:05
MVC services demo 1
class UsersController < ApplicationController
def create
if @user.save
render json: @user, status: 200
else
render json @user.errors, status: 400
end
end
end
@gusrub
gusrub / mvc_model.rb
Created March 4, 2018 02:11
MVC model
class User < ApplicationRecord
validates :first_name, presence: true
validates :last_name, presence: true
validates :email, presence: true
validates :token, presence: true
end
@gusrub
gusrub / mvc_model_2.rb
Created March 4, 2018 02:23
MVC model 2
class User < ApplicationRecord
validates :first_name, presence: true
validates :last_name, presence: true
validates :email, presence: true
validates :token, presence: true
before_create :generate_token
after_create :send_welcome_email
private
@gusrub
gusrub / users_helper.rb
Created March 4, 2018 02:52
Users helper
module UsersHelper
def generate_token(user)
uri = URI("https://example.com/#{user.email}")
response = Net::HTTP.get_response(uri)
if response.code == '200'
return JSON.parse(response.body)[:token]
end
end
def send_welcome_email(user)
@gusrub
gusrub / mvc_controller_3.rb
Created March 4, 2018 02:55
MVC controller 3
class UsersController < ApplicationController
include UsersHelper
def create
if params[:token].blank?
@user.token = generate_token(@user)
end
if @user.save
send_welcome_email(@user) if params[:send_email]
render json: @user, status: 200
class TokenProvider
def self.generate(email)
uri = URI("https://example.com/#{email}")
response = Net::HTTP.get_response(uri)
if response.code == '200'
return JSON.parse(response.body)[:token]
end
end
end
# service name hints that it does
class CreateUserService
# default attributes that all services should have
attr_reader :errors, :messages, :output
# custom attributes for what this service does
attr_reader :user, :send_email
# we receive all parameters in the initialization
def initialize(user:, send_email: true)