
# See http://snippets.rorbuilder.info/user/ravinder/tag/httparty

require 'rubygems'
require 'sinatra'
require 'net/http'
require 'uri'
require 'open-uri'
require 'json'
require 'pp'

get '/user_timeline' do
  callback = params.delete('callback') # jsonp

  name = params.delete('name')
  count = params.delete('count')
  ounnt ||= 20
  page = params.delete('page')
  page ||= 1

  res = nil;
  Net::HTTP.new('twitter.com').start { |w|
    url = "/statuses/user_timeline/#{name}.json?count=#{count}&page=#{page}"
    pp url
    req = Net::HTTP::Get.new(url)
    res = w.request(req)
  }

  json = res.body
  # pp json

  response = nil
  if callback
    content_type :js
    response = "#{callback}(#{json})"
  else
    content_type :json
    response = json
  end

  response
end

get '/search' do
  callback = params.delete('callback') # jsonp

  query = params.delete('q')
  count = params.delete('count')
  ounnt ||= 20
  page = params.delete('page')
  page ||= 1

  res = nil
  Net::HTTP.new('search.twitter.com').start { |w|
    url = "/search.json?q=#{query}&count=#{count}&page=#{page}"
    pp url
    req = Net::HTTP::Get.new(url)
    res = w.request(req)
  }

  json = res.body
  # pp json

  response = nil
  if callback
    content_type :js
    response = "#{callback}(#{json})"
  else
    content_type :json
    response = json
  end

  response
end
