Sha256: b836d6bb0522ec3c810133c687597095c8b4fc077b45740ca7e79424e68d4f49

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require 'sinatra/base'
require 'haml'
require 'json'
require 'time'

module Capybara
  module Remote
    module Viewer
      class Server < Sinatra::Base

        def self.path
          @path
        end
        def self.path=(val)
          @path = val
        end

        set :views, File.expand_path('../../../../../views', __FILE__)
        set :public_folder, File.expand_path('../../../../../static', __FILE__)

        def files
          Dir.glob File.join(Server.path, '**/*.html')
        end

        def file(id)
          files.find { |f| f =~ /#{id}.html$/ }
        end

        def file_path(str)
          "/files/#{file_name(str)}"
        end

        def file_name(str)
          File.basename(str, '.html')
        end

        def file_date(str)
          filename = file_name(str)
          prefix, date_str = filename.split('-')
          if prefix == 'capybara'
            time = Time.parse date_str[0..13]
            ms = date_str[14..-1]
            time.strftime("%Y/%m/%d - %H:%M:%S.#{ms}")
          else
            filename
          end
        end

        get '/' do
          haml :index, locals: { dir: Server.path, files: files }
        end

        get '/files' do
          headers 'Content-Type' => 'application/json'

          list = files.map do |file|
            { name: file_date(file), url: file_path(file) }
          end

          { files: list }.to_json
        end

        get '/files/:id' do
          File.read file(params['id'])
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
capybara-remote-viewer-0.0.2 lib/capybara/remote/viewer/server.rb