Class: HarGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/file_sv/har_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#contentString

Returns Har file content.

Returns:

  • (String)

    Har file content



13
14
15
# File 'lib/file_sv/har_generator.rb', line 13

def content
  @content
end

#entriesObject

Returns the value of attribute entries.



15
16
17
# File 'lib/file_sv/har_generator.rb', line 15

def entries
  @entries
end

#har_file_locationObject

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_baseObject

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_folderObject

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.

Parameters:

  • folder (String)

    Folder to create



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

#generateObject



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

#parse_harObject



25
26
27
28
29
30
# File 'lib/file_sv/har_generator.rb', line 25

def parse_har
	parsed = JSON.parse(self.content)
	pages = parsed['log']['pages']
	self.entries = parsed['log']['entries']
	FileSv.logger.info "Found #{self.entries.count} entries in #{self.har_file_location}"
end