Class: Barthes::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/barthes/action.rb

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Action

Returns a new instance of Action.



7
8
9
10
11
# File 'lib/barthes/action.rb', line 7

def initialize(env)
	@env = env.dup
	client_class = @env['client_class'] ? @env['client_class'].constantize : Barthes::Client::HTTParty
	@client = client_class.new(evaluate_params env)
end

Instance Method Details

#action(action) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/barthes/action.rb', line 17

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'].empty?
			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'
			else
				action['status'] = 'success'
			end
		else
			action['status'] = 'success'
		end

		if cache_config = action['cache']
			value = @client.extract(evaluate_params(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

#evaluate_params(params) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/barthes/action.rb', line 74

def evaluate_params(params)
	new_params = {}
	params.each do |k, v|
		if v.class == String
			new_v = v.gsub(/\$\{time:(.+?):(.+?)\}/) { Chronic.parse($1).strftime($2) }
			new_v = new_v.gsub(/^\$\{base64:(.+)\}$/m) do
				match = $1
				match = match.gsub(/\$\{(.+?)\}/) { @env[$1] }
				match = match.gsub(/\@\{(.+?)\}/) { Barthes::Cache[$1] }
				Base64.encode64(match)
			end
			new_v = new_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

#indent(size, string) ⇒ Object



13
14
15
# File 'lib/barthes/action.rb', line 13

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