Skip to content

Instantly share code, notes, and snippets.

Avatar

Adrian Duyzer adriand

View GitHub Profile
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 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 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 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 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 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 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 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 twitter.rb
# http://httparty.rubyforge.org/
# E.g.: Twitter.get('/1/users/show.json', :query => { :screen_name => "USERNAME" })
class Twitter
include HTTParty
base_uri 'api.twitter.com'
end
View layout.haml
!!!
%html{ :lang => "en" }
%head
%meta{ :content => "text/html; charset=utf-8", "http-equiv" => "Content-Type" }/
%title
= "#{@page_title} &plusmn;" if @page_title
99 Bottles
%link{ :href => "/style.css", :title => "no title", :rel => "stylesheet", :media => "screen", :type => "text/css", :charset => "utf-8" }
%script{ :type => "text/javascript", :src => "/jquery.js" }
/ Drop in an erb file that just contains Javascript - that way, any Javascript we need is contained cleanly in a
You can’t perform that action at this time.