lockitron 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +79 -0
- data/Rakefile +2 -0
- data/bin/lockitron +32 -0
- data/gem.sublime-project +8 -0
- data/gem.sublime-workspace +1106 -0
- data/lib/lockitron.rb +6 -0
- data/lib/lockitron/authentication.rb +26 -0
- data/lib/lockitron/lockitron.rb +67 -0
- data/lib/lockitron/locks.rb +66 -0
- data/lib/lockitron/version.rb +3 -0
- data/lockitron.gemspec +20 -0
- metadata +110 -0
data/lib/lockitron.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
DOMAIN = "https://api.lockitron.com"
|
2
|
+
LOCKS_URL = DOMAIN + "/v1/locks"
|
3
|
+
|
4
|
+
module Lockitron
|
5
|
+
module Authentication
|
6
|
+
|
7
|
+
def access_token
|
8
|
+
@@access_token ||= ENV["LOCKITRON_ACCESS_TOKEN"]
|
9
|
+
invalid_access_token! if @@access_token == "" || @@access_token.nil?
|
10
|
+
@@access_token
|
11
|
+
end
|
12
|
+
|
13
|
+
def access_token=(token)
|
14
|
+
@@access_token = token
|
15
|
+
end
|
16
|
+
|
17
|
+
def invalid_access_token!
|
18
|
+
puts "Invalid Access Token!"
|
19
|
+
puts "I need a valid access token stored in the environment variable LOCKITRON_ACCESS_TOKEN. Head over to \"https://api.lockitron.com/v1/oauth/applications\" to get an access token."
|
20
|
+
puts "Once you have an access token, run \"export LOCKITRON_ACCESS_TOKEN=ACCESS_TOKEN\", where ACCESS_TOKEN is your access token."
|
21
|
+
puts "Store the access token as an environment variable in ~/.bashrc if you don't want to have to do this everytime"
|
22
|
+
exit
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module Lockitron
|
2
|
+
class Locks
|
3
|
+
extend Lockitron::Authentication
|
4
|
+
DOMAIN = "https://api.lockitron.com"
|
5
|
+
LOCKS_URL = DOMAIN + "/v1/locks"
|
6
|
+
SETTINGS_PATH = './settings'
|
7
|
+
|
8
|
+
def self.locks
|
9
|
+
url = "#{LOCKS_URL}?access_token=#{access_token}"
|
10
|
+
@@locks ||= RestClient.get url do |response, request, result|
|
11
|
+
if response.code == 200
|
12
|
+
JSON.parse(response.to_str)
|
13
|
+
elsif response.code == 401
|
14
|
+
invalid_access_token!
|
15
|
+
self.locks
|
16
|
+
else
|
17
|
+
puts "Something went wrong, and I'm not sure what. Please send an email to jarred@lockitron.com and tell him what happened."
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.lock_names
|
23
|
+
@@names ||= locks.collect { |lock| lock['lock']['name'] }
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.unlock_by_name(name)
|
27
|
+
@@lock = find_lock_by_name(name)
|
28
|
+
access_lock(@@lock, "unlock")
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.lock_by_name(name)
|
32
|
+
@@lock = find_lock_by_name(name)
|
33
|
+
access_lock(@@lock, "lock")
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.access_lock(lock, direction)
|
37
|
+
url = "#{LOCKS_URL}/#{lock['id']}/#{direction}"
|
38
|
+
RestClient.post url, :access_token => access_token do |response|
|
39
|
+
if response.code == 200
|
40
|
+
puts "Successfully #{direction.capitalize}ed #{lock['name'].capitalize}!"
|
41
|
+
elsif response.code == 401
|
42
|
+
invalid_access_token!
|
43
|
+
access_lock(lock,direction)
|
44
|
+
else
|
45
|
+
puts "Something went wrong, and I'm not sure what. Please send an email to jarred@lockitron.com and tell him what happened."
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.find_lock_by_name(name)
|
51
|
+
@@lock = locks.select { |lock| lock['lock']['name'] == name }
|
52
|
+
if @@lock.empty?
|
53
|
+
puts "No available lock named #{name}\n\n"
|
54
|
+
available_locks
|
55
|
+
exit
|
56
|
+
else
|
57
|
+
return @@lock.first["lock"]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.available_locks
|
62
|
+
access_token # Ensures that an access token has been selected
|
63
|
+
puts "Available locks:\n\n"
|
64
|
+
locks.each { |lock| puts lock['lock']['name'] }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rest-client'
|
3
|
+
require 'json'
|
4
|
+
require 'lockitron/authentication'
|
5
|
+
|
6
|
+
module Lockitron
|
7
|
+
class Locks
|
8
|
+
extend Lockitron::Authentication
|
9
|
+
|
10
|
+
def self.all
|
11
|
+
url = "#{LOCKS_URL}?access_token=#{access_token}"
|
12
|
+
@@locks ||= RestClient.get url do |response, request, result|
|
13
|
+
if response.code == 200
|
14
|
+
JSON.parse(response.to_str)
|
15
|
+
elsif response.code == 401
|
16
|
+
invalid_access_token!
|
17
|
+
else
|
18
|
+
puts "An unknown error occurred. Please send an email to jarred@lockitron.com and tell him what happened."
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.names
|
24
|
+
@@names ||= all.collect { |lock| lock['lock']['name'] }
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.unlock(name)
|
28
|
+
@@lock = find(name)
|
29
|
+
access(@@lock, "unlock")
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.lock(name)
|
33
|
+
@@lock = find(name)
|
34
|
+
access(@@lock, "lock")
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.access(lock, direction)
|
38
|
+
url = "#{LOCKS_URL}/#{lock['id']}/#{direction}"
|
39
|
+
RestClient.post url, :access_token => access_token do |response|
|
40
|
+
if response.code == 200
|
41
|
+
puts "Successfully #{direction.capitalize}ed #{lock['name'].capitalize}!"
|
42
|
+
elsif response.code == 401
|
43
|
+
invalid_access_token!
|
44
|
+
else
|
45
|
+
puts "An unknown error ocurred. Please send an email to jarred@lockitron.com and tell him what happened."
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.find(name)
|
51
|
+
@@lock = all.select { |lock| lock['lock']['name'] == name }
|
52
|
+
if @@lock.empty?
|
53
|
+
puts "No available lock named #{name}\n\n"
|
54
|
+
list
|
55
|
+
else
|
56
|
+
return @@lock.first["lock"]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.list
|
61
|
+
access_token # Ensures that an access token has been selected
|
62
|
+
puts "Available locks:\n"
|
63
|
+
all.each { |lock| puts lock['lock']['name'] }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/lockitron.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/lockitron/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = "lockitron"
|
6
|
+
gem.homepage = "http://github.com/jarred-sumner/lockitron"
|
7
|
+
gem.license = "MIT"
|
8
|
+
gem.summary = %Q{Lock and unlock your lock your Lockitron-powered locks from bash and Ruby}
|
9
|
+
gem.description = %Q{Lockitron lets you unlock your front door from anywhere in the world, including your smartphone. We have an iPhone app, an Android app, a webapp, a mobile web app, a REST API, and now, a RubyGem.}
|
10
|
+
gem.email = "jarred@lockitron.com"
|
11
|
+
gem.authors = ["Jarred Sumner"]
|
12
|
+
gem.add_dependency 'rest-client'
|
13
|
+
gem.add_dependency 'thor'
|
14
|
+
gem.add_dependency 'json'
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($\)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.version = Lockitron::VERSION
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lockitron
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jarred Sumner
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rest-client
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: thor
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: json
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Lockitron lets you unlock your front door from anywhere in the world,
|
63
|
+
including your smartphone. We have an iPhone app, an Android app, a webapp, a mobile
|
64
|
+
web app, a REST API, and now, a RubyGem.
|
65
|
+
email: jarred@lockitron.com
|
66
|
+
executables:
|
67
|
+
- lockitron
|
68
|
+
extensions: []
|
69
|
+
extra_rdoc_files: []
|
70
|
+
files:
|
71
|
+
- .gitignore
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- bin/lockitron
|
77
|
+
- gem.sublime-project
|
78
|
+
- gem.sublime-workspace
|
79
|
+
- lib/lockitron.rb
|
80
|
+
- lib/lockitron/authentication.rb
|
81
|
+
- lib/lockitron/lockitron.rb
|
82
|
+
- lib/lockitron/locks.rb
|
83
|
+
- lib/lockitron/version.rb
|
84
|
+
- lockitron.gemspec
|
85
|
+
homepage: http://github.com/jarred-sumner/lockitron
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 1.8.21
|
107
|
+
signing_key:
|
108
|
+
specification_version: 3
|
109
|
+
summary: Lock and unlock your lock your Lockitron-powered locks from bash and Ruby
|
110
|
+
test_files: []
|