Skip to content

Instantly share code, notes, and snippets.

View thescubageek's full-sized avatar
🤿
Are you for scuba?

Steve Craig thescubageek

🤿
Are you for scuba?
  • @evidation
  • Wilmington, NC
View GitHub Profile
@thescubageek
thescubageek / long_division.rb
Created July 9, 2019 17:30
Long Division Coding Challenge
### BEST SOLUTION I HAVE SO FAR (unrealistic to acheive on first try)
# SUPER DRY'd up way of testing both techniques
def divide(x, y, technique='multiply')
sign = (x < 0 && y > 0 || x > 0 && y < 0) ? -1 : 1
x, y = x.abs, y.abs
return 0 if x == 0 || y == 0 || y > x
return sign * 1 if x == y
i = 1
@thescubageek
thescubageek / bulk_insert_assignment.rb
Created October 22, 2018 16:16
Bulk assign punch item assignments
project_id = 69 # replace with project id
login_ids = [332, 331, 133, 267, 119, 249, 251, 250] # replace with assignee login ids
batch_size = 300 # replace with batch size
pi_ids = PunchItem.where(project_id: project_id).where("deleted_at IS NULL").pluck(:id)
already_used_ids = PunchItemAssignment.where(punch_item_id: pi_ids).pluck(:punch_item_id)
pi_ids = (pi_ids - already_used_ids).shuffle
idx = 0
PunchItem.includes(:punch_item_assignments).where(id: pi_ids).find_each(batch_size: batch_size) do |pi|
@thescubageek
thescubageek / steem_post_broadcaster.rb
Last active March 22, 2018 22:07
SteemPostBroadcaster
class SteemPostBroadcaster
attr_accessor :options
def initialize(opts={})
@options = opts
end
def post!
raise "Post is missing title, body, or tags" unless is_valid_post?
account.post!(post_options) unless ENV['DISABLE_AUTO_POSTING'].to_boolean
@thescubageek
thescubageek / steem_percentage.rb
Last active March 7, 2018 04:07
steem_percentage.rb
module SteemPercentage
extend ActiveSupport::Concern
STEEM_MAX_PERCENT = 10000.0
# converts to [0.0-1.0] => percent_decimal(8000) == 0.8
def percent_decimal(num)
num.to_f / STEEM_MAX_PERCENT
end
@thescubageek
thescubageek / steem_post_finder.rb
Last active February 28, 2018 16:28
SteemPostFinder
require 'radiator'
class SteemPostFinder
# the max results we can return for a single API query
BATCH_SIZE = 100
def initialize(tag_name)
@tag_name = tag_name
end
@thescubageek
thescubageek / active_record_in_order.rb
Created October 4, 2017 19:20
WIP: Find and return ActiveRecord models by attribute according to specific order
Widget.where(id: child_widget_ids).index_by(&:id).values_at(*child_widget_ids).squish
@thescubageek
thescubageek / sort_hub_clients_by_location_count.rb
Last active August 23, 2017 17:47
HUB: Sort Clients by Location Count
RISK_FACTORS = {
"Senior-Living": 32,
"Apartments": 15,
"Self-Storage": 20,
"Veterinarian": 9
}.freeze
def clients_by_vertical_and_location_count(vertical, g5_internal=false)
Client.where(vertical: vertical)
.where(g5_internal: g5_internal)
@thescubageek
thescubageek / serialize_and_upload_bad_websites.rb
Created March 16, 2017 16:45
write bad duplicate websites to s3
dups = Website.all.inject({}) { |h,w| h[w.owner_id] ||= 0; h[w.owner_id] += 1; h }.reject! { |k,v| v <= 1 }
bad_webs = Website.where(owner_id: dups.keys).all.sort_by(&:name)
bad_files = bad_webs.inject({}) do |h, w|
file_name = "#{w.id}-#{w.name.parameterize}"
mgr = Saves::WebsiteSavesManager.new("steve.craig@getg5.com", w.owner)
file_path = mgr.send(:create_save_file, file_name)
s3 = S3Bucket.new(w.owner, {s3_folder: 'websites-saves'})
file = s3.upload(file_path, file_name)
h[w.id] = { owner_id: w.owner_id, file: s3.bucket_file_url(file_name) }
@thescubageek
thescubageek / find_phone_widgets_with_phone_number_values.rb
Last active March 4, 2017 14:34
finds phone number widgets with values for the phone_number field
widgets = Widget.includes(:garden_widget, :settings).by_name('Phone Number').all
settings = widgets.map { |w| w.settings.where_name('phone_number').all }.squish.select { |s| s.value.present? && && s.value != "{{location_phone_number}}" }
ret = settings.inject("") { |str, s| str << "- #{s.website.name} (#{s.website.owner.urn}) - #{s.website.status}): #{s.value}\n"; str }
puts "#{Client.client.name} (#{Client.client.urn}):\n#{ret}\n" unless ret.blank?
@thescubageek
thescubageek / find_multi_website_locations.rb
Created September 19, 2016 19:18
finds locations with multiple websites - CMS
def find_multi_website_locations
Location.all.select { |l| Website.where(owner_id: l.id).count > 1 }
end