file_sv 0.1.12 → 0.2.0

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: 72954e0e4bfe4eda783ccb40b4219bcd7a915a86c328e9157625c9151d085de4
4
- data.tar.gz: e250a4b0320757bc1577a538be9e59c72ec12a25f4b3a4e442bde182784a3cdc
3
+ metadata.gz: 7177e3a064f21ddcd10c0e939d9557460e23a260d247cfbed515a94a5b9265a3
4
+ data.tar.gz: e5cc7b549e0d6264a2a357a00de1d85ef18b7aafa757c621c0eeb647bc6db332
5
5
  SHA512:
6
- metadata.gz: 9b3c6837fcb24dcad7355cf32f7c2f868393935224f32826ee5d585e522fdd07ddb42d8d761362ead1ed997cc4eb88550e30c3dc569a3affd4ae088034364ffb
7
- data.tar.gz: 42f8f2b8c2a149ebbdb61924dd3aaede4797da0dc3ebb3ab13945bc7cc719f3b3e32330b546c927320e3dd42c6d951ad5ac99adefcdf271f9eecf1d85e79060a
6
+ metadata.gz: 61d515119cc7b4d3cefca1f8231c2d85f51394518e448013b4457271f01dddffd370426a5f4077bd5c05f7a446085643524c2968a443b0a1fd8fd86a5b3a6276
7
+ data.tar.gz: 96c064f198dbea63b6c5f251f4b2758856759f604cf8b3f3e31ccbb80fc895ee9d8861fe486e605d6a01feaab5f848a46d81501beadd1f1fee975d7bec9de146
data/exe/file_sv CHANGED
@@ -33,6 +33,13 @@ class Exe < Thor
33
33
  ServiceLoader.inspect folder
34
34
  end
35
35
 
36
+ desc "generate har_file_location mock_service_base", "Generate mock folder/file structure from recording"
37
+ def generate(har_file_location, mock_service_base, output_folder)
38
+ require "file_sv/har_generator"
39
+ generator = HarGenerator.new(har_file_location, mock_service_base, output_folder)
40
+ generator.generate
41
+ end
42
+
36
43
  desc "version", "Version of FileSv"
37
44
  def version
38
45
  require "file_sv/version"
@@ -0,0 +1,78 @@
1
+ require_relative '../file_sv'
2
+ require 'json'
3
+ require 'fileutils'
4
+
5
+ class HarGenerator
6
+
7
+ attr_accessor :har_file_location
8
+
9
+ attr_accessor :mock_service_base
10
+
11
+ attr_accessor :output_folder
12
+ # @return [String] Har file content
13
+ attr_accessor :content
14
+
15
+ attr_accessor :entries
16
+
17
+ def initialize(har_file_location, mock_service_base, output_folder)
18
+ self.har_file_location = har_file_location
19
+ self.mock_service_base = mock_service_base
20
+ self.output_folder = output_folder
21
+ self.content = File.read(har_file_location)
22
+ parse_har
23
+ end
24
+
25
+ def parse_har
26
+ parsed = JSON.parse(self.content)
27
+ pages = parsed['log']['pages']
28
+ self.entries = parsed['log']['entries']
29
+ puts "Found #{self.entries.count} entries in #{self.har_file_location}"
30
+ end
31
+
32
+ def generate
33
+ self.entries.each do |entry|
34
+ request = entry['request']
35
+ response = entry['response']
36
+
37
+ request_url = request['url']
38
+ if (!request_url.include?(self.mock_service_base))
39
+ next;
40
+ end
41
+
42
+ path = request_url[self.mock_service_base.length..-1]
43
+ path_folder = path.split('.')[0]
44
+
45
+ method = request['method']
46
+ status_code = response['status']
47
+ content = response['content']
48
+ mime_type = content['mimeType']
49
+ extension = mime_type.split('/').last
50
+
51
+ extension = 'js' if (extension == 'javascript')
52
+
53
+ location = "#{self.output_folder}/#{path_folder}/#{method}_#{status_code}.#{extension}"
54
+ create_file_at "./#{location}", extension, content['text']
55
+ end
56
+ end
57
+
58
+ # Create folder if there's not a file already there.
59
+ # Will create parent folder if necessary.
60
+ # @param [String] folder Folder to create
61
+ def create_folder(folder)
62
+ if File.exist? folder
63
+ warn "!! #{folder} already exists and is not a directory" unless File.directory? folder
64
+ else
65
+ FileUtils.mkdir_p folder
66
+ puts "Created folder: #{folder}"
67
+ end
68
+ end
69
+
70
+ def create_file_at(file_path, extension, content)
71
+ file_path = file_path.gsub('//', '/')
72
+ folder_name = File.split(file_path).first
73
+ folder_name += ".#{extension}" if (extension == '.css' || extension == '.js')
74
+ create_folder folder_name
75
+ puts "Creating response at #{file_path}"
76
+ File.open("#{folder_name}/#{File.split(file_path).last}", 'w') { |f| f.puts content }
77
+ end
78
+ end
@@ -52,6 +52,9 @@ class PlannedEndpoint
52
52
  self.status_code = GlobalSettings.empty_body_status
