Class: Expedition::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = 'localhost', port = 4028) ⇒ Client

Initializes a new Client for executing commands.

Parameters:

  • host (String) (defaults to: 'localhost')

    The host to connect to.

  • port (Integer) (defaults to: 4028)

    The port to connect to.



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_missingResponse

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.

Parameters:

  • command (Symbol, String)

    The command to send to the service.

  • parameters (Array)

    Optional parameters to send to the service.

Returns:

  • (Response)

    The service's response.



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

#hostString

Returns The host this client will execute commands on.

Returns:

  • (String)

    The host this client will execute commands on.



12
13
14
# File 'lib/expedition/client.rb', line 12

def host
  @host
end

#portInteger

Returns The host port this client will connect to.

Returns:

  • (Integer)

    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

#devicesResponse

Sends the devdetails command, returning an array of devices found in the service's response.

Returns:



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

#metricsResponse

Sends the devs command, returning an array of metrics found in the service's response.

Returns:



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

#poolsResponse

Sends the pools command, returning an array of pools found in the service's response.

Returns:



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.

Parameters:

  • command (Symbol, String)

    The command to send to the service.

  • parameters (Array)

    Optional parameters to send to the service.

Returns:

  • (Response)

    The service's response.



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