Skip to content

Instantly share code, notes, and snippets.

@adam12
Created September 15, 2022 14:58
Show Gist options
  • Save adam12/db4f109feb48cf20ebe4180f71823029 to your computer and use it in GitHub Desktop.
Save adam12/db4f109feb48cf20ebe4180f71823029 to your computer and use it in GitHub Desktop.
# frozen-string-literal: true
module Commands
class PingServer
def self.configure(receiver, attr_method = :ping_server, server:, api_key:)
instance = build(api_key: api_key, server: server)
receiver.public_send("#{attr_method}=", instance)
end
def self.build(api_key:, server:)
new(api_key).tap do |instance|
HttpClient.configure(instance, host_name: server)
end
end
attr_accessor :api_key
##
# Dependencies
attr_accessor :conn
def initialize(api_key)
self.api_key = api_key
end
def call
conn.get do |req|
req.url "/api/v1/servers/localhost"
req.headers["X-API-Key"] = api_key
req.headers["Accept"] = "application/json"
req.headers["Content-type"] = "application/json"
end.success?
rescue Faraday::ConnectionFailed
false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment