Skip to content

Instantly share code, notes, and snippets.

View mrpunkin's full-sized avatar

Bryan Corey mrpunkin

View GitHub Profile
@mrpunkin
mrpunkin / arel_based.rb
Created March 7, 2012 21:47
Using arel to query SQL functions
dc = Arel::Nodes::NamedFunction.new "DATE", [ p[:created_at] ] ## second argument must be an array
## Sub this...
arel.project("DATE(`photos`.`created_at`)")
## For this...
arel.project(dc.to_sql)
arel.to_sql >> "SELECT DATE(`photos`.`created_at`) FROM `photos` INNER JOIN `votes` ON `photos`.`id` = `votes`.`photo_id`"
class ZohoOrder
include ActiveModel::Model
include ActiveModel::Validations::Callbacks
ZohoFields = [
'ID',
'Added_Time'
]
def self.converted_zoho_attributes
@mrpunkin
mrpunkin / Spam.rb
Created June 27, 2018 22:17
ZendeskAPI code to loop through specific tickets and mark them all as spam.
class Spam
require 'zendesk_api'
def self.client
@client ||= ZendeskAPI::Client.new do |config|
config.url = "https://pediment.zendesk.com/api/v2" # e.g. https://mydesk.zendesk.com/api/v2
config.username = "admin@pediment.com"
config.password = "pedb00ks"
config.retry = true
end
{
"height":2448,
"width":3264,
"md5":"8c6e38213c009aa62ccbfed210db7189",
"exif":{
"EXIF ApertureValue":"None",
"Image ExifOffset":"204",
"EXIF ComponentsConfiguration":"YCbCr",
"GPS GPSLatitudeRef":"N",
"GPS GPSAltitudeRef":"0",
@mrpunkin
mrpunkin / Client.php
Last active December 21, 2016 14:27
Fix to re-process header if initial header is 100 Continue in Amazon MWS Fulfillment Outbound Shipment PHP client library
<?php
...
//// Starting @ line 635 of FBAOutboundServiceMWS/Client.php
list($other, $responseBody) = explode("\r\n\r\n", $response, 2);
$other = preg_split("/\r\n|\n|\r/", $other);
list($protocol, $code, $text) = explode(' ', trim(array_shift($other)), 3);
// Re-check for new header after continue header.
@mrpunkin
mrpunkin / exception_notifier.rb
Created September 5, 2013 21:17
Extension for ExceptionNotifier class to ignore "invalid byte sequence" errors.
class ExceptionNotifier
def call(env)
@app.call(env)
rescue Exception => exception
options = (env['exception_notifier.options'] ||= {})
options.reverse_merge!(@options)
unless Array.wrap(options[:ignore_exceptions]).include?(exception.class) or /invalid byte sequence/ =~ exception.message
Notifier.exception_notification(env, exception).deliver
env['exception_notifier.delivered'] = true
@mrpunkin
mrpunkin / routes.rb
Created July 15, 2013 22:05
Preferred method for named routes in a single, non-resource controller?
## Option A
scope "search", :controller => :search do
root :to => 'search#index', :as => 'search'
get :users, :as => "search_users"
get :photos, :as => "search_photos"
end
## Option B
@mrpunkin
mrpunkin / gist:5700075
Created June 3, 2013 18:08
Determine object from restful / polymorphic routes, taking into account the possibility that to_param has been overridden for the given object's class.
cls = controller_name.classify.constantize
paramtest = cls.new
paramtest.attributes.map{|name,val| paramtest[name] = name }
to_param = paramtest.to_param
@object = cls.send("find_by_#{to_param}", params[:id])
arels = [Vote, Comment, Love].collect{|u|
t = u.arel_table
u.select([
t[:created_at].maximum.as("ts"),
t[:photo_id].as("what_id"),
"'Photo' AS what_type"
]).group(t[:photo_id]).arel
}
union = Arel::Nodes::Union.new(arels[0], arels[1])
SELECT photos.id FROM photos
LEFT OUTER JOIN votes ON votes.photo_id = photos.id
LEFT OUTER JOIN flags ON flags.photo_id = photos.id AND flags.type="Love"
LEFT OUTER JOIN comments ON comments.photo_id = photos.id
WHERE photos.user_id = 38218
GROUP BY photos.id;