/
web.rb
51 lines (43 loc) · 1.06 KB
/
web.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- encoding: utf-8 -*-
require 'rubygems'
require 'bundler'
Bundler.require
Faraday.default_adapter = :em_synchrony
class JavaScrape < Sinatra::Base
register Sinatra::Synchrony
Connection = Faraday.new do |builder|
builder.use FaradayMiddleware::FollowRedirects, :limit=>3
builder.adapter Faraday.default_adapter
end
get '/' do
haml :index
end
get '/get' do
Connection.get(params[:uri]).body
end
post '/post' do
Connection.post(params[:uri], params.reject{|k| k=="uri"}).body
end
helpers do
def code_mirror_js
<<-EOS
editor = CodeMirror.fromTextArea(document.getElementById("code"), {
mode: 'javascript',
lineNumbers: true,
extraKeys: { 'Alt-R': function() { $("input#run").click(); } },
matchBrackets: true
});
EOS
end
def example
<<-EOS
get("http://engadget.com", function(result) {
$(result).find("h4.post_title a").each(function() {
println($(this).text());
println($(this).attr("href"));
});
});
EOS
end
end
end