Class: Hkp::Client
- Inherits:
-
Object
- Object
- Hkp::Client
- Defined in:
- lib/hkp.rb
Constant Summary collapse
- MAX_REDIRECTS =
3
Instance Method Summary collapse
- #get(path, redirect_depth = 0) ⇒ Object
-
#initialize(server, ssl_verify_mode: OpenSSL::SSL::VERIFY_PEER) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(server, ssl_verify_mode: OpenSSL::SSL::VERIFY_PEER) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/hkp.rb', line 17 def initialize(server, ssl_verify_mode: OpenSSL::SSL::VERIFY_PEER) uri = URI server @host = uri.host @port = uri.port @use_ssl = false @ssl_verify_mode = ssl_verify_mode # set port and ssl flag according to URI scheme case uri.scheme.downcase when 'hkp' # use the HKP default port unless another port has been given @port ||= 11371 when /\A(hkp|http)s\z/ # hkps goes through 443 by default @port ||= 443 @use_ssl = true end @port ||= 80 end |
Instance Method Details
#get(path, redirect_depth = 0) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/hkp.rb', line 38 def get(path, redirect_depth = 0) Net::HTTP.start @host, @port, use_ssl: @use_ssl, verify_mode: @ssl_verify_mode do |http| request = Net::HTTP::Get.new path response = http.request request case response.code.to_i when 200 return response.body when 301, 302 if redirect_depth >= MAX_REDIRECTS raise TooManyRedirects else http_get response['location'], redirect_depth + 1 end else raise InvalidResponse, response.code end end end |