Class: Lockitron::Lock

Inherits:
Object
  • Object
show all
Defined in:
lib/lockitron/lock.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Lock

A lock

Parameters:

  • options (Hash)
  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):



21
22
23
24
25
26
# File 'lib/lockitron/lock.rb', line 21

def initialize(params={})
  @uuid = params[:uuid]
  @name = params[:name]
  @user = params[:user]
  refresh if @user #if we have credentials, go ahead and sync with the API
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



5
6
7
# File 'lib/lockitron/lock.rb', line 5

def keys
  @keys
end

#latitudeObject (readonly)

Returns the value of attribute latitude.



5
6
7
# File 'lib/lockitron/lock.rb', line 5

def latitude
  @latitude
end

#longitudeObject (readonly)

Returns the value of attribute longitude.



5
6
7
# File 'lib/lockitron/lock.rb', line 5

def longitude
  @longitude
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/lockitron/lock.rb', line 3

def name
  @name
end

#statusObject (readonly)

Returns the value of attribute status.



5
6
7
# File 'lib/lockitron/lock.rb', line 5

def status
  @status
end

#uuidObject (readonly)

Returns the value of attribute uuid.



4
5
6
# File 'lib/lockitron/lock.rb', line 4

def uuid
  @uuid
end

Class Method Details

.from_json(blob) ⇒ Lockitron::Lock

Initializes a lock from a Lockitron JSON representation

Parameters:

  • JSON (Hash)

Returns:



10
11
12
13
14
# File 'lib/lockitron/lock.rb', line 10

def self.from_json blob
  id = blob['id']
  name = blob['name']
  self.new(name: name, uuid: id)
end

Instance Method Details

#as(user) {|_self| ... } ⇒ Object

Takes a block of actions to perform as user

Parameters:

Yields:

  • (_self)

Yield Parameters:



30
31
32
33
34
# File 'lib/lockitron/lock.rb', line 30

def as user
  insert_key user
  yield self
  remove_key
end

#insert_key(user) ⇒ Object

Sets up the user context

Parameters:



38
39
40
# File 'lib/lockitron/lock.rb', line 38

def insert_key user
  @user = user
end

#invite(params = {}) ⇒ Hash

Note:

Must be performed in user context

Invites a user to this lock.

Parameters:

  • Options (Hash)
  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :phone (String)
  • :email (String)
  • :role (String)

    Defaults to guest

  • :fullname (String)
  • :start (Time)

    Optional key start time

  • :expiration (Time)

    Optional key stop time

Returns:

  • (Hash)

    API response

Raises:

  • (InvalidArgument)


106
107
108
109
110
111
112
113
114
115
116
# File 'lib/lockitron/lock.rb', line 106

def invite(params={})
  require_user
  raise InvalidArgument, "Phone or email required" unless params[:email] or params[:phone]
  if params[:start]
    params[:start] = params[:start].to_i
  else
    params[:start] = Time.now.to_i
  end
  params[:expiration] = params[:expiration].to_i if params[:expiration]
  @user.post "locks/#{@uuid}/keys", params
end

#lockHash

Note:

Must be performed in user context

Locks this lock

Returns:

  • (Hash)

    API response



50
51
52
53
54
55
56
57
58
# File 'lib/lockitron/lock.rb', line 50

def lock
  require_user
  begin
    @user.post "locks/#{@uuid}/lock"
    @status = "lock"
  rescue ApiError
    false
  end
end

#locked?boolean

Returns door is locked.

Returns:

  • (boolean)

    door is locked



61
62
63
# File 'lib/lockitron/lock.rb', line 61

def locked?
  return @status == "lock"
end

#refreshObject

Note:

Must be performed with user context

Syncs the lock’s status



85
86
87
88
89
90
91
92
93
# File 'lib/lockitron/lock.rb', line 85

def refresh
  require_user
  lock = @user.get "locks/#{@uuid}"
  @name = lock['name']
  @status = lock['status']
  @latitude = lock['latitude']
  @longitude = lock['longitude']
  @keys = lock['keys']
end

#remove_keyObject

Tears down the user context



43
44
45
# File 'lib/lockitron/lock.rb', line 43

def remove_key
  @user = nil
end

#unlockHash

Note:

Must be performed in user context

Unlocks this lock

Returns:

  • (Hash)

    API response



73
74
75
76
77
78
79
80
81
# File 'lib/lockitron/lock.rb', line 73

def unlock
  require_user
  begin
    @user.post "locks/#{@uuid}/unlock"
    @status = "unlock"
  rescue ApiError
    false
  end
end

#unlocked?boolean

Returns door is unlocked.

Returns:

  • (boolean)

    door is unlocked



66
67
68
# File 'lib/lockitron/lock.rb', line 66

def unlocked?
  return @status == "unlock"
end