Class: CDNGet::HttpConnection

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, headers = nil) ⇒ HttpConnection

Returns a new instance of HttpConnection.



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

def initialize(uri, headers=nil)
  http = Net::HTTP.new(uri.host, uri.port)
  if uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  end
  http.start()
  @http = http
  @headers = headers
end

Class Method Details

.open(uri, headers = nil) ⇒ Object



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

def self.open(uri, headers=nil)
  http = self.new(uri, headers)
  return http unless block_given?()
  begin
    return yield http
  ensure
    http.close()
  end
end

Instance Method Details

#_get_resp_body(resp) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/cdnget.rb', line 82

def _get_resp_body(resp)
  case resp
  when Net::HTTPSuccess
    return resp.body
  else
    raise HttpError.new(resp.code.to_i, resp.message)
  end
end

#closeObject



91
92
93
# File 'lib/cdnget.rb', line 91

def close()
  @http.finish()
end

#get(uri) ⇒ Object



62
63
64
65
# File 'lib/cdnget.rb', line 62

def get(uri)
  resp = request('GET', uri.path, uri.query)
  return _get_resp_body(resp)
end

#post(uri, payload) ⇒ Object



67
68
69
70
# File 'lib/cdnget.rb', line 67

def post(uri, payload)
  resp = request('POST', uri.path, uri.query, payload: payload)
  return _get_resp_body(resp)
end

#request(meth, path, query = nil, payload: nil, headers: nil) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/cdnget.rb', line 72

def request(meth, path, query=nil, payload: nil, headers: nil)
  path += "?" + query if query
  if @headers
    headers ||= {}
    headers.update(@headers)
  end
  resp = @http.send_request(meth, path, payload, headers)
  return resp
end