Skip to content

Instantly share code, notes, and snippets.

@tehprofessor
tehprofessor / archive.rb
Created March 11, 2012 03:53
Archiving Audio Stream in Ruby
require 'net/http'
require 'uri'
url = URI.parse('http://stream.kpsu.org:8080/listen')
# This thread is used to kill the recording thread after the
# designated amount of time.
timelimit = Thread.new do
# running_time_in_seconds is how many seconds the clip should be
# one hour = 3600 seconds
@tehprofessor
tehprofessor / gist:2128162
Created March 19, 2012 23:08
NewRelic OpenPGP Server Down (Alternative Method)
PGP.net goes down from time to time, and incase it does you can use this to add the NewRelic key:
wget -O - http://download.newrelic.com/548C16BF.gpg | apt-key add -
@tehprofessor
tehprofessor / sinatra_before_filter.rb
Created April 14, 2012 20:07
Sinatra before filter selection using path + http method as qualifier
# As always, I hope I didn't reinvent the wheel or do something stupid... If I have please let me know!
#
# I'm currently using this with async_sinatra; haven't tested it without (sorry!)
#
# Example method usage:
#
# before_filter [[:apost, "/users/?"] do
# require_login
# end
@tehprofessor
tehprofessor / error_pages.rb
Created May 14, 2012 16:53 — forked from corny/error_pages.rb
Custom error pages
#
# Usage:
# class ApplicationController < ActionController::Base
# include ErrorPages
# end
#
require 'active_support/core_ext/array/extract_options'
module ErrorPages
extend ActiveSupport::Concern
@tehprofessor
tehprofessor / object_id.rb
Created June 15, 2012 02:42
bson-1.6.4/lib/bson/types/object_id.rb
# encoding: UTF-8
# --
# Copyright (C) 2008-2011 10gen Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
@tehprofessor
tehprofessor / Image_dimensions_validation_with_carrierwave_vips.rb
Last active December 15, 2015 19:59
Image dimensions validation with carrierwave-vips
require 'vips'
class Photo < ActiveRecord::Base
# Include the VIPS moduel in model so we have access to the `Image` class
include VIPS
# Basic attributes
attr_accessible :image, :image_cache, :crop_x, :crop_y, :crop_w, :crop_h
# mount Carrierwave
mount_uploader :image, PhotoUploader
# Call the minimum_dimensions method on validation
validate :minimum_dimensions
@tehprofessor
tehprofessor / watir_phantomjs_user_agent_settings.rb
Created May 1, 2013 18:28
Configuring Watir to use a custom user agent with the PhantomJS driver. The user agent gem has been deprecated, and this functionality is not documented anywhere (obvious).
require 'watir-webdriver'
capabilities = Selenium::WebDriver::Remote::Capabilities.phantomjs("phantomjs.page.settings.userAgent" => "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36")
driver = Selenium::WebDriver.for :phantomjs, :desired_capabilities => capabilities
browser = ::Watir::Browser.new driver
# Quick test to make sure it's set
browser.goto 'http://www.useragentstring.com/'
browser.textarea(:id => "uas_textfeld").value
@tehprofessor
tehprofessor / model.rb
Created May 9, 2013 03:46
How to determine if an instance was created (e.g. not previously persisted) after it has been saved. Very much use case specific.
class Model < ActiveRecord::Base
attr_accessor :was_new_record
def was_new_record?
@was_new_record == true ? true : false
end
end
@tehprofessor
tehprofessor / objc_method.m
Last active December 18, 2015 00:18
Calling an objective c method in Ruby Motion
[UIBarButtonItem configureFlatButtonsWithColor:[UIColor peterRiverColor]
highlightedColor:[UIColor belizeHoleColor]
cornerRadius:3];
@tehprofessor
tehprofessor / home_screen.rb
Last active December 18, 2015 00:18
No method error when trying to use FlatUIKit's configureFlatButtonsWithColor https://github.com/Grouper/FlatUIKit
# Notes:
#
# FlatUIKit: https://github.com/Grouper/FlatUIKit
#
# Their Example:
# [UIBarButtonItem configureFlatButtonsWithColor:[UIColor peterRiverColor]
# highlightedColor:[UIColor belizeHoleColor]
# cornerRadius:3];
#