Skip to content

Instantly share code, notes, and snippets.

View watson's full-sized avatar

Thomas Watson watson

View GitHub Profile
@watson
watson / applestore.rb
Created February 9, 2010 15:22
This script polls the Apple Store every 4 seconds to see if it returns a good HTTP status code. When this happens, it uses your computer speakers to say "Hurray. The Apple Store is now open for business" (note: the sound only works on OS X). It works beca
require 'open-uri'
check_every = 4 # seconds
url = "http://store.apple.com/us"
check_again = true
print "Checking #{url}: "
while check_again
begin
module MessageType
MMS = "mms"
SMS = "sms"
end
class MessageType
VALID_TYPES = [:sms, :mms]
class << self
def method_missing(method, *args, &block)
@watson
watson / gateway.rb
Created March 22, 2010 22:35
Hurtig pseudo kode for håndtering af ind- og udgående beskeder fra og til flere forskellige gateways
module Gateway
UNWIRE = 'unwire'
FOO = 'foo'
BAR = 'bar'
class << self
def all
[UNWIRE, FOO, BAR]
end
require 'sketchup'
module MyPlugin
class Helper
class << self
LENGTH_TEXT_SYMBOL_DICT = {
:inches => '"',
:feet => "'",
:millimeters => "mm",
:centimeters => "cm",
@watson
watson / work_processes_controller.rb
Created September 29, 2010 11:59
Show action in work_processes_controller.rb
def show
@work_process = @project.work_processes.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @work_process.to_xml(:include => [:project,
:companies,
:equipment,
:inspections,
:material_packages,
@watson
watson / html5_audio.js
Created October 25, 2010 22:31
Get a HTML5 audio element to replay without loading it from the server again
var audio = document.getElementById('my_audio');
audio.currentTime = 0;
audio.play();
@watson
watson / html5_audio.html
Created October 25, 2010 23:22
Testing Audio in Safai...
<!DOCTYPE html>
<html>
<head>
<title>HTML5 Audio Demo</title>
</head>
<body>
<h1>HTML5 Audio Demo</h1>
<audio controls preload="auto" autobuffer>
<source src="http://www.vorbis.com/music/Epoq-Lepidoptera.ogg" />
<source src="http://zack1.e-technik.tu-ilmenau.de/%7Edoering/mdstudio/stingray/Stingray.mp3" />
@watson
watson / application_controller.rb
Created October 29, 2010 17:13
A way to accept the same XML input as model.to_xml generates as output (see http://stackoverflow.com/questions/4052677/rails-nested-resources-input-vs-output-format-inconsistency for details)
class ApplicationController < ActionController::Base
private
def fix_nested_attribute_structure(data = nil, model = nil)
unless data
# guess data based on current controller
data = params[self.class.to_s.sub('Controller', '').singularize.underscore.to_sym]
end
unless model
# guess model based on current controller
@watson
watson / multiple-s3-swf-upload-plugin-objects.html
Created November 23, 2010 13:50
Showing issue with using more than one instance of s3-swf-upload object on the same webpage. In that case only the last Flash object gets shown. For details see bug report https://github.com/nathancolgate/s3-swf-upload-plugin/issues/#issue/21 - For this t
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="/javascripts/s3_upload.js" type="text/javascript"></script>
<script type="text/javascript">
var s3_swf_1_object = s3_swf_init('s3_swf_1', { buttonWidth: 100,
buttonHeight: 30,
buttonUpPath: '/flash/s3_up_button.gif',
@watson
watson / file_resource.rb
Created January 27, 2011 11:35
Example Rails model using Paperclip and storing files on S3. This shows how to correctly set a custom header and saving the file in the correct location after it has been directly uploaded to from the users browser.
class FileResource < ActiveRecord::Base
has_attached_file :attachment,
:storage => :s3,
:bucket => ENV['S3_BUCKET'],
:s3_credentials => { :access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET'] },
:path => 'files/:id/:filename',
:url => '/files/:id/:filename'