Class: Lockitron::Lock
- Inherits:
-
Object
- Object
- Lockitron::Lock
- Defined in:
- lib/lockitron/lock.rb
Instance Attribute Summary collapse
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
-
#latitude ⇒ Object
readonly
Returns the value of attribute latitude.
-
#longitude ⇒ Object
readonly
Returns the value of attribute longitude.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
Class Method Summary collapse
-
.from_json(blob) ⇒ Lockitron::Lock
Initializes a lock from a Lockitron JSON representation.
Instance Method Summary collapse
-
#as(user) {|_self| ... } ⇒ Object
Takes a block of actions to perform as user.
-
#initialize(params = {}) ⇒ Lock
constructor
A lock.
-
#insert_key(user) ⇒ Object
Sets up the user context.
-
#invite(params = {}) ⇒ Hash
Invites a user to this lock.
-
#lock ⇒ Hash
Locks this lock.
-
#locked? ⇒ boolean
Door is locked.
-
#refresh ⇒ Object
Syncs the lock’s status.
-
#remove_key ⇒ Object
Tears down the user context.
-
#unlock ⇒ Hash
Unlocks this lock.
-
#unlocked? ⇒ boolean
Door is unlocked.
Constructor Details
#initialize(params = {}) ⇒ Lock
A lock
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
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
5 6 7 |
# File 'lib/lockitron/lock.rb', line 5 def keys @keys end |
#latitude ⇒ Object (readonly)
Returns the value of attribute latitude.
5 6 7 |
# File 'lib/lockitron/lock.rb', line 5 def latitude @latitude end |
#longitude ⇒ Object (readonly)
Returns the value of attribute longitude.
5 6 7 |
# File 'lib/lockitron/lock.rb', line 5 def longitude @longitude end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/lockitron/lock.rb', line 3 def name @name end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
5 6 7 |
# File 'lib/lockitron/lock.rb', line 5 def status @status end |
#uuid ⇒ Object (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
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
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
38 39 40 |
# File 'lib/lockitron/lock.rb', line 38 def insert_key user @user = user end |
#invite(params = {}) ⇒ Hash
Must be performed in user context
Invites a user to this lock.
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 |
#lock ⇒ Hash
Must be performed in user context
Locks this lock
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.
61 62 63 |
# File 'lib/lockitron/lock.rb', line 61 def locked? return @status == "lock" end |
#refresh ⇒ Object
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_key ⇒ Object
Tears down the user context
43 44 45 |
# File 'lib/lockitron/lock.rb', line 43 def remove_key @user = nil end |
#unlock ⇒ Hash
Must be performed in user context
Unlocks this lock
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.
66 67 68 |
# File 'lib/lockitron/lock.rb', line 66 def unlocked? return @status == "unlock" end |