class PlannedEndpoint
Endpoint planned to be served
Constants
- HTTP_METHODS
@return [Array]
Attributes
file_path[RW]
@return [String] Path where content is sourced from
method[RW]
@return [String] REST Method
path[RW]
Location where endpoint will be hosted
status_code[RW]
@return [Integer] HTTP Status code
Public Class Methods
new(path)
click to toggle source
Represent a new endpoint
# File lib/file_sv/planned_endpoint.rb, line 23 def initialize(path) self.file_path = path self.path = serving_loc_for path @file = true if not_text? assign_params_from_name self.method ||= GlobalSettings.default_method return if status_code set_default_status_code end
Public Instance Methods
assign_method_from(file_parts)
click to toggle source
Set REST method used based on filename
# File lib/file_sv/planned_endpoint.rb, line 88 def assign_method_from(file_parts) method_parts = file_parts.find_all { |pa| HTTP_METHODS.include? pa } if method_parts.size > 1 raise FileSv::FileNameError, "Filename #{filename} has more than 1 REST methods #{method_parts}" end self.method = method_parts[0] if method_parts.size == 1 end
assign_params_from_name()
click to toggle source
Set attributes based on filename
# File lib/file_sv/planned_endpoint.rb, line 71 def assign_params_from_name file_parts = filename.split("_") assign_method_from file_parts assign_status_from file_parts end
assign_status_from(file_parts)
click to toggle source
Set status code based on filename
# File lib/file_sv/planned_endpoint.rb, line 78 def assign_status_from(file_parts) status_parts = file_parts.find_all { |part| part.to_i > 99 && part.to_i < 1000 } if status_parts.size > 1 raise FileSv::FileNameError, "Filename #{filename} has more than 1 status code #{status_parts}" end self.status_code = status_parts[0].to_i if status_parts.size == 1 end
content(binding)
click to toggle source
@return [Object] Content of file
# File lib/file_sv/planned_endpoint.rb, line 102 def content(binding) render_text(binding) end
default_empty_code()
click to toggle source
Return default status code for an empty body
# File lib/file_sv/planned_endpoint.rb, line 45 def default_empty_code return false if not_text? if content(binding).strip.empty? self.status_code = GlobalSettings.empty_body_status return true end false end
file?()
click to toggle source
@return [Boolean] Whether endpoint serves a file not a string
# File lib/file_sv/planned_endpoint.rb, line 40 def file? @file end
filename()
click to toggle source
@return [String] Filename without extension or containing folder
# File lib/file_sv/planned_endpoint.rb, line 66 def filename File.basename(file_path, ".*") end
not_text?()
click to toggle source
# File lib/file_sv/planned_endpoint.rb, line 106 def not_text? extension = File.extname(file_path).delete(".") %w[ico png jpg mp4 mp3].include? extension end
render_text(binding)
click to toggle source
@return [String] Render text
# File lib/file_sv/planned_endpoint.rb, line 112 def render_text(binding) ERB.new(File.read(serving_file_name)).result(binding) end
serving_file_name()
click to toggle source
# File lib/file_sv/planned_endpoint.rb, line 97 def serving_file_name File.join(SvPlan.serving_folder, file_path) end
serving_loc_for(path)
click to toggle source
# File lib/file_sv/planned_endpoint.rb, line 34 def serving_loc_for(path) loc = File.split(path).first # TODO: Handle if path has a % at beginning of a folder loc.gsub("#{File::SEPARATOR}%", "#{File::SEPARATOR}:") end
set_default_status_code()
click to toggle source
Set default status code based on empty body or type of METHOD
# File lib/file_sv/planned_endpoint.rb, line 56 def set_default_status_code return if default_empty_code setting_class = FileSv.rest_methods[method.to_sym] raise NotImplementedError, "Unable to interpret method #{method}" unless setting_class self.status_code = setting_class.default_status end