Class: Hashbang::Standalone::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/hashbang/standalone/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Middleware

Returns a new instance of Middleware.



7
8
9
10
# File 'lib/hashbang/standalone/middleware.rb', line 7

def initialize(root)
  Config.load File.expand_path('config.rb', root)
  Headless.new.start
end

Instance Method Details

#call(environment) ⇒ Object



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
# File 'lib/hashbang/standalone/middleware.rb', line 12

def call(environment)
  url = environment['QUERY_STRING'].split('&').find{|x| x[0,4] == 'url='}

  unless url.to_s.length == 0
    url = url.split('=')
    url.shift

    url = url.join('=')
    url = URI.unescape url
    url = Crawler.urlFromUrl url
  end

  host = URI.parse(url).host

  if url.to_s.length == 0 || !host.match(Config.url)
    return [200, {"Content-Type" => "text/html; charset=utf-8"}, ['']]
  end

  html = Crawler.gimme url, Config.timeout

  if html.respond_to? :force_encoding
    html.force_encoding "UTF-8"
  end

  return [200, {"Content-Type" => "text/html; charset=utf-8"}, [html]]
end