file_sv 0.1.8 → 0.1.11

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: f0b7afdb64abc1410be6cebdcf263fca4f5d6a1bc3319e499aa2465b494ea144
4
- data.tar.gz: f64237aecf384cafc4bc9469ebecd6c3e4958083ebad3a1e5e90959b425e2662
3
+ metadata.gz: f83c739611ce58614e8d076e6be444b889ff63727b174dcfbd13025e3367b2d6
4
+ data.tar.gz: 042d4b3ae8ad91e6708b19bcdf9ad5b3473682494a8a6c4879ef50e61dbcce29
5
5
  SHA512:
6
- metadata.gz: 7df5f1b80d1330a08ea6aafa52ba1d4e3d0d01ae963d276ca0a3ad0bffa4f03333e328955e227a86a4d4d980ae9c4a911ee018fea14cbf5d82ceac6baf5c4cdf
7
- data.tar.gz: e6d42d44838e2bb3d44bb1427c904dd1a75bbe400b26d882ed4109ebcdd6ceae1c82ebb33333a6efc0114a647a7e861118f851d536b9461c19ee97b83ae93932
6
+ metadata.gz: fbe538e3dad13e5bdd4af99c8d10b792397e5a1252c2a9809a5120a9aec9302a5d83aff05f3a36d9ef112a3c335184c2c97d4668fa9e44f25fe2ffe319b9175c
7
+ data.tar.gz: d5427782a507a555b305578c50354405f89b6307824d810916a529018f392c541f003f3068d96d257fb8d8ea9460bae38b6db176cba33210bbca62edb1636cbb
data/exe/file_sv CHANGED
File without changes
@@ -15,6 +15,8 @@ class PlannedEndpoint
15
15
  attr_accessor :method
16
16
  # @return [Integer] HTTP Status code
17
17
  attr_accessor :status_code
18
+ # @return [Integer] Delay before responding
19
+ attr_accessor :delay
18
20
 
19
21
  # @return [Array]
20
22
  HTTP_METHODS = %w[get put post patch delete options].freeze
@@ -26,6 +28,7 @@ class PlannedEndpoint
26
28
  @file = true if not_text?
27
29
  assign_params_from_name
28
30
  self.method ||= GlobalSettings.default_method
31
+ self.delay ||= 0
29
32
  return if status_code
30
33
 
31
34
  set_default_status_code
@@ -71,6 +74,7 @@ class PlannedEndpoint
71
74
  def assign_params_from_name
72
75
  file_parts = filename.split("_")
73
76
  assign_method_from file_parts
77
+ assign_delay_from file_parts
74
78
  assign_status_from file_parts
75
79
  end
76
80
 
@@ -84,9 +88,19 @@ class PlannedEndpoint
84
88
  self.status_code = status_parts[0].to_i if status_parts.size == 1
85
89
  end
86
90
 
91
+ # Set status code based on filename
92
+ def assign_delay_from(file_parts)
93
+ delay_parts = file_parts.find_all { |part| part.start_with?('delay') }
94
+ if delay_parts.size > 1
95
+ raise FileSv::FileNameError, "Filename #{filename} has more than 1 delay #{delay_parts}"
96
+ end
97
+
98
+ self.delay = delay_parts[0][5..-1].to_i if delay_parts.size == 1
99
+ end
100
+
87
101
  # Set REST method used based on filename
88
102
  def assign_method_from(file_parts)
89
- method_parts = file_parts.find_all { |pa| HTTP_METHODS.include? pa }
103
+ method_parts = file_parts.find_all { |part| HTTP_METHODS.include? part }
90
104
  if method_parts.size > 1
91
105
  raise FileSv::FileNameError, "Filename #{filename} has more than 1 REST methods #{method_parts}"
92
106
  end
@@ -112,4 +126,10 @@ class PlannedEndpoint
112
126
  def render_text(binding)
113
127
  ERB.new(File.read(serving_file_name)).result(binding)
114
128
  end
129
+
130
+ def short_description
131
+ desc = self.status_code.to_s
132
+ desc += " (delay #{self.delay} sec)" if self.delay > 0
133
+ return desc
134
+ end
115
135
  end
@@ -3,7 +3,7 @@
3
3
  # Responsible for loading all files in service directory and creating service
4
4
  module ServiceLoader
5
5
  # @return [String] Folder where service served from
6
- @serving_folder = ""
6
+ @serving_folder = "."
7
7
 
8
8
  class << self
9
9
  # Create virtual service plan based on folder
@@ -5,19 +5,24 @@ require_relative "file_processor"
5
5
  # Holds plan of what virtual service will look like
