Class: HTTP::Client

Inherits:
Object show all
Defined in:
lib/http_client/mri/client.rb,
lib/http_client/jruby/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



3
4
5
6
7
# File 'lib/http_client/mri/client.rb', line 3

def initialize(options)
  @default_host = options[:default_host]
  @timeout_in_seconds = options[:timeout_in_seconds]
  @uri = URI.parse(@default_host)
end

Instance Method Details

#delete(path) ⇒ Object



17
18
19
# File 'lib/http_client/mri/client.rb', line 17

def delete(url)
  execute(HTTP::Delete.new(url)).body
end

#execute(request) ⇒ Object



25
26
27
# File 'lib/http_client/mri/client.rb', line 25

def execute(req)
  req.execute_native_request(self)
end

#execute_request(requested_host, requested_port, req) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/http_client/mri/client.rb', line 29

def execute_request(requested_host, requested_port, req)
  host = requested_host || @uri.host
  port = requested_port || @uri.port
  req['Cookie'] = @cookie unless @cookie.nil?

  Net::HTTP.new(host, port).start do |http|
    http.read_timeout = @timeout_in_seconds || 30
    response = http.request(req)

    if response['location']
      redirect_uri = URI.parse(response['location'])
      return execute_request(redirect_uri.host, redirect_uri.port,  Net::HTTP::Get.new("#{redirect_uri.path}?#{redirect_uri.query}"))
    end

    @cookie = response['set-cookie']

    response
  end
end

#get(params, options = {}) ⇒ Object



9
10
11
# File 'lib/http_client/mri/client.rb', line 9

def get(url, params = {})
  execute(HTTP::Get.new(url, params)).body
end

#head(params, options = {}) ⇒ Object



16
17
18
# File 'lib/http_client/jruby/client.rb', line 16

def head(params, options = {})
  read_response(Head.new(params, options))
end

#options(params, options = {}) ⇒ Object



20
21
22
# File 'lib/http_client/jruby/client.rb', line 20

def options(params, options = {})
  read_response(Options.new(params, options))
end

#post(params, options = {}) ⇒ Object



13
14
15
# File 'lib/http_client/mri/client.rb', line 13

def post(url, params = {})
  execute(HTTP::Post.new(url, params)).body
end

#put(path) ⇒ Object



21
22
23
# File 'lib/http_client/mri/client.rb', line 21

def put(url)
  execute(HTTP::Put.new(url)).body
end

#read_response(request) ⇒ Object



36
37
38
# File 'lib/http_client/jruby/client.rb', line 36

def read_response(request)
  execute(request).body
end

#shutdownObject



49
# File 'lib/http_client/mri/client.rb', line 49

def shutdown; end