Skip to content

Instantly share code, notes, and snippets.

View pduey's full-sized avatar

Paul Duey pduey

  • Teladoc Health
  • Brooklyn, NY
View GitHub Profile
@pduey
pduey / gcs-cors.rb
Last active July 31, 2020 19:01
Google Cloud Service CORS Configuration in the Rails Console
# In order to use Rails Active Storage Direct Upload to a cloud service, e.g., using Rails
# Action Text rich text editor image upload button, the cloud service will need CORS
# configured. The GCS CORS doc at https://cloud.google.com/storage/docs/configuring-cors
# describes 3 ways of doing it. I wanted to use the Ruby code method, since I already had
# GCS configured for my app and could avoid configuring some other utility. The key was
# getting a handle on a client from my rails app. So, assuming you have already uploaded
# something via your app (not via direct upload):
client = ActiveStorage::Blob.last.service.send :client
bucket = client.buckets.first
bucket.update do |bucket|
@pduey
pduey / luhn_validator.rb
Created March 12, 2013 14:36
Ruby method that implements luhn algorithm to validate a number, used by many credit card companies.
#!/usr/bin/env ruby
# Save as file, e.g., luhn_validator.rb, make executable, then: ./luhn_validator.rb 'number'
DIGIT_REDUX = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9] # simple lookup instead of doubling and summing if > 9
def valid_luhn_number?(number)
digits = number.to_s.split(//).map{|d| d.to_i}
digits.reverse!.each_index{|i| digits[i] = DIGIT_REDUX[digits[i]] if i.odd?}.reduce(:+) % 10 == 0
end
@pduey
pduey / patch_savon.rb
Last active October 13, 2015 08:58
Overwrite ruby method in Rails development only, calls original method with *args, &block
# I needed to overwrite (not override) the Savon::Client.request method in my development
# environment only, and only for 1 type of request that happens to hit a host I can access
# only in production. For all other requests, it should invoke the original Savon::Client.request.
# I found it a little tricky since it takes optional args and a block. Here's what worked, which
# I learned from these two stackoverflow answers
# http://stackoverflow.com/questions/4470108/when-monkey-patching-a-method-can-you-call-the-overridden-method-from-the-new-i
# http://stackoverflow.com/questions/89650/how-do-you-pass-arguments-to-define-method
# It's maybe similar to how rspec implements the stub method?
Savon::Client.class_eval do
@pduey
pduey / querystring.js
Last active December 12, 2021 12:53
Javascript to add/remove a query string parameter/value pair
// pduey: a couple of public and private functions to add or remove a query string parameter + value pair.
//
// I like this implementation because it transforms the query string into a coherent hash with unique keys
// and an Array for the values, so it handles repeated query string parameter names, which are meant to be
// interpreted as Arrays, yo. The "private" functions could, of course, be handy for other uses.
//
// The function names include "search" because they are derived from window.location.search.
// Ideally, I'd attach these to the window object, as in, window.location.prototype.
//
@pduey
pduey / 00 Rails autocomplete
Last active December 14, 2020 18:19
sunspot_solr ruby gem with solr autosuggest/autocomplete, JQuery UI autocomplete, and apache HTTP server
Architecture: Rails 3.2, Solr 1.4 with sunspot_solr gem, Apache HTTP server as reverse proxy to
Rails app, Jquery UI
Note: I don't care about the distinction between autocomplete and autosuggest. I am implementing what some
people call autosuggest, and I'm calling it autocomplete.
Given the above existing architecture, I want to add an autocomplete field into my app for a single
attribute on a single model. It needs to be fast and secure (duh). The search target field could have
white space and the search input should allow it, e.g., if I search with "iker's gui" it should return
@pduey
pduey / tabulate.rb
Created April 16, 2012 16:33
Ruby on Rails Tabular Data Ordered Column-wise
class Array
# Converts a one dimensional array into a table with the given number of columns. Optionally transposes
# the result. Suited for output to an HTML table you want sorted vertically instead of horizontally.
# Pads correctly, i.e., each row/column has at most one empty cell. num_columns is the desired number
# of columns in the result, regardless if the table is transposed.
def tabulate(num_columns, transpose=true)
if transpose
num_rows = num_columns
num_columns = ((self.size - 1) / num_columns) + 1