Class: Expedition::Client
- Inherits:
-
Object
- Object
- Expedition::Client
- Defined in:
- lib/expedition/client.rb
Instance Attribute Summary collapse
-
#host ⇒ String
The host this client will execute commands on.
-
#port ⇒ Integer
The host port this client will connect to.
Instance Method Summary collapse
-
#devices ⇒ Response
Sends the
devdetailscommand, returning an array of devices found in the service's response. -
#initialize(host = 'localhost', port = 4028) ⇒ Client
constructor
Initializes a new
Clientfor executing commands. -
#metrics ⇒ Response
Sends the
devscommand, returning an array of metrics found in the service's response. -
#pools ⇒ Response
Sends the
poolscommand, returning an array of pools found in the service's response. -
#send(command, *parameters, &block) ⇒ Response
(also: #method_missing)
Sends the supplied
commandwith optionally suppliedparametersto the service and returns the result, if any.
Constructor Details
#initialize(host = 'localhost', port = 4028) ⇒ Client
Initializes a new Client for executing commands.
27 28 29 30 |
# File 'lib/expedition/client.rb', line 27 def initialize(host = 'localhost', port = 4028) @host = host @port = port end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing ⇒ Response
Sends the supplied command with optionally supplied parameters to the
service and returns the result, if any.
Note: Since Object#send is overridden, use Object#__send__ to call
an actual method.
96 97 98 99 100 101 102 103 |
# File 'lib/expedition/client.rb', line 96 def send(command, *parameters, &block) socket = TCPSocket.open(host, port) socket.puts command_json(command, *parameters) parse(socket.gets, &block) ensure socket.close if socket.respond_to?(:close) end |
Instance Attribute Details
#host ⇒ String
Returns The host this client will execute commands on.
12 13 14 |
# File 'lib/expedition/client.rb', line 12 def host @host end |
#port ⇒ Integer
Returns The host port this client will connect to.
17 18 19 |
# File 'lib/expedition/client.rb', line 17 def port @port end |
Instance Method Details
#devices ⇒ Response
Sends the devdetails command, returning an array of devices found in the
service's response.
38 39 40 41 42 43 44 45 46 |
# File 'lib/expedition/client.rb', line 38 def devices send(:devdetails) do |body| body[:devdetails].collect { |attrs| attrs.delete(:devdetails) attrs[:variant] = attrs.delete(:name).downcase attrs } end end |
#metrics ⇒ Response
Sends the devs command, returning an array of metrics found in the
service's response.
54 55 56 57 58 |
# File 'lib/expedition/client.rb', line 54 def metrics send(:devs) do |body| body[:devs].collect(&method(:parse_metrics)) end end |
#pools ⇒ Response
Sends the pools command, returning an array of pools found in the
service's response.
66 67 68 69 70 |
# File 'lib/expedition/client.rb', line 66 def pools send(:pools) do |body| body[:pools].collect(&method(:parse_pool)) end end |
#send(command, *parameters, &block) ⇒ Response Also known as: method_missing
Sends the supplied command with optionally supplied parameters to the
service and returns the result, if any.
Note: Since Object#send is overridden, use Object#__send__ to call
an actual method.
87 88 89 90 91 92 93 94 |
# File 'lib/expedition/client.rb', line 87 def send(command, *parameters, &block) socket = TCPSocket.open(host, port) socket.puts command_json(command, *parameters) parse(socket.gets, &block) ensure socket.close if socket.respond_to?(:close) end |