Class: LightMeUp::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/light_me_up/api_client.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

DEFAULT_PORT =
9123
LIGHTS_PATH =
"/elgato/lights"
OPEN_TIMEOUT =

seconds

2
READ_TIMEOUT =

seconds

2
MAX_RETRIES =
2
TEMPERATURE_RANGE =
(143..344).freeze

Instance Method Summary collapse

Constructor Details

#initialize(ip_address:, retries: MAX_RETRIES, port: DEFAULT_PORT) ⇒ ApiClient

Returns a new instance of ApiClient.

Raises:



16
17
18
19
20
21
22
# File 'lib/light_me_up/api_client.rb', line 16

def initialize(ip_address:, retries: MAX_RETRIES, port: DEFAULT_PORT)
  raise Error, "No ip_address specified." unless ip_address && ip_address != ""

  @ip_address = ip_address
  @port = port
  @max_retries = retries
end

Instance Method Details

#statusObject



24
25
26
27
# File 'lib/light_me_up/api_client.rb', line 24

def status
  response = get(lights_uri)
  LightSerializer.deserialize(response).first
end

#toggle(brightness: nil, temperature: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/light_me_up/api_client.rb', line 29

def toggle(brightness: nil, temperature: nil)
  with_connection do |_http|
    current_status = status

    if current_status.on
      update(on: false, brightness: brightness, temperature: temperature)
    else
      update(on: true, brightness: brightness, temperature: temperature)
    end
  end
end

#turn_light_offObject



49
50
51
# File 'lib/light_me_up/api_client.rb', line 49

def turn_light_off
  update(on: false)
end

#turn_light_onObject



45
46
47
# File 'lib/light_me_up/api_client.rb', line 45

def turn_light_on
  update(on: true)
end

#update(on: nil, brightness: nil, temperature: nil) ⇒ Object



41
42
43
# File 'lib/light_me_up/api_client.rb', line 41

def update(on: nil, brightness: nil, temperature: nil)
  update_light(Light.new(on: on, brightness: brightness, temperature: temperature))
end

#update_brightness(brightness) ⇒ Object



53
54
55
# File 'lib/light_me_up/api_client.rb', line 53

def update_brightness(brightness)
  update(brightness: brightness)
end

#update_temperature(temperature) ⇒ Object



57
58
59
# File 'lib/light_me_up/api_client.rb', line 57

def update_temperature(temperature)
  update(temperature: temperature)
end