Skip to content

Instantly share code, notes, and snippets.

View stratigos's full-sized avatar
🧑‍🔬
Building the Internet

Todd stratigos

🧑‍🔬
Building the Internet
View GitHub Profile
@stratigos
stratigos / number_weekdays_in_year_to_today.rb
Created July 8, 2016 15:53
Ruby: get the number of weekdays in the last year up to today.
( (1.year.ago.to_date)..(Date.today) ).select{ |d| (1..5).include? d.wday }.size.to_f
@stratigos
stratigos / ruby_memory_profile_example.rb
Last active January 9, 2017 21:40
Example Tool for Profiling Memory Use in Ruby Scripts and Applications
#################################
# GEMFILE: gem 'get_process_mem'
#################################
require 'get_process_mem'
def profile_mem(description=nil)
mb = GetProcessMem.new.mb
puts "\n\n---- #{ description } - MEMORY USAGE(MB): #{ mb.round } ----\n\n"
end
@stratigos
stratigos / .rspec
Created January 9, 2017 21:39
RSpec Configuration
--color
--require spec_helper
@stratigos
stratigos / rspec_shared_example_with_shoulda_matchers_for_booleans.rb
Created April 19, 2018 16:46
RSpec Boolean Matcher Shared Example with Shoulda Matchers
# Shared specs for models with the `:validates_inclusion_of` validators, where
# Boolean values are specified. This is for use cases where Booleans are used
# in non-null DB columns, and validation errors need to be returned to actors
# attempting to store a NULL value (e.g., a IoT unit's firmware uploading a
# NULL value for an On/Off sensor reading).
# Additionally, `shoulda` will complain if `validate_inclusion_of` is used
# with the Booleans, and instead of turning off warnings, its best to just
# approach these specs differently. See
# [this github comment](https://github.com/thoughtbot/shoulda-matchers/issues/922#issuecomment-225752673)
# for more info.
@stratigos
stratigos / rspec_soft_delete_shared_example.rb
Created April 19, 2018 16:50
RSpec Matcher for ActsAsParanoid / Paranoia Active Record Model Specs
# Shared specs for models with the `acts_as_paranoid` features.
RSpec.shared_examples :soft_delete_model do
it { is_expected.to have_db_column(:deleted_at) }
it 'has a scope to select soft-deleted records' do
expect(described_class).to respond_to(:only_deleted)
end
it 'has a scope to select both not-deleted and soft-deleted records' do
@stratigos
stratigos / rspec_validates_numericality_with_shoulda_matchers.rb
Created April 19, 2018 16:54
RSpec Validates Numericality of Active Record Model with Shoulda Matchers
# Shared specs for models with the `:validates_numericality_of` validators.
# Usage: pass a hash of each of the symbolized names of each options for the
# numericality validator. If the option has a value with which to evaluate
# against, add it as the value of that option hash name.
# E.g.:
# `it_behaves_like :validates_numericality_model, :numeric_attr, { greater_than_or_equal_to: 0, only_integer: true }`
RSpec.shared_examples :validates_numericality_model do |attribute_name, options|
it { is_expected.to validate_numericality_of(attribute_name) }
@stratigos
stratigos / rspec_shared_example_defined_constants.rb
Created April 19, 2018 16:56
RSpec Shared Example Matcher for Class Constant Definition
# Shared specs for models with frozen CONST values. Expects two String values
# as parameters.
RSpec.shared_examples :has_const_model do |const_name, const_value|
it { expect(described_class).to be_const_defined(const_name.to_sym) }
it { expect("#{described_class}::#{const_name}".constantize).to be_frozen }
it { expect("#{described_class}::#{const_name}".constantize).to eq(const_value) }
end
@stratigos
stratigos / respec_helper_bearer_token_session_headers.rb
Last active April 19, 2018 17:19
RSpec Helper for Validating Token-based Auth Headers Returned with Request
module RequestSpecHelper::SessionsHelper
# Set single expectation that checks auth headers were returned.
def expect_auth_headers(response)
expect(response.headers.keys).to include('access-token', 'client', 'expiry', 'uid')
end
end
########################################################################
@stratigos
stratigos / rspec_helper_token_auth_sign_in.rb
Last active August 28, 2020 17:23
RSpec Helper for Token-based Authentication Sign-In
# Helpers to assist with the `devise_token_auth` security features, such as
# generation of new authentication tokens for each request.
# @see [devise_token_auth Github issue](https://github.com/lynndylanhurley/devise_token_auth/issues/75)
module RequestSpecHelper::ApiAuthHelper
module Extensions
# Helper method for authenticating a User for each Request type spec.
# Use `devise_token_auth` callback to generate an authentication token
# for each request.
# Call this on a created User at the beginning of a `describe` or `context`
@stratigos
stratigos / clear_swap.sh
Created November 11, 2018 22:34
Clear Swap Space on Ubuntu Linux (14.04)
function clearswap { sudo swapoff -a && sudo swapon -a; }