Class: Mixergy::Client
- Inherits:
-
Object
- Object
- Mixergy::Client
- Defined in:
- lib/mixergy/client.rb
Constant Summary collapse
- API_ROOT =
"https://www.mixergy.io/api/v2"
Instance Method Summary collapse
-
#default_tank_id ⇒ String?
Returns the default tank ID from config, or the first tank’s ID.
-
#initialize ⇒ Client
constructor
Create a new Mixergy API client.
-
#load_config ⇒ Mixergy::Config
Loads configuration from ~/.mixergy and sets the API token if present.
-
#login(username, password) ⇒ String
Authenticates with the Mixergy API and stores the token in the connection.
-
#set_charge(percent, tank = nil) ⇒ Boolean
Sets the target charge for a tank via the control endpoint.
-
#status(tank = nil) ⇒ Status
Fetches the latest status/measurement for a tank.
-
#tanks ⇒ Array<Tank>
Fetches all tanks associated with the account.
Constructor Details
#initialize ⇒ Client
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_id ⇒ String?
Returns the default tank ID from config, or the first tank’s 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_config ⇒ Mixergy::Config
Loads configuration from ~/.mixergy and sets the API token if present.
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.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mixergy/client.rb', line 41 def login(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.
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.
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 |
#tanks ⇒ Array<Tank>
Fetches all tanks associated with the account.
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 |