Skip to content

Instantly share code, notes, and snippets.

View adriand's full-sized avatar

Adrian Duyzer adriand

  • Hamilton, Ontario
View GitHub Profile
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
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
# 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
!!!
%html{ :lang => "en" }
%head
%meta{ :content => "text/html; charset=utf-8", "http-equiv" => "Content-Type" }/
%title
= "#{@page_title} ±" 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
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)
/ 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"}
# 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
require 'rubygems'
require 'ruby-debug'
abort("Supply a twitter screen name like: ruby twitter_info_via_curl.rb tomcreighton") unless ARGV.size > 0
# the -s argument makes curl silent (it will no longer show a progress meter)
# don't forget to append m to your regular expressions if you want the . to match newline characters
user_info = `curl -s http://api.twitter.com/1/users/show.xml?screen_name=#{ARGV[0]}`.match(
/<followers_count>(\d*)<\/followers_count>.*<statuses_count>(\d*)<\/statuses_count>/m)
puts "Followers: #{user_info[1]}"
# When we create our Rackup file, we'll already be requiring RubyGems and Sinatra,
# so we don't require them again if they've already been loaded.
require 'rubygems' unless defined? ::RubyGems
require 'sinatra' unless defined? ::Sinatra
require 'rack' # more on the decision to include this below
require 'dm-core'
require 'haml'
require 'ruby-debug'
# If you want changes to your application to appear in development mode without having to
def past(interval)
interval.inject({}) { |interval,(k,v)| interval[k] = v.abs * -1; interval }
end