Sha256: dbf2e495e5ffb0219f8dbf3657ec7621232f41aa32f977c36af41df4d2c350ba
Contents?: true
Size: 1.88 KB
Versions: 1
Compression:
Stored size: 1.88 KB
Contents
require 'pact/retry' module Pact module Hal class HttpClient attr_accessor :username, :password def initialize options @username = options[:username] @password = options[:password] end def get href, params = {}, headers = {} query = params.collect{ |(key, value)| "#{CGI::escape(key)}=#{CGI::escape(value)}" }.join("&") uri = URI(href) uri.query = query perform_request(create_request(uri, 'Get', nil, headers), uri) end def put href, body = nil, headers = {} uri = URI(href) perform_request(create_request(uri, 'Put', body, headers), uri) end def post href, body = nil, headers = {} uri = URI(href) perform_request(create_request(uri, 'Post', body, headers), uri) end def create_request uri, http_method, body = nil, headers = {} request = Net::HTTP.const_get(http_method).new(uri.request_uri) request['Content-Type'] = "application/json" if ['Post', 'Put', 'Patch'].include?(http_method) request['Accept'] = "application/hal+json" headers.each do | key, value | request[key] = value end request.body = body if body request.basic_auth username, password if username request end def perform_request request, uri options = {:use_ssl => uri.scheme == 'https'} response = Retry.until_true do Net::HTTP.start(uri.host, uri.port, :ENV, options) do |http| http.request request end end Response.new(response) end class Response < SimpleDelegator def body bod = __getobj__().body if bod && bod != '' JSON.parse(bod) else nil end end def success? __getobj__().code.start_with?("2") end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
pact-1.27.0 | lib/pact/hal/http_client.rb |