Class: Barthes::Client::HTTParty

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/barthes/client/httparty.rb

Direct Known Subclasses

Ace

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ HTTParty

Returns a new instance of HTTParty.



9
10
11
12
# File 'lib/barthes/client/httparty.rb', line 9

def initialize(env)
	@env = env
	set_proxy
end

Instance Method Details

#action(params) ⇒ Object



21
22
23
24
25
26
# File 'lib/barthes/client/httparty.rb', line 21

def action(params)
	url = @env['path'] ? @env['endpoint'] + @env['path'] : @env['endpoint']
	headers = @env['header'] ? @env['header'] : {}
	# TODO: method
	self.class.post(url, query: params, headers: headers)
end

#compare(response, expectation) ⇒ Object



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
# File 'lib/barthes/client/httparty.rb', line 28

def compare(response, expectation)
	result = nil
	case expectation['type']
	when 'response_code'
		result = response.code == expectation['value']
		{'result' => result, 'returned' => response.code, 'expected' => expectation['value']}
	when 'xpath_value'
		text = xpath(response, expectation['xpath']).text
		if expectation['method'].nil? || expectation['method'] == 'eq'
			result = (text == expectation['value'])
     		                elsif expectation['method'] == 'regexp'
			result = !(text.match Regexp.new(expectation['value'])).nil?
		elsif expectation['method'] == 'ne'
			result = (text != expectation['value'])
		end
		{'result' => result, 'returned' => text, 'expected' => expectation['value']}
	when 'xpath_size'
		size = xpath(response, expectation['xpath']).size
		if expectation['method'].nil? || expectation['method'] == 'eq'
			result = (size == expectation['value'])
		elsif expectation['method'] == 'gt'
			result = (size > expectation['value'])
		elsif expectation['method'] == 'gte'
			result = (size >= expectation['value'])
		elsif expectation['method'] == 'lt'
			result = (size < expectation['value'])
		elsif expectation['method'] == 'lte'
			result = (size <= expectation['value'])
		end
		{'result' => result, 'returned' => size, 'expected' => expectation['value']}
	else
		{'result' => true}
	end
end

#extract(config, response) ⇒ Object



63
64
65
66
67
# File 'lib/barthes/client/httparty.rb', line 63

def extract(config, response)
	if config['xpath']
		xpath(response, config['xpath']).text
	end
end

#set_proxyObject



14
15
16
17
18
19
# File 'lib/barthes/client/httparty.rb', line 14

def set_proxy
	if proxy_uri = ENV['HTTP_PROXY']
		uri = URI.parse(proxy_uri)
		self.class.http_proxy uri.host, uri.port
	end
end

#xpath(response, xpath) ⇒ Object



69
70
71
72
# File 'lib/barthes/client/httparty.rb', line 69

def xpath(response, xpath)
	doc = Nokogiri::XML response.body.gsub(/xmlns="(.+?)"/, '')
	doc.xpath(xpath)
end