class Expedition::Client

Attributes

host[RW]

@return [String]

The host this client will execute commands on.
port[RW]

@return [Integer]

The host port this client will connect to.

Public Class Methods

new(host = 'localhost', port = 4028) click to toggle source

Initializes a new ‘Client` for executing commands.

@param [String] host

The host to connect to.

@param [Integer] port

The port to connect to.
# File lib/expedition/client.rb, line 27
def initialize(host = 'localhost', port = 4028)
  @host = host
  @port = port
end

Public Instance Methods

devices() click to toggle source

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

@return [Response]

An array of devices.
# 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
method_missing(command, *parameters, &block)
Alias for: send
metrics() click to toggle source

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

@return [Response]

An array of metrics.
# File lib/expedition/client.rb, line 54
def metrics
  send(:devs) do |body|
    body[:devs].collect(&method(:parse_metrics))
  end
end
pools() click to toggle source

Sends the ‘pools` command, returning an array of pools found in the service’s response.

@return [Response]

An array of pools.
# 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) click to toggle source

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.

@param [Symbol, String] command

The command to send to the service.

@param [Array] parameters

Optional parameters to send to the service.

@return [Response]

The service's response.
# 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
Also aliased as: method_missing

Private Instance Methods

command_json(command, *parameters) click to toggle source
# File lib/expedition/client.rb, line 104
def command_json(command, *parameters)
  MultiJson.dump(command: command, parameter: parameters.join(','))
end
parse(response, &block) click to toggle source
# File lib/expedition/client.rb, line 100
def parse(response, &block)
  Response.parse(MultiJson.load(response.chomp("\x0")), &block)
end
parse_metrics(attrs) click to toggle source
# File lib/expedition/client.rb, line 108
def parse_metrics(attrs)
  attrs.merge!(
    enabled: attrs[:enabled] == 'Y',
    status: attrs[:status].downcase,
    last_share_time: (Time.at(attrs[:last_share_time]) rescue attrs[:last_share_time]),
    last_valid_work: (Time.at(attrs[:last_valid_work]) rescue attrs[:last_valid_work])
  )

  interval = attrs.keys.detect { |k| k =~ /^mhs_\d+s$/ }[/\d+s$/]

  # Remove the `_Ns` suffix from hash rate fields.
  attrs.merge!(
    mhs: attrs.delete("mhs_#{interval}"),
    khs: attrs.delete("khs_#{interval}")
  )
end
parse_pool(attrs) click to toggle source
# File lib/expedition/client.rb, line 125
def parse_pool(attrs)
  attrs.merge!(
    status: attrs[:status].downcase,
    long_poll: attrs[:long_poll] != 'N',
    last_share_time: (Time.at(attrs[:last_share_time]) rescue attrs[:last_share_time]),
  )
end