class Cloudhdr::HTTP::NetHTTP

Attributes

body[RW]
headers[RW]
method[RW]
options[RW]
params[RW]
skip_ssl_verify[RW]
timeout[RW]
uri[RW]
url[RW]

Public Class Methods

delete(url, options={}) click to toggle source
# File lib/cloudhdr/http/net_http.rb, line 49
def self.delete(url, options={})
  new(:delete, url, options).perform
end
get(url, options={}) click to toggle source
# File lib/cloudhdr/http/net_http.rb, line 45
def self.get(url, options={})
  new(:get, url, options).perform
end
new(method, url, options) click to toggle source
# File lib/cloudhdr/http/net_http.rb, line 26
def initialize(method, url, options)
  @method          = method
  @url             = url
  @body            = options.delete(:body)
  @params          = options.delete(:params)
  @headers         = options.delete(:headers)
  @timeout         = options.delete(:timeout)
  @skip_ssl_verify = options.delete(:skip_ssl_verify)
  @options         = options
end
post(url, options={}) click to toggle source
# File lib/cloudhdr/http/net_http.rb, line 37
def self.post(url, options={})
  new(:post, url, options).perform
end
put(url, options={}) click to toggle source
# File lib/cloudhdr/http/net_http.rb, line 41
def self.put(url, options={})
  new(:put, url, options).perform
end

Public Instance Methods

perform() click to toggle source
# File lib/cloudhdr/http/net_http.rb, line 53
def perform
  deliver(http, request)
end

Protected Instance Methods

deliver(http, request) click to toggle source
# File lib/cloudhdr/http/net_http.rb, line 60
def deliver(http, request)
  if timeout
    Timeout.timeout(timeout / 1000.0) do
      http.request(request)
    end
  else
    http.request(request)
  end
end
http() click to toggle source
# File lib/cloudhdr/http/net_http.rb, line 70
def http
  u = uri

  http = Net::HTTP.new(u.host, u.port)

  if u.scheme == 'https'
    http.use_ssl = true

    if !skip_ssl_verify && root_cert_path = locate_root_cert_path
      http.ca_path = root_cert_path
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
      http.verify_depth = 5
    else
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
  end

  http
end
locate_root_cert_path() click to toggle source
# File lib/cloudhdr/http/net_http.rb, line 134
def locate_root_cert_path
  self.class.root_cert_paths.detect do |root_cert_path|
    File.directory?(root_cert_path)
  end
end
path() click to toggle source
# File lib/cloudhdr/http/net_http.rb, line 116
def path
  u = uri

  if u.path.empty?
    u.path = '/'
  end

  if u.query.to_s.empty?
    u.path
  else
    u.path + '?' + u.query.to_s
  end
end
request() click to toggle source
# File lib/cloudhdr/http/net_http.rb, line 90
def request
  r = request_class.new(path)
  r.body = body if body
  if headers
    headers.each do |header, value|
      r.add_field(header.to_s, value.to_s)
    end
  end
  r
end
request_class() click to toggle source
# File lib/cloudhdr/http/net_http.rb, line 130
def request_class
  Net::HTTP.const_get(method.to_s.capitalize)
end