public
Description:
Homepage: http://seekr.es
Clone URL: git://github.com/Hecpeare/seekr.git
Héctor Pérez (author)
Thu Jan 15 09:28:28 -0800 2009
seekr / seekr.rb
100644 44 lines (42 sloc) 1.386 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
# License: GNU Affero GPL, http://www.fsf.org/licensing/licenses/agpl-3.0.html
# Héctor Pérez Arenas, http://hecpeare.net
require 'sinatra'
require 'yaml'
require 'cgi'
get '/' do
  redirect "index.html"
end
get '/*.*' do
  vars=split_and_prepare(path)
  list=YAML.load(IO.read('list.yml'))
 
  case size=vars.size - 1
  when 0
    url="http://www.google.com/search?q=" + vars[0]
  when 1
    #e.g. seekr.es/w/free_software -> http://en.wikipedia.org/wiki/Free_software
    url=list[vars[0]]
    url.gsub!("$1$",vars[1]) if url
  else
    #e.g. seekr.es/w/es/GNU -> http://es.wikipedia.org/wiki/GNU
    url=list[vars[0] + size.to_s]
    size.times{|n| url.gsub!("$#{n+1}$",vars[n+1])} if url
  end
  redirect url ? url.gsub("%3A",":").gsub("%2F","/") : "/try_again.html"
end
def replace_if_abb(p)
  YAML.load(IO.read('abbreviations.yml'))[p] || p
end
def split_and_prepare(key)
  #key.split(%2F) # CGI.escape("/") -> %2F
  last_param_is_url = key.gsub!(/http:.*/,'') ? CGI.escape($&) : nil
  vars=key.split("/")
  vars<<last_param_is_url if last_param_is_url
  vars[0]=replace_if_abb(vars[0]).downcase
  vars
end
def path
  # path = request.path_info fails when the uri is "/one%2Ftwo" (opensearch escapes..) TODO: fix and use request.path_info
  path = CGI.unescape($1) if request.inspect=~/REQUEST_URI\"=>\"([^\"]*)\"/
  path.slice!(0) # remove the first character ("/")
  path
end