file_sv 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b8c717829a4044f5970f099311a51d86317e009cb04d01e2b596e2ac24ee5296
4
- data.tar.gz: 64a21579ccd9c87ea14a0e8b634f9095450cea66167ef0d414bff97e77f7ce3f
3
+ metadata.gz: 7207368213637f9e6b91164ea82a398553db87e01743a0a191d5328635e91b84
4
+ data.tar.gz: 70f93031650207bc4df46cbee90ecea4da58c0b2ade43db384a76e034db58cf2
5
5
  SHA512:
6
- metadata.gz: 594bfe0862c82585f8568cf3a46d2952bd631bf6795fd7b518a74a4b578eb9eab500523016602110f5ca60094f9a53bfe91c395970ad3ca5774d3d51f2a8427e
7
- data.tar.gz: dd153f38c16a882d3ff806dcec71a7b798e7578aea0fe5322f868ea77bd6af7958e7eefc23b47e31f18bb35a78c869e0386155b7936e62086e07411734a2a9ab
6
+ metadata.gz: 5402cb767636ad98243cab0adae1008a6e9683abdffc3d98a52de3987e9042eae1b703a3394d41213e8bd6591c59b17fef2afd8bc7facd4609d5ed8755a0666c
7
+ data.tar.gz: d95d5443fccb5db6e05892c403667c77e216296edad1e5dd37688b4d15bf5e23f61c88175496f60ca98d06bbd68de3fc0c497120d58a93e7c64e71f8623f1bb9
data/lib/file_sv.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "yaml"
3
4
  require_relative "file_sv/version"
4
5
  require_relative "file_sv/global_settings"
5
6
  require_relative "file_sv/sv_plan"
@@ -29,14 +30,20 @@ CONFIG_FILE = "file_sv.yaml"
29
30
 
30
31
  # Set values in global settings based on config
31
32
  def load_default_config(file_path)
32
- require "yaml"
33
33
  return unless File.exist? file_path
34
34
 
35
35
  config = YAML.load_file file_path
36
+ return unless config # Handle empty YAML file
37
+
36
38
  config["global"]&.each do |key, value|
37
39
  GlobalSettings.send("#{key}=", value)
38
40
  end
39
41
 
42
+ load_rest_method_config config
43
+ end
44
+
45
+ # Load details of each REST method
46
+ def load_rest_method_config(config)
40
47
  FileSv.rest_methods.each do |method, setting_class|
41
48
  config[method.to_s]&.each { |key, value| setting_class.send("#{key}=", value) }
42
49
  end
@@ -3,6 +3,7 @@
3
3
  require "erb"
4
4
  require "securerandom"
5
5
  require "faker"
6
+ require "pathname"
6
7
 
7
8
  # Endpoint planned to be served
8
9
  class PlannedEndpoint
@@ -21,7 +22,7 @@ class PlannedEndpoint
21
22
  # Represent a new endpoint
22
23
  def initialize(path)
23
24
  self.file_path = path
24
- self.path = File.split(path).first
25
+ self.path = serving_loc_for path
25
26
  @file = true if not_text?
26
27
  assign_params_from_name
27
28
  self.method ||= GlobalSettings.default_method
@@ -30,6 +31,11 @@ class PlannedEndpoint
30
31
  set_default_status_code
31
32
  end
32
33
 
34
+ def serving_loc_for(path)
35
+ loc = File.split(path).first # TODO: Handle if path has a % at beginning of a folder
36
+ loc.gsub("#{File::SEPARATOR}%", "#{File::SEPARATOR}:")
37
+ end
38
+
33
39
  # @return [Boolean] Whether endpoint serves a file not a string
34
40
  def file?
35
41
  @file
@@ -39,7 +45,7 @@ class PlannedEndpoint
39
45
  def default_empty_code
40
46
  return false if not_text?
41
47
 
42
- if content.strip.empty?
48
+ if content(binding).strip.empty?
43
49
  self.status_code = GlobalSettings.empty_body_status
44
50
  return true
45
51
  end
@@ -93,8 +99,8 @@ class PlannedEndpoint
93
99
  end
94
100
 
95
101
  # @return [Object] Content of file
96
- def content
97
- render_text
102
+ def content(binding)
103
+ render_text(binding)
98
104
  end
99
105
 
