class RedHatSupportLib::Network::HttpConnection

Constants

USER_AGENT

Attributes

config[R]

Public Class Methods

new(config) click to toggle source
# File lib/network/http_connection.rb, line 14
def initialize(config)
  @config = config
  user_agent = USER_AGENT
  unless config.user_agent.nil?
    user_agent = config.user_agent
  end
  @additional_headers = {:user_agent => user_agent}
  #unless config.proxy_host.nil? || config.proxy_host.strip.empty?
  #  RestClient.proxy = "http://#{config.proxy_user}:#{config.proxy_password}@#{config.proxy_host}:#{config.proxy_port}"
  #end
  unless config.log_location.nil?
    RestClient.log = config.log_location
  end
end

Public Instance Methods

delete(relative_uri, headers={}) { |code, headers| ... } click to toggle source
# File lib/network/http_connection.rb, line 57
def delete(relative_uri, headers={}, &block)

  hdr = @additional_headers.merge!(headers)
  result = get_resource(relative_uri).delete hdr
  if block
    yield result.code, result.headers
  end
end
get(relative_uri, headers={}, &block) click to toggle source
# File lib/network/http_connection.rb, line 29
def get(relative_uri, headers={}, &block)

  hdr = @additional_headers.merge!(headers)
  result = get_resource(relative_uri).get hdr
  parse_response(result.body, headers[:accept])

end
post(relative_uri, data, headers={}) { |code, headers| ... } click to toggle source
# File lib/network/http_connection.rb, line 37
def post(relative_uri, data, headers={}, &block)

  hdr = @additional_headers.merge!(headers)
  result = get_resource(relative_uri).post data, hdr
  if block
    yield result.code, result.headers
  end
  parse_response(result.body, headers[:accept])
end
put(relative_uri, data, headers={}) { |code, headers| ... } click to toggle source
# File lib/network/http_connection.rb, line 47
def put(relative_uri, data, headers={}, &block)

  hdr = @additional_headers.merge!(headers)
  result = get_resource(relative_uri).put data, hdr
  if block
    yield result.code, result.headers
  end
  parse_response(result.body, headers[:accept])
end
upload(relative_uri, file, headers={}) { |code, headers| ... } click to toggle source

upload a file as multipart

# File lib/network/http_connection.rb, line 67
def upload(relative_uri, file, headers={}, &block)
  request = RedHatSupportLib::Network::HttpRequest.new(
      :headers => headers,
      :method => :post,
      :url => "#{@config.base_uri}#{relative_uri}",
      :user => @config.username,
      :password => @config.password,
      :payload => {
          :multipart => true,
          :file => file
      },
      :proxy => config.proxy
  )

  result = request.execute
  if block
    yield result.code, result.headers
  end
end

Private Instance Methods

get_resource(relative_uri) click to toggle source
# File lib/network/http_connection.rb, line 88
def get_resource(relative_uri)
  resource = RedHatSupportLib::Network::HttpResource.new("#{@config.base_uri}#{relative_uri}",
                                                         {:user => @config.username,
                                                          :password => @config.password,
                                                          :proxy => config.proxy})
end
parse_response(body, format) click to toggle source
# File lib/network/http_connection.rb, line 95
def parse_response(body, format)
  return nil if body.nil? || body.strip.empty? || body == "null"
  if (format == :json)
    return JSON.parse(body)
  end
  body # for now do nothing for other formats
end