Module: Lindo

Defined in:
lib/lindo.rb,
lib/lindo/browser.rb

Defined Under Namespace

Classes: Browser

Constant Summary collapse

ASSETS =
%w(images stylesheets javascripts)

Instance Method Summary collapse

Instance Method Details

#copy_assetsObject



25
26
27
28
29
30
# File 'lib/lindo.rb', line 25

def copy_assets
  ASSETS.each do |e|
    dir = File.join(::Rails.root, "public", e)
    FileUtils.cp_r(dir, tmp_dir) if File.exists?(dir)
  end
end

#create_tmp_dirObject



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

def create_tmp_dir
  FileUtils.rm_r(tmp_dir) if File.exists?(tmp_dir)
  FileUtils.mkdir_p(tmp_dir)
end

#open_from_file(data, file) ⇒ Object



32
33
34
35
36
# File 'lib/lindo.rb', line 32

def open_from_file(data, file)
  scrub(data)
  File.open(file, File::CREAT|File::TRUNC|File::WRONLY) { |f| f << data }
  Browser.open(file)
end

#response_htmlObject



50
51
52
# File 'lib/lindo.rb', line 50

def response_html
  File.join(tmp_dir, "response.html")
end

#response_txtObject



46
47
48
# File 'lib/lindo.rb', line 46

def response_txt
  File.join(tmp_dir, "response.txt")
end

#scrub(data) ⇒ Object



38
39
40
# File 'lib/lindo.rb', line 38

def scrub(data)
  ASSETS.each { |e| data.gsub!("=\"/#{e}/", "=\"#{e}/") }
end

#tmp_dirObject



42
43
44
# File 'lib/lindo.rb', line 42

def tmp_dir
  File.join(::Rails.root, "tmp", "lindo")
end

#vr(format = :web) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/lindo.rb', line 7

def vr(format=:web)
  create_tmp_dir
  copy_assets
  if format.is_a?(Symbol)
    case format
      when :web  then open_from_file(@response.body, response_html)
      when :html then open_from_file(@response.body, response_txt)
    end
  else
    open_from_file(format, response_html)
  end
end