Skip to content

Instantly share code, notes, and snippets.

@rdetert
rdetert / time_ago.rb
Last active August 29, 2015 14:07
A quick and dirty helper method to display a short form distance of time in words.
# Usage:
# short_time_in_words(Time.now, 5.seconds.ago) # => "5s"
# short_time_in_words(Time.now, 5.minutes.ago) # => "5m"
# short_time_in_words(Time.now, 5.hours.ago) # => "5h"
# short_time_in_words(Time.now, 55.hours.ago) # => "2d"
#
def short_time_in_words(from_time, to_time)
from_time = from_time.to_i
/*
* Wrote this for the Design Board at equilter.com almost 10 years ago.
* Can't believe it still works and it's still in use!
* Should work for any DHTML browser known to man.
*
* Not so fond memories of getting it to work on MSIE 4 for Mac.
*
* To experience:
* 1. Add items to shopping cart
* 2. Click the Design Board at the top right.
@rdetert
rdetert / abstract_email_controller.rb
Created June 28, 2011 23:54
Generate Email Body from Controller or Observer to Send via Delayed Job
# controllers/shared/abstract_email_controller.rb
module Shared
class AbstractEmailController < AbstractController::Base
include AbstractController::Rendering
include AbstractController::Layouts
include AbstractController::Helpers
include AbstractController::Translation
include AbstractController::AssetPaths
@rdetert
rdetert / omniauth_facebook_login.rb
Created July 2, 2011 22:46
Using Omniauth to Login to Facebook with an Access Token
FB = OmniAuth::Strategies::Facebook.new("nothing")
client = ::OAuth2::Client.new("nothing", "nothing", FB.client_options)
cached_token = "app_id_part|session_id_part|token_part" # or maybe you sent it from the iPhone
access_token = ::OAuth2::AccessToken.new(client, cached_token)
FB.instance_variable_set("@access_token", access_token)
FB.user_info
@rdetert
rdetert / mp3_convert.sh
Created July 15, 2011 09:04
Want to convert your music files to mp3 because you've had the same CD in your car for 3 years? I thought so.
#!/bin/sh
vlc="/Applications/VLC.app/Contents/MacOS/VLC"
ext="mp3"
dest="/Users/ryan/Desktop/mp3/"
for ARG in "$@"; do
NEWFILE="$dest$(basename "$ARG").$ext"
$vlc -I dummy -vvv "$ARG" --sout "#transcode{acodec=mp3,ab=128,channels=2,samplerate=44100}:std{mux=raw,dst=\"$NEWFILE\",access=file}" vlc://quit
done
@rdetert
rdetert / facebook_verify.rb
Created July 20, 2011 09:26
Rake Task to set whether a user has been Facebook verified or not using Omniauth
namespace :facebook do
desc "Check if users have updated their Facebook verification status"
task :update_user_verifiation_status => :environment do
require 'omniauth'
# get all users who are not currently verified
@users = User.joins(:authentications, :profile).where(:"authentications.provider" => "facebook", :"user_profiles.facebook_verified" => 0).select("users.id AS id, authentications.uid, authentications.access_token, user_profiles.facebook_verified")
@users.each do |user|
FB = OmniAuth::Strategies::Facebook.new("nothing")
@rdetert
rdetert / dragonfly.rb
Created May 8, 2013 04:07
How to get Dragonfly working on with a Mongo Datastore using Mongoid 3 on both localhost and Heroku. It's a little bit of a pain and here is the quick and dirty approach without Monkey Patching.
require 'dragonfly'
app = Dragonfly[:images]
mconfig = Mongoid.load!("config/mongoid.yml", Rails.env)
app.datastore = Dragonfly::DataStorage::MongoDataStore.new
app.datastore.configure do |c|
cfg = mconfig['sessions']['default']
@rdetert
rdetert / _form.html.erb
Last active December 18, 2015 19:39
Uses Pikaday and Moment JS jQuery plugins to make a user-friendly DateTime picker to work with Rails
<%= stylesheet_link_tag "pikaday", :media => "all" %>
<%= form_for(@news) do |f| %>
<!-- Generate HTML -->
<%= form_datetime_selector_helper(:obj => f,
:field_name => :publish_date,
:label_name => 'Publish Date',
:date => @news.publish_date).html_safe %>
<% end %>
@rdetert
rdetert / gist:7633417
Last active December 29, 2015 07:08
Sport Statistics
class SportStatistic
include Mongoid::Document
end
class AthleteStatistic < SportStatistic
end
class SoccerAthleteStatistic < AthleteStatistic
field: goals
@rdetert
rdetert / app_annie_sales_api.rb
Created January 5, 2014 23:58
An example of how to use the App Annie API to get sales using Ruby.
require "net/https"
require "uri"
require "date"
ACCOUNT_ID = "000000"
APP_ID = "000000000"
API_KEY = "0000000000000000000000000000000000000000"
yesterday = (Date.today.to_time - 1).strftime("%Y-%m-%d")