6
6
  class SvPlan
7
7
  @endpoints = {}
8
+ @file_list = []
8
9
  class << self
9
10
  # @return [Hash] Endpoints included in plan. Key - endpoint, value - methods served under it
10
11
  attr_reader :endpoints
11
12
  # @return [String] Folder plan is served from
12
13
  attr_accessor :serving_folder
13
14
 
15
+ # @return [String] List of files plan is built from
16
+ attr_accessor :file_list
17
+
14
18
  # Create a plan new plan for a virtual service
15
19
  def create(folder)
16
20
  self.serving_folder = folder
17
21
  puts "Creating service based on files in #{folder}"
18
- file_list = Dir.glob("#{folder}/**/*.*", File::FNM_DOTMATCH) - Dir.glob("#{folder}/#{GlobalSettings.ignore_files}, File::FNM_DOTMATCH")
22
+ file_list = Dir.glob("#{folder}/**/*.*", File::FNM_DOTMATCH) - Dir.glob("#{folder}/#{GlobalSettings.ignore_files}", File::FNM_DOTMATCH)
19
23
  file_list.each do |file|
20
24
  next if File.directory? file
25
+ SvPlan.file_list << file
21
26
  process_file file
22
27
  end
23
28
  end
@@ -64,9 +69,9 @@ Serving based on folder: #{Dir.pwd}. Related to folder executed: #{serving_folde
64
69
 
65
70
  private
66
71
 
67
- # @return [String]
72
+ # @return [String] Message to description all endpoints for a method undera endpoint
68
73
  def description_message(method_name, endpoints)
69
- " #{method_name.upcase} (#{endpoints.size} responses) #{endpoints.collect(&:status_code)}\n"
74
+ " #{method_name.upcase} (#{endpoints.size} responses) #{endpoints.collect(&:short_description)}\n"
70
75
  end
71
76
  end
72
77
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FileSv
4
- VERSION = "0.1.8"
4
+ VERSION = "0.1.11"
5
5
  end
@@ -32,15 +32,29 @@ for more details'
32
32
  endpoint.file? ? send_file(endpoint.serving_file_name) : endpoint.content(binding)
33
33
  end
34
34
 
35
+ def append_content_type(endpoint)
36
+ if (endpoint.file_path.end_with? '.json')
37
+ content_type :json
38
+ elsif endpoint.file_path.end_with? '.xml'
39
+ content_type :xml
40
+ elsif endpoint.file_path.end_with? '.css'
41
+ content_type :css
42
+ elsif endpoint.file_path.end_with? '.js'
43
+ content_type :js
44
+ end
45
+ end
46
+
35
47
  # Log endpoint. Return content and status code defined by endpoint
36
48
  # @param [PlannedEndpoint] endpoint Planned endpoint to serve
37
49
  def serve(endpoint, id = nil)
38
50
  @id = id
51
+ append_content_type(endpoint)
39
52
  if ENV['debug'] == "true"
40
53
  message = "Using endpoint based on file #{endpoint.serving_file_name}."
41
54
  message += " Using param '#{@id}'" if id
42
55
  puts message
43
56
  end
57
+ sleep endpoint.delay if endpoint.delay > 0
44
58
  [endpoint.status_code, output_for(endpoint, binding)]
45
59
  end
46
60
 
