Skip to content

Instantly share code, notes, and snippets.

Adrian Duyzer adriand

Block or report user

Report or block adriand

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View Person.rb
# sure, 'class Tweep' would work too, but no way in hell are we going down that road.
class Person
include DataMapper::Resource
property :id, Serial
property :screen_name, String, :unique_index => true
property :name, String
property :profile_image_url, String
# storing these will allow us to rank this person against other users
# if we wish, we can also use these as cached results from Twitter if the user
View form.haml
/ Although from the user's perspective we are requesting a "username", we'll use Twitter's nomenclature
/ for this field and call it screen_name in our code instead. By using the same names as the API you
/ interact with, you decrease the cognitive overhead of dealing with the API.
%form{:action => "/value", :method => "post"}
Enter your Twitter username:
%input{:name => "screen_name", :type => "text"}
%input{:type => "submit", :value => "Submit"}
View controlled.rb
post '/value' do
screen_name = params[:screen_name]
if screen_name && screen_name != ""
@info = Twitter.get('/1/users/show.json', :query => { :screen_name => screen_name })
if @info['error']
redirect "/?failure=There was an error retrieving your account information. Twitter says: #{info['error']}."
else
# Since we've now successfully retrieved information on a user account, we'll either look up or save this user in our
# database.
person = Person.first(:screen_name => screen_name)
View progress_statements.rb
def progress_statements
[
["Evaluating Followers", "Questionable"],
["Analyzing Retweet Recursion Depth", "Substantial"],
["Syntax, Grammar and Vocabulary Analysis", "Seventh grade"],
["Determining Mediated Collective Influence", "Fourth degree"],
["Resolving Social Matrix Lattices", "Semi-entwined"],
["Reticulating Splines", "Bezier"]
]
end
View insult.rb
def follower_insult(followers)
case followers
when 0..100 then ["Hopefully you've got more friends offline, though we're not counting on it."]
when 101..1000 then ["That's like, what, a small village? A hamlet? Way to escape the farmstead, chief.", "And you thought you'd get more popular once you finished high school."]
else ["Proof positive that large numbers of people can be very, very wrong.", "Lemmings."]
end.sort_by { rand }.first
end
View zencoder.rb
class Zencoder
include HTTParty
headers "Accept" => "application/json", "Content-Type" => "application/json"
base_uri 'https://app.zencoder.com/api'
default_params :api_key => "YOUR-API-KEY-GOES-HERE"
format :json
attr_accessor :job_id
attr_reader :errors
attr_reader :output_url
View video.rb
class Video < ActiveRecord::Base
validates_presence_of :title
named_scope :finished, :conditions => { :encoded_state => "finished" }
has_attached_file :video,
:url => ":class/:id/:style/:basename.:extension",
:path => ":class/:id/:style/:basename.:extension",
:storage => :s3
validates_attachment_presence :video
View videos_controller.rb
# don't forget to turn off forgery protection:
# protect_from_forgery :except => [:swfupload, :encode_notify]
# if you're not using swfupload, code like this would be in your create and update methods
# all you really need to ensure is that after @video.save, you run @video.encode!
def swfupload
@video = Video.new(params[:video])
@video.swfupload_file = params[:Filedata]
# if we're in production, test should be nil but otherwise we're going to want to encode
# videos using Zencoder's test setting so we're not spending cash to do so
View add_video_fields.rb
class AddVideoFields < ActiveRecord::Migration
def self.up
add_column :videos, :thumbnail_file_name, :string
add_column :videos, :thumbnail_content_type, :string
add_column :videos, :thumbnail_file_size, :integer
add_column :videos, :thumbnail_updated_at, :datetime
add_column :videos, :video_file_name, :string
add_column :videos, :video_content_type, :string
add_column :videos, :video_file_size, :integer
add_column :videos, :video_updated_at, :datetime
View paperclip_defaults.rb
module Paperclip
# The Attachment class manages the files for a given attachment. It saves
# when the model saves, deletes when the model is destroyed, and processes
# the file upon assignment.
class Attachment
def self.default_options
@default_options ||= {
:url => "/system/:class/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/system/:class/:attachment/:id/:style/:basename.:extension",
You can’t perform that action at this time.