Skip to content

Instantly share code, notes, and snippets.

View cgallagher's full-sized avatar

Chris Gallagher cgallagher

View GitHub Profile
@cgallagher
cgallagher / ses.rb
Created March 28, 2011 14:03
Send Emails from db to multiple users via Amazon SES
class MailshotController < ApplicationController
def init
due_emails = EmailQueue.find(:all, :conditions => ["scheduled_at < ? AND is_complete = ?", DateTime.now, false])
for email in due_emails
users = get_users_by_locale(email.locale)
user_emails = get_user_emails(users)
options = {
:from => email.from_email_address,
:to => user_emails,
@cgallagher
cgallagher / paperclip_s3_url_rewriter.rb
Created August 30, 2011 13:05
A piece of code that will help paperclip to detect if the user is browsing your app in HTTPS and serve up S3 Images over the corresponding protocol.
#create this file in app/middleware and then in config/application.rb at the beginning of the application class you need to call it "config.middleware.use "PaperclipS3UrlRewriter"
class PaperclipS3UrlRewriter
def initialize(app)
@app = app
end
def call(env)
status, headers, response = @app.call(env)
if response.is_a?(ActionController::Response) && response.request.protocol == 'https://' && headers["Content-Type"].include?("text/html")
@cgallagher
cgallagher / scroll_top.js
Created March 28, 2012 09:07
Animate scroll top within FB iFrame
function scroll_to_top() {
FB.Canvas.getPageInfo(function (pageInfo) {
var iframe_scroll_y = pageInfo.scrollTop;
var target_div_y = $('#dr_dre').position().top;
if (iframe_scroll_y > target_div_y) {
var animOptions = {
step: function () {
FB.Canvas.scrollTo(0, this.y);
},
@cgallagher
cgallagher / ugc_publish_action.rb
Created April 19, 2012 09:31
Pubish a UGC Image via Bookface
def publish!(fbid, key, shwop=nil, entry=nil)
fb_action = Facebookaction.find_by_key(key)
access_token = Bookface::Base.client_credentials
a_type = fb_action.action_type
url = "http://#{Settings.host_url}/actions/#{fb_action.key}"
if entry.width > 520 and entry.height > 520
puts "image is big enough for user generated attachment"
params = { Settings.facebook.fb_object_ref => url,
"image[0][url]" => entry.entry_asset_url,
# application.rb
def user_is_safari_fucked?
BPLogger.debug(request.env['HTTP_USER_AGENT'])
request.env['HTTP_USER_AGENT'].match(/(AppleWebKit).+Version\/(5\.1\.[4-9])/)
end
def js_redirect_to(url, signed_request=nil)
@cgallagher
cgallagher / action.rb
Created May 3, 2012 11:45
Basic FB Action
class Fbaction < ActiveRecord::Base
class << self
def publish!(fbid)
url = url + "/#{entry.id}"
params = { "<object_ref_here>" => url,
:access_token => access_token
}
res = Bookface::Graph.request("#{fbid}/<namespace_here>:<action_name_here>", params, :method => :post)
end
@cgallagher
cgallagher / davos.rb
Created May 3, 2012 11:40
Basic insert text on image via imagemagick
class Davos
#require 'RMagick'
include Magick
def self.for(user, message)
bottle_composition = create_composition(user, message)
bottle_file = Tempfile.new("composite-for-#{user.fbid}.jpg")
bottle_composition.write(bottle_file.path)
@cgallagher
cgallagher / og.rb
Created June 11, 2012 10:05
OG Endpoint
def show
if request.user_agent.index('facebookexternalhit')
@thing = Thing.find(params[:id])
add_meta :property => 'og:title', :content => @thing.title
add_meta :property => 'og:type', :content => "article"
add_meta :property => 'og:url', :content => @thing.url_to_the_content
add_meta {{:property => 'og:image', :content => @thing.image_url}}
end
end
@cgallagher
cgallagher / match_friends.rb
Created June 12, 2012 11:40
Match friends who are also using an app and see if they are also installed in the db.
def match_friends
# now just find the friends of the user who are also using the app.
begin
res = Bookface::Graph.request("fql", { :q=> "SELECT uid, name, pic_square FROM user WHERE is_app_user AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me())", :access_token => access_token })
rescue
res = nil
end
friend_ids = []
# retrieve friend list from graph and compile a list of friends who are registered - aww friend
unless res.nil? or res["data"].nil?
@cgallagher
cgallagher / milestone.rb
Created June 13, 2012 10:28
Active Admin Image Upload Form
ActiveAdmin.register Milestone do
scope :all, :default => true
scope :global
scope :user_specific
index do
column :title
column :description
column :required_litres
column :is_active