Class: Mixergy::Client

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

Constant Summary collapse

API_ROOT =
"https://www.mixergy.io/api/v2"

Instance Method Summary collapse

Constructor Details

#initializeClient

Create a new Mixergy API client.



15
16
17
18
19
20
21
22
# File 'lib/mixergy/client.rb', line 15

def initialize
  @connection = Faraday.new(
    url: API_ROOT
  ) do |faraday|
    faraday.request :json      # Automatically encode request bodies as JSON
    faraday.response :json     # Automatically parse response bodies as JSON
  end
end

Instance Method Details

#default_tank_idString?

Returns the default tank ID from config, or the first tank’s ID.

Returns:

  • (String, nil)

    the default tank ID



68
69
70
71
72
73
# File 'lib/mixergy/client.rb', line 68

def default_tank_id
  @default_tank_id ||= begin
    load_config
    @config[:default_tank_id] || tanks.first&.id
  end
end

#load_configMixergy::Config

Loads configuration from ~/.mixergy and sets the API token if present.

Returns:



26
27
28
29
30
31
32
33
34
# File 'lib/mixergy/client.rb', line 26

def load_config
  @config ||= begin
    config = Mixergy::Config.new
    config.load
    # FIXME: find a better place to put this
    @connection.headers["Authorization"] = "Bearer #{config[:token]}"
    config
  end
end

#login(username, password) ⇒ String

Authenticates with the Mixergy API and stores the token in the connection.

Parameters:

  • username (String)

    the username

  • password (String)

    the password

Returns:

  • (String)

    the authentication token

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mixergy/client.rb', line 41

def (username, password)
  resp = @connection.post(
    "account/login",
    {username: username, password: password}
  )
  data = resp.body

  if data["token"]
    @connection.headers["Authorization"] = "Bearer #{data["token"]}"
    data["token"]
  else
    raise Mixergy::Error, "Login failed (status: #{resp.status}, body: #{resp.body.inspect})"
  end
end

#set_charge(percent, tank = nil) ⇒ Boolean

Sets the target charge for a tank via the control endpoint.

Parameters:

  • percent (Integer)

    the target charge percentage

  • tank (Tank, String, nil) (defaults to: nil)

    the Tank object or Tank GUID (optional)

Returns:

  • (Boolean)

    true if successful, false otherwise



89
90
91
92
93
94
# File 'lib/mixergy/client.rb', line 89

def set_charge(percent, tank = nil)
  tank_id = tank.id if tank.is_a?(Tank)
  tank_id = default_tank_id if tank_id.nil?
  resp = @connection.put("tanks/#{tank_id}/control", {charge: percent})
  resp.success?
end

#status(tank = nil) ⇒ Status

Fetches the latest status/measurement for a tank.

Parameters:

  • tank (Tank, nil) (defaults to: nil)

    the tank object (optional)

Returns:

  • (Status)

    the status object for the tank



78
79
80
81
82
83
# File 'lib/mixergy/client.rb', line 78

def status(tank = nil)
  tank_id = tank.id if tank.is_a?(Tank)
  tank_id = default_tank_id if tank_id.nil?
  resp = @connection.get("tanks/#{tank_id}/measurements/latest")
  Status.new(resp.body)
end

#tanksArray<Tank>

Fetches all tanks associated with the account.

Returns:

  • (Array<Tank>)

    list of Tank objects



58
59
60
61
62
63
64
# File 'lib/mixergy/client.rb', line 58

def tanks
  resp = @connection.get("tanks")
  tank_list = resp.body.dig("_embedded", "tankList") || []
  tank_list.map do |tank_data|
    Tank.new(tank_data)
  end
end