class Barthes::Client::HTTParty

Public Class Methods

new(env) click to toggle source
# File lib/barthes/client/httparty.rb, line 9
def initialize(env)
        @env = env
        set_proxy
end

Public Instance Methods

action(params) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/barthes/client/httparty.rb, line 63
def extract(config, response)
        if config['xpath']
                xpath(response, config['xpath']).text
        end
end
set_proxy() click to toggle source
# 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) click to toggle source
# File lib/barthes/client/httparty.rb, line 69
def xpath(response, xpath)
        doc = Nokogiri::XML response.body.gsub(/xmlns="(.+?)"/, '')
        doc.xpath(xpath)
end