Class: Rack::Rekon::App

Inherits:
Proxy
  • Object
show all
Defined in:
lib/rack/rekon/app.rb

Constant Summary collapse

RIAK_BUCKET =
'riak'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ App

Returns a new instance of App.

Yields:

  • (_self)

Yield Parameters:



16
17
18
19
20
21
22
23
# File 'lib/rack/rekon/app.rb', line 16

def initialize
  yield self if block_given?

  # set defaults
  self.riak_host ||= 'localhost'
  self.riak_port ||= 8098
  self.riak_protocol ||= 'http'
end

Instance Attribute Details

#riak_hostObject

Returns the value of attribute riak_host.



10
11
12
# File 'lib/rack/rekon/app.rb', line 10

def riak_host
  @riak_host
end

#riak_portObject

Returns the value of attribute riak_port.



11
12
13
# File 'lib/rack/rekon/app.rb', line 11

def riak_port
  @riak_port
end

#riak_protocolObject

Returns the value of attribute riak_protocol.



12
13
14
# File 'lib/rack/rekon/app.rb', line 12

def riak_protocol
  @riak_protocol
end

Instance Method Details

#call(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rack/rekon/app.rb', line 25

def call(env)
  request = Rack::Request.new(env)

  # redirect to the rekon app root
  if path_and_query_for(request) == ''
    uri = URI(request.url)
    uri.path = request.script_name + '/riak/rekon/go'
    [301, { 'Location' => uri.to_s, 'Content-Type' => 'text/html' }, []]
  else
    super
  end
end

#path_and_query_for(request) ⇒ Object



46
47
48
49
# File 'lib/rack/rekon/app.rb', line 46

def path_and_query_for(request)
  start_index = request.url.index(request.script_name) + request.script_name.size
  request.url[start_index..-1]
end

#plain_text_body(headers, body) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/rack/rekon/app.rb', line 73

def plain_text_body(headers, body)
  body = body.first if body.is_a?(Array) && body.size == 1
  return body unless headers['content-encoding'] == 'gzip'
  headers.delete('content-encoding')
  io = StringIO.new(body)
  Zlib::GzipReader.new(io).read
end

#rekon_rootObject



42
43
44
# File 'lib/rack/rekon/app.rb', line 42

def rekon_root
  @rekon_root ||= riak_root + "/#{RIAK_BUCKET}/rekon"
end

#rewrite_response(request, status, headers, body) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rack/rekon/app.rb', line 55

def rewrite_response(request, status, headers, body)
  rewriter = case headers['content-type']
    when 'text/html'; ResponseRewriters::HTML
    when 'application/x-sammy-template'; ResponseRewriters::SammyTemplate
    when 'application/javascript'
      case request.path.split('/').last
        when 'rekon.js'; ResponseRewriters::RekonJS
      end
  end

  if rewriter
    rewriter = rewriter.new(request.env['SCRIPT_NAME'], plain_text_body(headers, body))
    body = [rewriter.rewritten_body]
  end

  [status, headers, body]
end

#riak_rootObject



38
39
40
# File 'lib/rack/rekon/app.rb', line 38

def riak_root
  @riak_root ||= "#{riak_protocol}://#{riak_host}:#{riak_port}"
end

#uri_for(request) ⇒ Object



51
52
53
# File 'lib/rack/rekon/app.rb', line 51

def uri_for(request)
  riak_root + path_and_query_for(request)
end