Class: VirtualServer

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/file_sv/virtual_server.rb

Overview

Virtual server hosting virtual service defined through files

Instance Method Summary collapse

Instance Method Details

#append_content_type(endpoint) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/file_sv/virtual_server.rb', line 43

def append_content_type(endpoint)
  if endpoint.file_path.end_with? ".json"
    content_type :json
  elsif endpoint.file_path.end_with? ".xml"
    content_type :xml
  elsif endpoint.file_path.end_with? ".css"
    content_type :css
  elsif endpoint.file_path.end_with? ".js"
    content_type :js
  end
end

#output_for(endpoint, binding) ⇒ Object

Output for endpoint, either a file or text content

Parameters:



39
40
41
# File 'lib/file_sv/virtual_server.rb', line 39

def output_for(endpoint, binding)
  endpoint.file? ? send_file(endpoint.serving_file_name) : endpoint.content(binding)
end

#serve(endpoint, id = nil) ⇒ Object

Log endpoint. Return content and status code defined by endpoint

Parameters:



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/file_sv/virtual_server.rb', line 57

def serve(endpoint, id = nil)
  @id = id
  append_content_type(endpoint)
  if ENV["debug"] == "true"
    message = "Using endpoint based on file #{endpoint.serving_file_name}."
    message += " Using param '#{@id}'" if id
    FileSv.logger.debug message
  end
  sleep endpoint.delay if endpoint.delay > 0
  [endpoint.status_code, output_for(endpoint, binding)]
end