Class: PlannedEndpoint
- Inherits:
-
Object
- Object
- PlannedEndpoint
- Defined in:
- lib/file_sv/planned_endpoint.rb
Overview
Endpoint planned to be served
Constant Summary collapse
- HTTP_METHODS =
%w[get put post patch delete options].freeze
Instance Attribute Summary collapse
-
#delay ⇒ Integer
Delay before responding.
-
#file_path ⇒ String
Path where content is sourced from.
-
#method ⇒ String
REST Method.
-
#path ⇒ Object
Location where endpoint will be hosted.
-
#status_code ⇒ Integer
HTTP Status code.
Instance Method Summary collapse
-
#assign_delay_from(file_parts) ⇒ Object
Set status code based on filename.
-
#assign_method_from(file_parts) ⇒ Object
Set REST method used based on filename.
-
#assign_params_from_name ⇒ Object
Set attributes based on filename.
-
#assign_status_from(file_parts) ⇒ Object
Set status code based on filename.
-
#content(binding) ⇒ Object
Content of file.
-
#default_empty_code ⇒ Object
Return default status code for an empty body.
-
#file? ⇒ Boolean
Whether endpoint serves a file not a string.
-
#filename ⇒ String
Filename without extension or containing folder.
-
#initialize(path) ⇒ PlannedEndpoint
constructor
Represent a new endpoint.
- #not_text? ⇒ Boolean
-
#render_text(binding) ⇒ String
Render text.
- #serving_file_name ⇒ Object
- #serving_loc_for(path) ⇒ Object
-
#set_default_status_code ⇒ Object
Set default status code based on empty body or type of METHOD.
- #short_description ⇒ Object
Constructor Details
#initialize(path) ⇒ PlannedEndpoint
Represent a new endpoint
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/file_sv/planned_endpoint.rb', line 25 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 self.delay ||= 0 return if status_code set_default_status_code end |
Instance Attribute Details
#delay ⇒ Integer
Returns Delay before responding.
19 20 21 |
# File 'lib/file_sv/planned_endpoint.rb', line 19 def delay @delay end |
#file_path ⇒ String
Returns Path where content is sourced from.
13 14 15 |
# File 'lib/file_sv/planned_endpoint.rb', line 13 def file_path @file_path end |
#method ⇒ String
Returns REST Method.
15 16 17 |
# File 'lib/file_sv/planned_endpoint.rb', line 15 def method @method end |
#path ⇒ Object
Location where endpoint will be hosted
11 12 13 |
# File 'lib/file_sv/planned_endpoint.rb', line 11 def path @path end |
#status_code ⇒ Integer
Returns HTTP Status code.
17 18 19 |
# File 'lib/file_sv/planned_endpoint.rb', line 17 def status_code @status_code end |
Instance Method Details
#assign_delay_from(file_parts) ⇒ Object
Set status code based on filename
95 96 97 98 99 100 101 102 |
# File 'lib/file_sv/planned_endpoint.rb', line 95 def assign_delay_from(file_parts) delay_parts = file_parts.find_all { |part| part.start_with?('delay') } if delay_parts.size > 1 raise FileSv::FileNameError, "Filename #{filename} has more than 1 delay #{delay_parts}" end self.delay = delay_parts[0][5..-1].to_i if delay_parts.size == 1 end |
#assign_method_from(file_parts) ⇒ Object
Set REST method used based on filename
105 106 107 108 109 110 111 112 |
# File 'lib/file_sv/planned_endpoint.rb', line 105 def assign_method_from(file_parts) method_parts = file_parts.find_all { |part| HTTP_METHODS.include? part } 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 ⇒ Object
Set attributes based on filename
77 78 79 80 81 82 |
# File 'lib/file_sv/planned_endpoint.rb', line 77 def assign_params_from_name file_parts = filename.split("_") assign_method_from file_parts assign_delay_from file_parts assign_status_from file_parts end |
#assign_status_from(file_parts) ⇒ Object
Set status code based on filename
85 86 87 88 89 90 91 92 |
# File 'lib/file_sv/planned_endpoint.rb', line 85 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) ⇒ Object
Returns Content of file.
119 120 121 |
# File 'lib/file_sv/planned_endpoint.rb', line 119 def content(binding) render_text(binding) end |
#default_empty_code ⇒ Object
Return default status code for an empty body
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/file_sv/planned_endpoint.rb', line 48 def default_empty_code return false if not_text? if content(binding).strip.empty? self.status_code = GlobalSettings.empty_body_status return true end return false rescue Encoding::CompatibilityError false end |
#file? ⇒ Boolean
Returns Whether endpoint serves a file not a string.
43 44 45 |
# File 'lib/file_sv/planned_endpoint.rb', line 43 def file? @file end |
#filename ⇒ String
Returns Filename without extension or containing folder.
72 73 74 |
# File 'lib/file_sv/planned_endpoint.rb', line 72 def filename File.basename(file_path, ".*") end |
#not_text? ⇒ Boolean
123 124 125 126 |
# File 'lib/file_sv/planned_endpoint.rb', line 123 def not_text? extension = File.extname(file_path).delete(".") %w[ico png jpg mp4 mp3].include? extension end |
#render_text(binding) ⇒ String
Returns Render text.
129 130 131 |
# File 'lib/file_sv/planned_endpoint.rb', line 129 def render_text(binding) ERB.new(File.read(serving_file_name)).result(binding) end |
#serving_file_name ⇒ Object
114 115 116 |
# File 'lib/file_sv/planned_endpoint.rb', line 114 def serving_file_name File.join(SvPlan.serving_folder, file_path) end |
#serving_loc_for(path) ⇒ Object
37 38 39 40 |
# File 'lib/file_sv/planned_endpoint.rb', line 37 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 ⇒ Object
Set default status code based on empty body or type of METHOD
62 63 64 65 66 67 68 69 |
# File 'lib/file_sv/planned_endpoint.rb', line 62 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 |
#short_description ⇒ Object
133 134 135 136 137 |
# File 'lib/file_sv/planned_endpoint.rb', line 133 def short_description desc = self.status_code.to_s desc += " (delay #{self.delay} sec)" if self.delay > 0 return desc end |