class RubyMyq::System

Attributes

garage_doors[R]

Public Class Methods

new(user, pass) click to toggle source
# File lib/ruby_myq/system.rb, line 9
def initialize(user, pass)
  @username = user
  @password = pass
  login(@username, @password)
  request_account
  discover_endpoints
end

Public Instance Methods

discover_endpoints() click to toggle source
# 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() click to toggle source
# File lib/ruby_myq/system.rb, line 98
def empty_device_arrays
  @gateways = []
  @garage_doors = []
  @lights = []
end
find_door_by_id(id) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/ruby_myq/system.rb, line 57
def login(username, password)
  options = {
    headers: RubyMyq::HEADERS,
    body: { username: username, password: password }.to_json,
    format: :json
    # debug_output: STDOUT
  }

  response = HTTParty.post(login_uri, options)
  @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() click to toggle source

private

# 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() click to toggle source
# File lib/ruby_myq/system.rb, line 74
def request_account
  options = {
    headers: @headers,
    body: { expand: 'account' }.to_json,
    format: :json
    # debug_output: STDOUT
  }

  response = HTTParty.get(request_account_uri, options)
  @account_uri = response['Account']['href'].gsub(/v5/, 'v5.1')
end
request_account_uri() click to toggle source
# 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() click to toggle source
# File lib/ruby_myq/system.rb, line 86
def request_device_list
  uri = "#{@account_uri}/#{RubyMyq::DEVICE_LIST_ENDPOINT}"

  options = {
    headers: @headers,
    format: :json
    # debug_output: STDOUT
  }

  HTTParty.get(uri, options)
end