<commit_before>require 'barthes/cache'

module Barthes
	class Action
		def initialize(env)
			@env = env.dup
			client_class = Object.const_get(@env['client_class'])
			@client = client_class.new(env)
		end

		def indent(size, string)
			string.split("\n").map {|line| "\t" * size + "#{line}\n" }
		end

		def action(action)
			@env.update(action['env']) if action['env']
			params = evaluate_params(action['params'])

			if action['expectations']
				if action['max_loop']
					action['max_loop'].to_i.times do
						sleep action['sleep'].to_i/1000 if action['sleep']	

						response = @client.action(params)
						action['expectations'].each do |expectation|
							result = @client.compare(response, evaluate_params(expectation))
							expectation.update(result)
						end
						if action['expectations'].all? {|e| e['result'] == true }
							break
						end
					end
				end
			end

			sleep action['sleep'].to_i/1000 if action['sleep']	

			action['request'] = params
			action['response'] = response = @client.action(params)

			if action['expectations']
				action['expectations'].each do |expectation|
					result = @client.compare(response, evaluate_params(expectation))
					expectation.update(result)
				end
			end

			if cache_config = action['cache']
				value = @client.extract(cache_config, response)
				action['cache']['value'] = value
				Barthes::Cache[cache_config['key']] = value
			end
			action
		end

		def evaluate_params(params)
			new_params = {}
			params.each do |k, v|
				if v.class == String
					new_v = v.gsub(/\$\{(.+?)\}/) { @env[$1] }
					new_v = new_v.gsub(/\@\{(.+?)\}/) { Barthes::Cache[$1] }
				else
					new_v = v
				end
				new_params[k] = new_v
			end
			new_params
		end
	end
end
<commit_msg>set action status<commit_after>require 'barthes/cache'

module Barthes
	class Action
		def initialize(env)
			@env = env.dup
			client_class = Object.const_get(@env['client_class'])
			@client = client_class.new(env)
		end

		def indent(size, string)
			string.split("\n").map {|line| "\t" * size + "#{line}\n" }
		end

		def action(action)
			begin
				@env.update(action['env']) if action['env']
				params = evaluate_params(action['params'])

				if action['expectations']
					if action['max_loop']
						action['max_loop'].to_i.times do
							sleep action['sleep'].to_i/1000 if action['sleep']	

							response = @client.action(params)
							action['expectations'].each do |expectation|
								result = @client.compare(response, evaluate_params(expectation))
								expectation.update(result)
							end
							if action['expectations'].all? {|e| e['result'] == true }
								break
							end
						end
					end
				end

				sleep action['sleep'].to_i/1000 if action['sleep']	

				action['request'] = params
				action['response'] = response = @client.action(params)

				if action['expectations']
					action['expectations'].each do |expectation|
						result = @client.compare(response, evaluate_params(expectation))
						expectation.update(result)
					end
					if action['expectations'].all? {|e| e['result'] == true }
						action['status'] = 'failure'
					end
				else
					action['status'] = 'success'
				end

				if cache_config = action['cache']
					value = @client.extract(cache_config, response)
					action['cache']['value'] = value
					Barthes::Cache[cache_config['key']] = value
				end
			rescue StandardError => e
				action['status'] = 'error'
				action['error'] = {
					'class' => e.class,
					'message' => e.message,
					'backtrace' => e.backtrace
				}
			end
			action
		end

		def evaluate_params(params)
			new_params = {}
			params.each do |k, v|
				if v.class == String
					new_v = v.gsub(/\$\{(.+?)\}/) { @env[$1] }
					new_v = new_v.gsub(/\@\{(.+?)\}/) { Barthes::Cache[$1] }
				else
					new_v = v
				end
				new_params[k] = new_v
			end
			new_params
		end
	end
end

{'fraction_non_alphanumeric': 0.1575679172056921, 'fraction_numerical': 0.00517464424320828, 'mean_word_length': 2.969199178644764, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1640456', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1271, 'n_words': 269}