Class: Snapa::File

Inherits:
Rack::File
  • Object
show all
Defined in:
lib/snapa.rb

Instance Method Summary collapse

Constructor Details

#initialize(root, options = {}) ⇒ File

Returns a new instance of File.



8
9
10
11
# File 'lib/snapa.rb', line 8

def initialize(root, options={})
  @cache_path = options[:cache_path]
  super(root)
end

Instance Method Details

#_call(env) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/snapa.rb', line 37

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

  build(request) unless F.file?(path(request))

  serving(env)
end

#build(request) ⇒ Object



25
26
27
# File 'lib/snapa.rb', line 25

def build(request)
  system [cmd, script, "'#{url(request)}'", "'#{@path}'"].join(' ')
end

#cache_pathObject



29
30
31
# File 'lib/snapa.rb', line 29

def cache_path
  @cache_path ||= defined?(Rails) ? Rails.root.join('public/system') : '/tmp'
end

#cmdObject



13
14
15
# File 'lib/snapa.rb', line 13

def cmd
  'phantomjs'
end

#path(request) ⇒ Object



33
34
35
# File 'lib/snapa.rb', line 33

def path(request)
  @path ||= F.join(cache_path, 'snapa', ::Digest::MD5.hexdigest(request.query_string) + '.png')
end

#scriptObject



17
18
19
# File 'lib/snapa.rb', line 17

def script
  F.expand_path('../snapa.js', __FILE__)
end

#url(request) ⇒ Object



21
22
23
# File 'lib/snapa.rb', line 21

def url(request)
  [@root, request.query_string].join('?')
end