Class: RubyMyq::System
- Inherits:
-
Object
- Object
- RubyMyq::System
- Defined in:
- lib/ruby_myq/system.rb
Instance Attribute Summary collapse
-
#garage_doors ⇒ Object
readonly
Returns the value of attribute garage_doors.
Instance Method Summary collapse
- #discover_endpoints ⇒ Object
- #empty_device_arrays ⇒ Object
- #find_door_by_id(id) ⇒ Object
- #find_door_by_name(name) ⇒ Object
-
#initialize(user, pass) ⇒ System
constructor
A new instance of System.
- #instantiate_device(device, _headers) ⇒ Object
- #login(username, password) ⇒ Object
-
#login_uri ⇒ Object
private.
- #request_account ⇒ Object
- #request_account_uri ⇒ Object
- #request_device_list ⇒ Object
Constructor Details
#initialize(user, pass) ⇒ System
Returns a new instance of System.
9 10 11 12 13 14 15 |
# File 'lib/ruby_myq/system.rb', line 9 def initialize(user, pass) @username = user @password = pass login(@username, @password) request_account discover_endpoints end |
Instance Attribute Details
#garage_doors ⇒ Object (readonly)
Returns the value of attribute garage_doors.
8 9 10 |
# File 'lib/ruby_myq/system.rb', line 8 def garage_doors @garage_doors end |
Instance Method Details
#discover_endpoints ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/ruby_myq/system.rb', line 17 def discover_endpoints empty_device_arrays response = request_device_list devices = response['items'] devices.each do |device| instantiate_device(device, @headers) end end |
#empty_device_arrays ⇒ Object
98 99 100 101 102 |
# File 'lib/ruby_myq/system.rb', line 98 def empty_device_arrays @gateways = [] @garage_doors = [] @lights = [] end |
#find_door_by_id(id) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/ruby_myq/system.rb', line 26 def find_door_by_id(id) id = id.to_s @garage_doors.each do |door| return door if door.id == id end nil end |
#find_door_by_name(name) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/ruby_myq/system.rb', line 34 def find_door_by_name(name) @garage_doors.each do |door| return door if door.name == name end nil end |
#instantiate_device(device, _headers) ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ruby_myq/system.rb', line 104 def instantiate_device(device, _headers) if device['device_type'] == 'garagedooropener' @garage_doors << RubyMyq::Device::GarageDoor.new(device, @headers) # elsif device["device_type"] == "hub" # @gateways << RubyMyq::Device::Gateway.new(device, self) # elsif device["MyQDeviceTypeName"]=="???" # I need a MyQ light switch to implement this feature # @lights << RubyMyq::Device::LightSwitch.new(device) end end |
#login(username, password) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ruby_myq/system.rb', line 57 def login(username, password) = { headers: RubyMyq::HEADERS, body: { username: username, password: password }.to_json, format: :json # debug_output: STDOUT } response = HTTParty.post(login_uri, ) @security_token = response['SecurityToken'] @headers = {}.dup @headers.merge!(RubyMyq::HEADERS) @headers.merge!(SecurityToken: @security_token) # @cached_login_response = response # "logged in successfully" end |
#login_uri ⇒ Object
private
43 44 45 46 47 48 |
# File 'lib/ruby_myq/system.rb', line 43 def login_uri uri = ''.dup uri << "https://#{RubyMyq::HOST_URI}/" uri << "#{RubyMyq::API_VERSION}/" uri << RubyMyq::LOGIN_ENDPOINT end |
#request_account ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ruby_myq/system.rb', line 74 def request_account = { headers: @headers, body: { expand: 'account' }.to_json, format: :json # debug_output: STDOUT } response = HTTParty.get(request_account_uri, ) @account_uri = response['Account']['href'].gsub(/v5/, 'v5.1') end |
#request_account_uri ⇒ Object
50 51 52 53 54 55 |
# File 'lib/ruby_myq/system.rb', line 50 def request_account_uri uri = ''.dup uri << "https://#{RubyMyq::HOST_URI}/" uri << "#{RubyMyq::API_VERSION}/" uri << RubyMyq::ACCOUNT_ENDPOINT end |
#request_device_list ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ruby_myq/system.rb', line 86 def request_device_list uri = "#{@account_uri}/#{RubyMyq::DEVICE_LIST_ENDPOINT}" = { headers: @headers, format: :json # debug_output: STDOUT } HTTParty.get(uri, ) end |