Class: K3cloud::Http
- Inherits:
-
Object
- Object
- K3cloud::Http
- Defined in:
- lib/k3cloud/http.rb
Overview
HTTP Client
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#connect_timeout ⇒ Object
Returns the value of attribute connect_timeout.
-
#header ⇒ Object
Returns the value of attribute header.
-
#request_timeout ⇒ Object
Returns the value of attribute request_timeout.
-
#status_code ⇒ Object
Returns the value of attribute status_code.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(url, header, body, connect_timeout = 120, request_timeout = 120) ⇒ Http
constructor
A new instance of Http.
- #post ⇒ Object
Constructor Details
#initialize(url, header, body, connect_timeout = 120, request_timeout = 120) ⇒ Http
Returns a new instance of Http.
14 15 16 17 18 19 20 |
# File 'lib/k3cloud/http.rb', line 14 def initialize(url, header, body, connect_timeout = 120, request_timeout = 120) @url = url @header = header || {} @body = body @connect_timeout = connect_timeout @request_timeout = request_timeout end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
12 13 14 |
# File 'lib/k3cloud/http.rb', line 12 def body @body end |
#connect_timeout ⇒ Object
Returns the value of attribute connect_timeout.
12 13 14 |
# File 'lib/k3cloud/http.rb', line 12 def connect_timeout @connect_timeout end |
#header ⇒ Object
Returns the value of attribute header.
12 13 14 |
# File 'lib/k3cloud/http.rb', line 12 def header @header end |
#request_timeout ⇒ Object
Returns the value of attribute request_timeout.
12 13 14 |
# File 'lib/k3cloud/http.rb', line 12 def request_timeout @request_timeout end |
#status_code ⇒ Object
Returns the value of attribute status_code.
12 13 14 |
# File 'lib/k3cloud/http.rb', line 12 def status_code @status_code end |
#url ⇒ Object
Returns the value of attribute url.
12 13 14 |
# File 'lib/k3cloud/http.rb', line 12 def url @url end |
Instance Method Details
#post ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/k3cloud/http.rb', line 22 def post uri = URI.parse(@url) http = Net::HTTP.new(uri.host, uri.port) # SSL/TLS configuration if uri.scheme == "https" http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http.open_timeout = @connect_timeout http.read_timeout = @request_timeout request = Net::HTTP::Post.new(uri.request_uri) request.initialize_http_header(@header) request["Content-Type"] = "application/json" request["User-Agent"] = generate_user_agent request.body = @body.is_a?(String) ? @body : @body.to_json response = http.request(request) @status_code = response.code.to_i raise K3cloud::ResponseError, "status: #{response.code}, desc: #{response.body}" if @status_code >= 400 response.body rescue Errno::ETIMEDOUT, Net::OpenTimeout, Net::ReadTimeout => e raise K3cloud::ResponseError, "Request timed out: #{e.}" rescue Net::HTTPServerError => e raise K3cloud::ResponseError, "Server error: #{e.}" rescue => e raise K3cloud::ResponseError, "Unexpected error: #{e.}" end |