-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.rb
More file actions
56 lines (46 loc) · 1.46 KB
/
Copy pathapp.rb
File metadata and controls
56 lines (46 loc) · 1.46 KB
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
52
53
54
55
56
require 'rubygems'
require 'sinatra'
require 'indextank'
require 'yajl'
before do
@docid = "seamonkey#{rand(1122334455)}"
# grab IndexTank credentials from ENV
@js = Yajl::Parser.parse ENV['VCAP_SERVICES']
if @js and @js['indextank-1.0'] then
# grab 0, assuming you have just one provisioned instance of indextank-1.0 ..
creds = @js['indextank-1.0'][0]['credentials']
@itc = IndexTank::Client.new(creds['INDEXTANK_PRIVATE_API_URL'])
# and assume you have just one index.
@index = @itc.indexes(creds["INDEXTANK_INDEX_NAMES"][0])
end
end
after do
if @index then
begin
@index.document(@docid).delete()
rescue e
# could not delete the document .. too bad.
p e
end
end
end
get '/' do
if @index then
# add a document
d = @index.document(@docid)
d.add({:text => "some random text .. #{@docid}"})
# search it
docs = @index.search("random #{@docid}")
# did we find it?
if docs['results'].length == 1 then
# success!
"<h1>This is an IndexTank-powered Cloud Foundry App. </h1> <h2>it just created, searched and deleted #{@docid}</h2>"
else
# fail
[ 500, "something went wrong .. the document is not indexed .. " ]
end
else
# fail
[500, "NO IndexTank provisioned ... #{ENV['VCAP_SERVICES']}"]
end
end