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



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/file_sv/virtual_server.rb', line 35

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:



31
32
33
# File 'lib/file_sv/virtual_server.rb', line 31

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:



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/file_sv/virtual_server.rb', line 49

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
    puts message
  end
  sleep endpoint.delay if endpoint.delay > 0
  [endpoint.status_code, output_for(endpoint, binding)]
end