@@ -1,16 +1,16 @@
1
- # frozen_string_literal: true
2
-
3
- # Process YAML files
4
- class YamlProcessor
5
- class << self
6
- # Process YAML file
7
- def process(filename)
8
- if filename == "/file_sv.yaml"
9
- puts "Overriding default config based on #{File.join(Dir.pwd, filename[1..])}"
10
- load_default_config filename[1..]
11
- else
12
- puts "Skipping #{filename}"
13
- end
14
- end
15
- end
16
- end
1
+ # frozen_string_literal: true
2
+
3
+ # Process YAML files
4
+ class YamlProcessor
5
+ class << self
6
+ # Process YAML file
7
+ def process(filename)
8
+ if filename == "/file_sv.yaml"
9
+ puts "Overriding default config based on #{File.join(Dir.pwd, filename[1..])}"
10
+ load_default_config filename[1..]
11
+ else
12
+ puts "Skipping #{filename}"
13
+ end
14
+ end
15
+ end
16
+ end
data/lib/file_sv.rb CHANGED
@@ -1,64 +1,64 @@
1
- # frozen_string_literal: true
2
-
3
- require "yaml"
4
- require_relative "file_sv/version"
5
- require_relative "file_sv/global_settings"
6
- require_relative "file_sv/sv_plan"
7
- require_relative "file_sv/service_loader"
8
- require_relative "file_sv/planned_endpoint"
9
-
10
- # Create Service Virtualization from a simple file system
11
- module FileSv
12
- # General error for FileSv
13
- class Error < StandardError; end
14
-
15
- # Error related to incorrect format of filename
16
- class FileNameError < Error; end
17
-
18
- class << self
19
- # @return [Hash] Mapping of REST method names to setting classes
20
- def rest_methods
21
- {
22
- get: GetSettings, post: PostSettings, patch: PatchSettings, options: OptionsSettings,
23
- delete: DeleteSettings, put: PutSettings
24
- }
25
- end
26
- end
27
- end
28
-
29
- CONFIG_FILE = "file_sv.yaml"
30
-
31
- # Set values in global settings based on config
32
- def load_default_config(file_path)
33
- return unless File.exist? file_path
34
-
35
- config = YAML.load_file file_path
36
- return unless config # Handle empty YAML file
37
-
38
- config["global"]&.each do |key, value|
39
- GlobalSettings.send("#{key}=", value)
40
- end
41
-
42
- load_rest_method_config config
43
- end
44
-
45
- # Load details of each REST method
46
- def load_rest_method_config(config)
47
- FileSv.rest_methods.each do |method, setting_class|
48
- config[method.to_s]&.each { |key, value| setting_class.send("#{key}=", value) }
49
- end
50
- end
51
-
52
- # Set global params based on ENV vars
53
- def set_based_on_env_vars
54
- GlobalSettings.instance_variables.each do |setting|
55
- setting_name = setting.to_s[1..]
56
- if ENV[setting_name]
57
- puts "Setting #{setting_name} to #{ENV[setting_name]}"
58
- GlobalSettings.send("#{setting_name}=", ENV[setting_name])
59
- end
60
- end
61
- end
62
-
63
- load_default_config CONFIG_FILE
64
- set_based_on_env_vars
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+ require_relative "file_sv/version"
5
+ require_relative "file_sv/global_settings"
6
+ require_relative "file_sv/sv_plan"
7
+ require_relative "file_sv/service_loader"
8
+ require_relative "file_sv/planned_endpoint"
9
+
10
+ # Create Service Virtualization from a simple file system
11
+ module FileSv
12
+ # General error for FileSv
13
+ class Error < StandardError; end
14
+
15
+ # Error related to incorrect format of filename
16
+ class FileNameError < Error; end
17
+
18
+ class << self
19
+ # @return [Hash] Mapping of REST method names to setting classes
20
+ def rest_methods
21
+ {
22
+ get: GetSettings, post: PostSettings, patch: PatchSettings, options: OptionsSettings,
23
+ delete: DeleteSettings, put: PutSettings
24
+ }
25
+ end
26
+ end
27
+ end
28
+
29
+ CONFIG_FILE = "file_sv.yaml"
30
+
31
+ # Set values in global settings based on config
32
+ def load_default_config(file_path)
33
+ return unless File.exist? file_path
34
+
35
+ config = YAML.load_file file_path
36
+ return unless config # Handle empty YAML file
37
+
38
+ config["global"]&.each do |key, value|
39
+ GlobalSettings.send("#{key}=", value)
40
+ end
41
+
42
+ load_rest_method_config config
43
+ end
44
+
45
+ # Load details of each REST method
46
+ def load_rest_method_config(config)
47
+ FileSv.rest_methods.each do |method, setting_class|
48
+ config[method.to_s]&.each { |key, value| setting_class.send("#{key}=", value) }
49
+ end
50
+ end
51
+
52
+ # Set global params based on ENV vars
53
+ def set_based_on_env_vars
54
+ GlobalSettings.instance_variables.each do |setting|
55
+ setting_name = setting.to_s[1..]
56
+ if ENV[setting_name]
57
+ puts "Setting #{setting_name} to #{ENV[setting_name]}"
58
+ GlobalSettings.send("#{setting_name}=", ENV[setting_name])
59
+ end
60
+ end
61
+ end
62
+
63
+ load_default_config CONFIG_FILE
64
+ set_based_on_env_vars
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.8
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Garratt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-19 00:00:00.000000000 Z
11
+ date: 2024-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faker
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rackup
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: sinatra
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -124,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
138
  - !ruby/object:Gem::Version
125
139
  version: '0'
126
140
  requirements: []
127
- rubygems_version: 3.2.32
141
+ rubygems_version: 3.2.3
128
142
  signing_key:
129
143
  specification_version: 4
130
144
  summary: REST service virtualisation through file structure.