Class: Desi::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/desi/http_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ HttpClient

Returns a new instance of HttpClient.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/desi/http_client.rb', line 11

def initialize(host)
  @uri = to_uri(host)

  case @uri.scheme
  when 'https'
    @http = ::Net::HTTP.new(@uri.host, 443)
    @http.use_ssl = true
    @http.verify_mode = ::OpenSSL::SSL::VERIFY_PEER
  when 'http'
    @http = ::Net::HTTP.new(@uri.host, @uri.port)
  else
    raise ArgumentError, "Won't process scheme #{@uri.scheme}"
  end
end

Instance Method Details

#delete(uri) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/desi/http_client.rb', line 52

def delete(uri)
  response = @http.request(Net::HTTP::Delete.new(uri))

  case response
    when Net::HTTPSuccess
      response
    else
      raise response.error!
  end
end

#get(uri, limit = 5) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/desi/http_client.rb', line 26

def get(uri, limit = 5)
  raise "Too many HTTP redirects!" if limit <= 0

  response = @http.request(Net::HTTP::Get.new(uri))

  case response
    when Net::HTTPSuccess
      response
    when Net::HTTPRedirection
      get(response['location'], limit - 1)
    else
      raise response.error!
  end
end

#post(uri) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/desi/http_client.rb', line 41

def post(uri)
  response = @http.request(Net::HTTP::Post.new(uri))

  case response
    when Net::HTTPSuccess
      response
    else
      raise response.error!
  end
end