Created
May 24, 2012 16:55
-
-
Save coderoshi/2782755 to your computer and use it in GitHub Desktop.
A Sinatra example to turn any site into a JSON service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'sinatra' | |
require 'httparty' | |
require 'nokogiri' | |
require 'json' | |
class HtmlParserIncluded < HTTParty::Parser | |
SupportedFormats.merge!('text/html' => :html) | |
def html | |
Nokogiri::HTML(body) | |
end | |
end | |
class Page | |
include HTTParty | |
parser HtmlParserIncluded | |
end | |
get '/' do | |
"Here there be cat$" | |
end | |
get '/bomb' do | |
content_type :json | |
count = params[:count].to_i | |
count = 5 if count < 1 | |
page = ((rand * 24) + 1).to_i | |
res = Page.get("http://cashcats.biz/page/#{page}") | |
cats = res.css('.photo_img').map{|x| x['src']} | |
cats = cats.sort_by{ rand }[0...count] | |
{ cats: cats }.to_json | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Just deploy these files to Heroku | |
require './app' | |
run Sinatra::Application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source :rubygems | |
gem 'sinatra' | |
gem 'httparty' | |
gem 'nokogiri' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment