Class: HarGenerator
- Inherits:
-
Object
- Object
- HarGenerator
- Defined in:
- lib/file_sv/har_generator.rb
Instance Attribute Summary collapse
-
#content ⇒ String
Har file content.
-
#entries ⇒ Object
Returns the value of attribute entries.
-
#har_file_location ⇒ Object
Returns the value of attribute har_file_location.
-
#mock_service_base ⇒ Object
Returns the value of attribute mock_service_base.
-
#output_folder ⇒ Object
Returns the value of attribute output_folder.
Instance Method Summary collapse
- #create_file_at(file_path, extension, content) ⇒ Object
-
#create_folder(folder) ⇒ Object
Create folder if there’s not a file already there.
- #generate ⇒ Object
-
#initialize(har_file_location, mock_service_base, output_folder) ⇒ HarGenerator
constructor
A new instance of HarGenerator.
- #parse_har ⇒ Object
Constructor Details
#initialize(har_file_location, mock_service_base, output_folder) ⇒ HarGenerator
Returns a new instance of HarGenerator.
17 18 19 20 21 22 23 |
# File 'lib/file_sv/har_generator.rb', line 17 def initialize(har_file_location, mock_service_base, output_folder) self.har_file_location = har_file_location self.mock_service_base = mock_service_base self.output_folder = output_folder self.content = File.read(har_file_location) parse_har end |
Instance Attribute Details
#content ⇒ String
Returns Har file content.
13 14 15 |
# File 'lib/file_sv/har_generator.rb', line 13 def content @content end |
#entries ⇒ Object
Returns the value of attribute entries.
15 16 17 |
# File 'lib/file_sv/har_generator.rb', line 15 def entries @entries end |
#har_file_location ⇒ Object
Returns the value of attribute har_file_location.
7 8 9 |
# File 'lib/file_sv/har_generator.rb', line 7 def har_file_location @har_file_location end |
#mock_service_base ⇒ Object
Returns the value of attribute mock_service_base.
9 10 11 |
# File 'lib/file_sv/har_generator.rb', line 9 def mock_service_base @mock_service_base end |
#output_folder ⇒ Object
Returns the value of attribute output_folder.
11 12 13 |
# File 'lib/file_sv/har_generator.rb', line 11 def output_folder @output_folder end |
Instance Method Details
#create_file_at(file_path, extension, content) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/file_sv/har_generator.rb', line 70 def create_file_at(file_path, extension, content) file_path = file_path.gsub('//', '/') folder_name = File.split(file_path).first folder_name += ".#{extension}" if (extension == '.css' || extension == '.js') create_folder folder_name puts "Creating response at #{file_path}" File.open("#{folder_name}/#{File.split(file_path).last}", 'w') { |f| f.puts content } end |
#create_folder(folder) ⇒ Object
Create folder if there’s not a file already there. Will create parent folder if necessary.
61 62 63 64 65 66 67 68 |
# File 'lib/file_sv/har_generator.rb', line 61 def create_folder(folder) if File.exist? folder warn "!! #{folder} already exists and is not a directory" unless File.directory? folder else FileUtils.mkdir_p folder puts "Created folder: #{folder}" end end |
#generate ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/file_sv/har_generator.rb', line 32 def generate self.entries.each do |entry| request = entry['request'] response = entry['response'] request_url = request['url'] if (!request_url.include?(self.mock_service_base)) next; end path = request_url[self.mock_service_base.length..-1] path_folder = path.split('.')[0] method = request['method'] status_code = response['status'] content = response['content'] mime_type = content['mimeType'] extension = mime_type.split('/').last extension = 'js' if (extension == 'javascript') location = "#{self.output_folder}/#{path_folder}/#{method}_#{status_code}.#{extension}" create_file_at "./#{location}", extension, content['text'] end end |