53
53
  return true
54
54
  end
55
+
56
+ return false
57
+ rescue Encoding::CompatibilityError
55
58
  false
56
59
  end
57
60
 
@@ -127,6 +130,19 @@ class PlannedEndpoint
127
130
  ERB.new(File.read(serving_file_name)).result(binding)
128
131
  end
129
132
 
133
+ # Log when API is called
134
+ def log_call(params)
135
+ if params.keys.count > 0
136
+ show_params = []
137
+ params.each do |key, value|
138
+ show_params << "#{key}=#{value}"
139
+ end
140
+ "#{self.method} #{self.path}?#{show_params.join('&')}"
141
+ else
142
+ "#{self.method} #{self.path}"
143
+ end
144
+ end
145
+
130
146
  def short_description
131
147
  desc = self.status_code.to_s
132
148
  desc += " (delay #{self.delay} sec)" if self.delay > 0
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FileSv
4
- VERSION = "0.1.12"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -58,7 +58,7 @@ for more details'
58
58
  [endpoint.status_code, output_for(endpoint, binding)]
59
59
  end
60
60
 
61
- SvPlan.endpoints.each do |_endpoint_path, endpoint_value|
61
+ SvPlan.endpoints.each do |endpoint_path, endpoint_value|
62
62
  endpoint_value.each do |_method_name, endpoints|
63
63
  endpoint_base = endpoints[0]
64
64
  documentation "Endpoint #{endpoint_base.path}" do
@@ -66,19 +66,29 @@ for more details'
66
66
  end
67
67
  if endpoint_base.path.include? "#{File::Separator}:"
68
68
  send(endpoint_base.method, endpoint_base.path) do |id|
69
+ response["Access-Control-Allow-Origin"] = "*"
69
70
  endpoint = endpoints.sample
70
71
  @params = params
71
- puts "#{endpoint_base.method} #{endpoint_base.path} ?#{@params}"
72
+ puts endpoint.log_call(@params)
72
73
  serve endpoint, id
73
74
  end
74
75
  else
75
76
  send(endpoint_base.method, endpoint_base.path) do
77
+ response["Access-Control-Allow-Origin"] = "*"
76
78
  endpoint = endpoints.sample
77
79
  @params = params
78
- puts "#{endpoint_base.method} #{endpoint_base.path} ?#{@params}" unless ENV['ignore_path'] && ("/#{ENV['ignore_path']}" == endpoint_base.path)
80
+ puts endpoint.log_call(@params) unless ENV['ignore_path'] && ("/#{ENV['ignore_path']}" == endpoint_base.path)
79
81
  serve endpoint
80
82
  end
81
83
  end
82
84
  end
85
+ # options endpoint for CORS
86
+ options endpoint_path do
87
+ puts "options: #{endpoint_path}"
88
+ response["Allow"] = "*"
89
+ response["Access-Control-Allow-Origin"] = "*"
90
+ response["Access-Control-Allow-Methods"] = "*"
91
+ response["Access-Control-Allow-Headers"] = "*"
92
+ end
83
93
  end
84
94
  end
data/lib/file_sv.rb CHANGED
@@ -30,12 +30,18 @@ CONFIG_FILE = "file_sv.yaml"
30
30
 
31
31
  # Set values in global settings based on config
32
32
  def load_default_config(file_path)
33
- return unless File.exist? file_path
33
+ unless File.exist? file_path
34
+ puts "No config found at #{file_path}" if ENV['debug'] == "true"
35
+ return
36
+ end
37
+ puts "Loading config from #{file_path}" if ENV['debug'] == "true"
34
38
 
35
39
  config = YAML.load_file file_path
36
40
  return unless config # Handle empty YAML file
37
41
 
42
+ puts "Config #{config}" if ENV['debug'] == "true"
38
43
  config["global"]&.each do |key, value|
44
+ puts "Setting #{key} to #{value}" if ENV['debug'] == "true"
39
45
  GlobalSettings.send("#{key}=", value)
40
46
  end
41
47
 
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.12
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Garratt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-22 00:00:00.000000000 Z
11
+ date: 2024-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faker
@@ -109,6 +109,7 @@ files:
109
109
  - lib/file_sv/file_processor.rb
110
110
  - lib/file_sv/file_sv.ico
111
111
  - lib/file_sv/global_settings.rb
112
+ - lib/file_sv/har_generator.rb
112
113
  - lib/file_sv/planned_endpoint.rb
113
114
  - lib/file_sv/render_file.rb
114
115
  - lib/file_sv/service_loader.rb