Class: RedHatSupportLib::Network::HttpConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/network/http_connection.rb

Constant Summary collapse

USER_AGENT =
"ruby-rest-client"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ HttpConnection

Returns a new instance of HttpConnection.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/network/http_connection.rb', line 12

def config
  @config
end

Instance Method Details

#delete(relative_uri, headers = {}, &block) ⇒ Object



57
58
59
60
61
62
63
64
# 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) ⇒ Object



29
30
31
32
33
34
35
# 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 = {}, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
# 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 = {}, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
# 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 = {}, &block) ⇒ Object

upload a file as multipart



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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