-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.rb
49 lines (38 loc) · 1.12 KB
/
github.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
require 'goliath'
require 'nokogiri'
require 'em-synchrony/em-http'
require 'open-uri'
require 'tire'
module Indexmind
class Indexmind::Doc
include Tire::Model::Persistence
def self.create_index
@index = Tire::Index.new('entries')
@index.delete
@index.create
@index.store :body => "Let's do it the old way!", :uri => "http://www.otra.com"
end
def self.store(url, page)
@index.store :body => page.text.split(" "), :uri => url
@index.refresh
end
end
class Github < Goliath::API
use Goliath::Rack::Params # parse query & body params
use Goliath::Rack::Formatters::JSON # JSON output formatter
use Goliath::Rack::Render # auto-negotiate response format
use Goliath::Rack::Validation::RequiredParam, {:key => 'url'}
Doc.create_index
def response(env)
url = params['url']
page = Nokogiri::HTML(open(url))
# Filter which tags you want to extract(h1, p....)
index(url, page)
[200, {}, page.class]
end
private
def index(url, page)
Indexmind::Doc.store(url, page)
end
end
end