Class: SleepIQ::Client
- Inherits:
-
Object
- Object
- SleepIQ::Client
- Includes:
- Bed, Family, Foundation, Pump, Sleeper
- Defined in:
- lib/sleepiq/client.rb
Instance Attribute Summary collapse
-
#awsalb ⇒ Object
Returns the value of attribute awsalb.
-
#bedid ⇒ Object
Returns the value of attribute bedid.
-
#key ⇒ Object
Returns the value of attribute key.
-
#sessid ⇒ Object
Returns the value of attribute sessid.
Instance Method Summary collapse
- #default_headers ⇒ Object
-
#initialize(**opts) ⇒ Client
constructor
rubocop:disable Metrics/AbcSize.
-
#login(username = @username, password = @password) ⇒ Object
rubocop:disable Metrics/AbcSize.
Methods included from Sleeper
#sleep_data, #sleep_slice_data, #sleeper
Methods included from Pump
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
Constructor Details
permalink #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] login 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
permalink #awsalb ⇒ Object
Returns the value of attribute awsalb.
6 7 8 |
# File 'lib/sleepiq/client.rb', line 6 def awsalb @awsalb end |
permalink #bedid ⇒ Object
Returns the value of attribute bedid.
6 7 8 |
# File 'lib/sleepiq/client.rb', line 6 def bedid @bedid end |
permalink #key ⇒ Object
Returns the value of attribute key.
6 7 8 |
# File 'lib/sleepiq/client.rb', line 6 def key @key end |
permalink #sessid ⇒ Object
Returns the value of attribute sessid.
6 7 8 |
# File 'lib/sleepiq/client.rb', line 6 def sessid @sessid end |
Instance Method Details
permalink #default_headers ⇒ Object
[View source]
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 |
permalink #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 login(username = @username, password = @password) # rubocop:disable Metrics/AbcSize login_conn = 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 = login_conn.put do |req| req.url '/rest/login' req..timeout = 10 req..write_timeout = 10 req..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 |