100
106
  def not_text?
@@ -103,7 +109,7 @@ class PlannedEndpoint
103
109
  end
104
110
 
105
111
  # @return [String] Render text
106
- def render_text
112
+ def render_text(binding)
107
113
  ERB.new(File.read(serving_file_name)).result(binding)
108
114
  end
109
115
  end
@@ -37,7 +37,7 @@ class SvPlan
37
37
  endpoints.sort { |a, b| a[0].length - b[0].length }.each do |endpoint, methods|
38
38
  endpoint_desc += "#{endpoint} \n"
39
39
  methods.each do |method_name, endpoints|
40
- endpoint_desc += " #{method_name.upcase} (#{endpoints.size} responses)\n"
40
+ endpoint_desc += description_message method_name, endpoints
41
41
  end
42
42
  end
43
43
 
@@ -51,10 +51,19 @@ Serving based on folder: #{Dir.pwd}. Related to folder executed: #{serving_folde
51
51
  "Endpoints: #{endpoints.inspect}"
52
52
  end
53
53
 
54
+ # Add endpoint to plan
55
+ # @param [PlannedEndpoint] other Endpoint to add to plan
54
56
  def +(other)
55
57
  @endpoints[other.path] ||= {}
56
58
  @endpoints[other.path][other.method] ||= []
57
59
  @endpoints[other.path][other.method] << other
58
60
  end
61
+
62
+ private
63
+
64
+ # @return [String]
65
+ def description_message(method_name, endpoints)
66
+ " #{method_name.upcase} (#{endpoints.size} responses) #{endpoints.collect(&:status_code)}\n"
67
+ end
59
68
  end
60
69
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FileSv
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.4"
5
5
  end
@@ -24,28 +24,38 @@ for more details'
24
24
 
25
25
  doc_endpoint "/docs"
26
26
 
27
+ # Output for endpoint, either a file or text content
28
+ # @param [PlannedEndpoint] endpoint Planned endpoint to serve
29
+ def output_for(endpoint, binding)
30
+ endpoint.file? ? send_file(endpoint.serving_file_name) : endpoint.content(binding)
31
+ end
32
+
33
+ # Log endpoint. Return content and status code defined by endpoint
34
+ # @param [PlannedEndpoint] endpoint Planned endpoint to serve
35
+ def serve(endpoint, id = nil)
36
+ message = "Using endpoint based on file #{endpoint.serving_file_name}."
37
+ @id = id
38
+ message += " Using param '#{@id}'" if id
39
+ puts message
40
+ [endpoint.status_code, output_for(endpoint, binding)]
41
+ end
42
+
27
43
  SvPlan.endpoints.each do |_endpoint_path, endpoint_value|
28
44
  endpoint_value.each do |_method_name, endpoints|
29
- if endpoints.size < 2
30
- endpoint = endpoints[0]
31
- documentation "Endpoint #{endpoint.path}" do
32
- response "1 kind of response"
33
- status endpoint.status_code
34
- end
35
- send(endpoint.method, endpoint.path) do
36
- [endpoint.status_code, endpoint.file? ? send_file(endpoint.serving_file_name) : endpoint.content]
45
+ endpoint_base = endpoints[0]
46
+ documentation "Endpoint #{endpoint_base.path}" do
47
+ response "#{endpoints.size} kinds of response"
48
+ end
49
+ if endpoint_base.path.include? "#{File::Separator}:"
50
+ send(endpoint_base.method, endpoint_base.path) do |id|
51
+ endpoint = endpoints.sample
52
+ serve endpoint, id
37
53
  end
38
54
  else
39
- endpoint_base = endpoints[0]
40
- documentation "Endpoint #{endpoint_base.path}" do
41
- response "#{endpoints.size} kinds of response"
42
- end
43
55
  send(endpoint_base.method, endpoint_base.path) do
44
- my_points = endpoints
45
- endpoint = my_points.sample
46
- [endpoint.status_code, endpoint.file? ? send_file(endpoint.serving_file_name) : endpoint.content]
56
+ endpoint = endpoints.sample
57
+ serve endpoint
47
58
  end
48
- # Average same methods at same endpoint
49
59
  end
50
60
  end
51
61
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_sv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Garratt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-05 00:00:00.000000000 Z
11
+ date: 2021-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faker