#!/usr/bin/env ruby require 'camping' require 'camping/session' require 'active_support' Camping.goes :Server module Server::Models class User @@id = 1 attr_accessor :id, :name def initialize(name) @id = @@id @@id += 1 @name = name end end class Status @@id = 1 attr_accessor :id, :text, :user, :created_at def initialize(text, created_at=Time.now) @id = @@id @@id += 1 @text = text @created_at = created_at @user = User.new('mtl_on_rails') end @@statuses = (1..20).collect do |i| Status.new("A truly fake message \##{i}", i.hours.ago) end def self.find_all @@statuses.reverse[0, 20] end def self.create(status) s = Status.new(status) @@statuses << s s end end end module Server::Controllers class Index < R '/' def get render :about end end class Timeline < R '/statuses/user_timeline/(.*).json', '/statuses/public_timeline.json' def get(*args) sleep 3 # Twitter is slow @statuses = Status.find_all #@headers['Content-Type'] = 'text/x-json' render :timeline end end class Update < R '/statuses/update.json' def post sleep 2 # Twitter is slow @s = Status.create(input.status) #@headers['Content-Type'] = 'text/x-json' render :status end end end module Server::Views def timeline "[#{@statuses.collect { |s| _status(s) }.join(',')}]" end def status _status @s end def _status(status) %Q({"text":"#{status.text}","id":#{status.id},"user":{"name":"#{status.user.name}","profile_image_url":"http:\/\/assets3.twitter.com\/images\/default_image.gif?1184377318","description":null,"location":null,"url":null,"screen_name":"#{status.user.name}","id":#{status.user.id},"protected":false},"created_at":"#{status.created_at}"}) end def about html do body do h1 'Hi!' p "I'm a fake Twitter server" h2 'I can fake ...' ul do li { a 'Public timeline', :href => R(Timeline) } li { a 'User timeline', :href => R(Timeline, :bob) } li { a 'Update status', :href => R(Update) } end end end end end