Class: SleepIQ::Client

Inherits:
Object
  • Object
show all
Includes:
Bed, Family, Foundation, Pump, Sleeper
Defined in:
lib/sleepiq/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Sleeper

#sleep_data, #sleep_slice_data, #sleeper

Methods included from Pump

#force_idle, #pump_status

Methods included from Foundation

#foundation_status, #light, #preset, #set_light, #stop_motion, #system

Methods included from Bed

#bed, #pause_mode, #sleep_number, #sleep_number_fav, #status, #update_pause_mode, #update_sleep_number, #update_sleep_number_fav

Methods included from Family

#family_status

Constructor Details

#initialize(**opts) ⇒ Client

rubocop:disable Metrics/AbcSize



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sleepiq/client.rb', line 14

def initialize(**opts) # rubocop:disable Metrics/AbcSize
  @username = opts[:username].nil? ? ENV['sleepiq_username'] : opts[:username]
  @password = opts[:password].nil? ? ENV['sleepiq_password'] : opts[:password]
  @key = opts[:key]
  @awsalb = opts[:awsalb]
  @sessid = opts[:sessid]
  @bedid = opts[:bedid]

   if @key.nil? || @awsalb.nil? || @sessid.nil?

  @conn = Faraday.new(
    url:     'https://api.sleepiq.sleepnumber.com',
    headers: default_headers,
    params:  { _k: @key },
    request: { timeout: 10, open_timeout: 3, write_timeout: 10 }
  ) do |conn|
    conn.response :json
    conn.adapter :net_http
  end

  @bedid = family_status['beds'][0]['bedId'] if @bedid.nil?
end

Instance Attribute Details

#awsalbObject

Returns the value of attribute awsalb.



6
7
8
# File 'lib/sleepiq/client.rb', line 6

def awsalb
  @awsalb
end

#bedidObject

Returns the value of attribute bedid.



6
7
8
# File 'lib/sleepiq/client.rb', line 6

def bedid
  @bedid
end

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/sleepiq/client.rb', line 6

def key
  @key
end

#sessidObject

Returns the value of attribute sessid.



6
7
8
# File 'lib/sleepiq/client.rb', line 6

def sessid
  @sessid
end

Instance Method Details

#default_headersObject



64
65
66
67
68
69
70
71
72
# File 'lib/sleepiq/client.rb', line 64

def default_headers
  { 'Content-Type':    'text/plain',
    'Host':            'api.sleepiq.sleepnumber.com',
    'Accept':          '*/*',
    'Cache-Control':   'no-cache',
    'Accept-Encoding': 'gzip, deflate',
    'Connection':      'keep-alive',
    'Cookie':          "AWSALB=#{@awsalb}; JSESSIONID=#{@sessid}" }
end

#login(username = @username, password = @password) ⇒ Object

rubocop:disable Metrics/AbcSize



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sleepiq/client.rb', line 37

def (username = @username, password = @password) # rubocop:disable Metrics/AbcSize
   = Faraday.new(
    url:     'https://api.sleepiq.sleepnumber.com',
    ssl:     { verify: false },
    headers: { 'Content-Type': 'text/plain', 'Host': 'api.sleepiq.sleepnumber.com' }
  ) do |conn|
    conn.response :json
    conn.adapter :net_http
  end

  result = .put do |req|
    req.url '/rest/login'
    req.options.timeout = 10
    req.options.write_timeout = 10
    req.options.open_timeout = 3
    req.body = "{\"login\":\"#{username}\",\"password\":\"#{password}\"}"
  end

  raise "status code was #{result.status}" unless result.status == 200

  result.headers['set-cookie'].split(' ').each do |k, _v|
    @sessid = k.split('=')[1].delete_suffix(';') if k.include? 'JSESSIONID'
    @awsalb = k.split('=')[1].delete_suffix(';') if k.include? 'AWSALB'
  end
  @key = result.body['key']
end