Class: ServerStatusClient
- Inherits:
-
Object
- Object
- ServerStatusClient
- Defined in:
- lib/server_status/client.rb
Instance Method Summary collapse
-
#initialize(hash) ⇒ ServerStatusClient
constructor
A new instance of ServerStatusClient.
-
#is_port_open? ⇒ Boolean
noinspection RubyResolve.
- #request ⇒ Object
Constructor Details
#initialize(hash) ⇒ ServerStatusClient
Returns a new instance of ServerStatusClient.
6 7 8 9 10 11 12 13 14 |
# File 'lib/server_status/client.rb', line 6 def initialize(hash) @domain = hash[:domain.to_s] @port = hash[:port.to_s] @scheme = hash[:ssl.to_s] ? 'https' : 'http' @url = "#@scheme://#@domain:#@port" @default = hash[:apis.to_s][:default.to_s] @payload = @default[:payload.to_s] @payload ||= {} end |
Instance Method Details
#is_port_open? ⇒ Boolean
noinspection RubyResolve
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/server_status/client.rb', line 55 def is_port_open? Timeout::timeout(1) do begin s = TCPSocket.new(@domain, @port) s.close return true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH return false end end rescue Timeout::Error return false end |
#request ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/server_status/client.rb', line 16 def request header, method, params = parse_params Timeout::timeout(5) do begin case method when :get header[:params] = params response = RestClient.get "#@url#{@default[:url.to_s]}", header when :post response = RestClient.post "#@url#{@default[:url.to_s]}", params.to_json, header end response.code rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH return 503 rescue => e e.response.code end end rescue Timeout::Error return 408 end |