Skip to content

Instantly share code, notes, and snippets.

HTTP_ERRORS = [
EOFError,
Errno::ECONNRESET,
Errno::EINVAL,
Net::HTTPBadResponse,
Net::HTTPHeaderSyntaxError,
Net::ProtocolError,
Timeout::Error,
Errno::ENETDOWN
]
# frozen_string_literal: true
class UserService
include SessionsHelper
def sigin_user
user = User.find_by_email(@email)
if user.present?
login(user)
response = { success: 'User Logged In successfully.' }
else
set tabstop=2 shiftwidth=2 expandtab
set autoindent
set smarttab
syntax on
set shell=/bin/bash
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
@localhostdotdev
localhostdotdev / factory.rb
Last active April 10, 2019 01:14
preview of a simple factory thing for ruby which I use instead of factorybot / fixtures
class Factory
def self.create(clazz, *args)
build(clazz, *args).tap(&:save!)
end
def self.build(clazz, *args)
"Factory::#{clazz}".constantize.new(clazz, *args).tap(&:process).record
end
def self.create_list(clazz, count, *args)
# frozen_string_literal: true
require 'test_helper'
class SignUpTest < ActionDispatch::IntegrationTest
test 'can user sign up' do
get users_path
assert_response :success
@localhostdotdev
localhostdotdev / using-biquery-ruby-free.md
Created April 13, 2019 12:10
using bigquery with ruby for free

using bigquery with ruby for free

main website asks for credit card, console/web ui just doesn't work.

.env:

@localhostdotdev
localhostdotdev / application-record-sql.rb
Last active April 14, 2019 09:17
ApplicationRecord.sql('select * from users left join accounts on accounts.user_id = users.id and accounts.kind = ?', 'something')
class ApplicationRecord < ActiveRecord::Base
def self.sql(sql, *values)
ActiveRecord::Base.connection.execute(
ActiveRecord::Base.send(:sanitize_sql_array, [sql, *values])
).each.to_a
end
end
users = ApplicationRecord.sql('select * from users')
users_and_accounts = ApplicationRecord.sql('select * from users left join accounts on accounts.user_id = users.id and accounts.kind = ?', 'something')
@localhostdotdev
localhostdotdev / 1-test-struct-hash-open-struct-simple-hash-etc.rb
Last active April 24, 2019 17:46
testing different kind of key-value structures
$struct = Struct.new(:name, :writing?).new("localhostdotdev", true)
$hash = Hash[name: "localhostdotdev", writing?: true]
$openstruct = OpenStruct.new(name: "localhostdotdev", writing?: true)
$hashwithindifferentaccess = HashWithIndifferentAccess[name: "localhostdotdev", writing?: true]
$yourcustomclass = "<insert your class here>"
$simplehash = SimpleHash[name: "localhostdotdev", writing?: true]
def try(code)
puts code
p eval(code, binding)
@localhostdotdev
localhostdotdev / performance of method_missing vs fetch vs [] vs Hash#[].rb
Created April 20, 2019 17:20
performance of method_missing vs fetch vs [] vs Hash#[]
class U
def self.compare(reports, n: 10)
Benchmark.bm do |x|
reports.each do |report|
if report.is_a?(Proc)
x.report { n.times { report.call } }
else
x.report(report.first) { n.times { report.last.call } }
end
end
@localhostdotdev
localhostdotdev / simple-hash.rb
Created April 20, 2019 18:31
Hash based SimpleHash (instead of HashWithIndifferentAccess)
class SimpleHash < Hash
def method_missing(method_name, *args, &block)
if keys.map(&:to_s).include?(method_name.to_s)
if args.empty? && block.nil?
send(:simple_fetch, method_name)
else
raise "can't pass arguments and/or blocks"
end
else
super