sesame-ruby 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8c01ae5ee19a7b5bb25718ea32393fd410caf53e
4
+ data.tar.gz: dba18b3e28a9efad5904bfc587bd5895bcc274f3
5
+ SHA512:
6
+ metadata.gz: 7985e3d5d683dd25b9bc4b6c1c0d347eaafeb27d71f42b6a125e02eb49d6a7dcc7e03be653f252cb89f884ed45f9c010d5bb6044ff2a784cab3a5f6c982b03ef
7
+ data.tar.gz: a38d66aa41820de7bbaadafc6d38e50a5a6c22ac3482f9da1e721d43866467a82168b2501afd05bcbc4f1e0cc75e26a9bcddab6f40d52106857b621a9b027f79
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,16 @@
1
+ inherit_from: .rubocop_todo.yml
2
+ Naming/FileName:
3
+ Exclude:
4
+ - 'lib/sesame-ruby.rb'
5
+
6
+ Naming/AccessorMethodName:
7
+ Exclude:
8
+ - 'lib/sesame/api.rb'
9
+
10
+ Style/MethodMissing:
11
+ Exclude:
12
+ - 'spec/*.rb'
13
+
14
+ Metrics/BlockLength:
15
+ Exclude:
16
+ - 'spec/*.rb'
@@ -0,0 +1,24 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2017-10-06 10:22:19 -0500 using RuboCop version 0.50.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
11
+ # URISchemes: http, https
12
+ Metrics/LineLength:
13
+ Max: 100
14
+
15
+ # Offense count: 5
16
+ Style/Documentation:
17
+ Exclude:
18
+ - 'spec/**/*'
19
+ - 'test/**/*'
20
+ - 'lib/sesame.rb'
21
+ - 'lib/sesame/api.rb'
22
+ - 'lib/sesame/client.rb'
23
+ - 'lib/sesame/error.rb'
24
+ - 'lib/sesame/sesame.rb'
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in sesame.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Michael Klein
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,57 @@
1
+ [![Gem Version](https://badge.fury.io/rb/sesame-ruby.png)](http://badge.fury.io/rb/sesame-ruby)
2
+ [![Build Status](https://travis-ci.org/mbklein/sesame-ruby.svg?branch=master)](https://travis-ci.org/mbklein/sesame-ruby)
3
+ [![Coverage Status](https://coveralls.io/repos/mbklein/sesame-ruby/badge.svg?branch=master&service=github)](https://coveralls.io/github/mbklein/sesame-ruby?branch=master)
4
+
5
+ # Sesame
6
+
7
+ This gem provides a simple Ruby wrapper around the [CANDY HOUSE Sesame API](https://docs.candyhouse.co/) to control
8
+ Sesame Bluetooth and Internet-connected locks.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'sesame-ruby'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install sesame-ruby
25
+
26
+ ## Usage
27
+
28
+ # Log into the API
29
+ > client = Sesame::Client.new(email: 'abc@i-lovecandyhouse.co', password: 'super-strong-password')
30
+ => #<Sesame::Client:0x007fa1a81771d0 user: abc@i-lovecandyhouse.co>
31
+
32
+ # Get an array of all API-enabled devices associated with the account
33
+ > client.sesames
34
+ => [#<Sesame::Sesame:0x007fa1aa3bb188 device_id: ABC1234567, nickname: Front door, is_unlocked: true, api_enabled: true, battery: 100>,
35
+ #<Sesame::Sesame:0x007fa1aa3bb160 device_id: DEF7654321, nickname: Back door, is_unlocked: false, api_enabled: false, battery: 80>]
36
+
37
+ # Get a single device
38
+ > sesame = client.sesame(device_id: 'ABC1234567')
39
+ => #<Sesame::Sesame:0x007fa1aa3bb188 device_id: ABC1234567, nickname: Front door, is_unlocked: true, api_enabled: true, battery: 100>
40
+
41
+ # Control the device
42
+ > sesame.unlock
43
+ => true
44
+ > sesame.lock
45
+ => true
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0).
@@ -0,0 +1,18 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ desc 'Run style checker'
8
+ RuboCop::RakeTask.new(:rubocop) do |task|
9
+ task.fail_on_error = true
10
+ end
11
+
12
+ desc 'Run all tests.'
13
+ task :ci do
14
+ Rake::Task[:rubocop].invoke
15
+ Rake::Task[:spec].invoke
16
+ end
17
+
18
+ task default: :ci
@@ -0,0 +1 @@
1
+ require File.expand_path('../sesame', __FILE__)
@@ -0,0 +1,9 @@
1
+ require 'sesame/version'
2
+ require 'sesame/error'
3
+ require 'sesame/api'
4
+ require 'sesame/client'
5
+ require 'sesame/sesame'
6
+
7
+ module Sesame
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,62 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module Sesame
5
+ module Api
6
+ ENDPOINT_URL = 'https://api.candyhouse.co/v1/'.freeze
7
+
8
+ def authorized?
9
+ !@auth_token.nil?
10
+ end
11
+
12
+ def client
13
+ @client ||= Faraday.new(url: ENDPOINT_URL)
14
+ end
15
+
16
+ def auth_token(value)
17
+ @auth_token = value
18
+ self
19
+ end
20
+
21
+ def login(email:, password:)
22
+ auth_response = post('accounts/login', email: email, password: password)
23
+ auth_token(auth_response['authorization'])
24
+ end
25
+
26
+ def get_sesames
27
+ get('sesames')
28
+ end
29
+
30
+ def get_sesame(device_id:)
31
+ get("sesame/#{device_id}")
32
+ end
33
+
34
+ def control_sesame(device_id:, type:)
35
+ post("sesame/#{device_id}/control", type: type)
36
+ end
37
+
38
+ def get(path)
39
+ call(:get, path)
40
+ end
41
+
42
+ def post(path, params)
43
+ call(:post, path, params)
44
+ end
45
+
46
+ def call(method, path, params = nil)
47
+ response = client.send(method) do |req|
48
+ req.url path
49
+ req.headers['Content-Type'] = 'application/json'
50
+ req.headers['X-Authorization'] = @auth_token unless @auth_token.nil?
51
+ req.body = params.to_json unless params.nil?
52
+ end
53
+ parse_response(response)
54
+ end
55
+
56
+ def parse_response(response)
57
+ parsed_response = response.headers['Content-Length'].to_i > 0 ? JSON.parse(response.body) : ''
58
+ raise Error.new(response.status, parsed_response) if response.status >= 400
59
+ parsed_response
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,24 @@
1
+ module Sesame
2
+ class Client
3
+ include Api
4
+
5
+ def initialize(email:, password:)
6
+ login(email: email, password: password)
7
+ @email = email
8
+ end
9
+
10
+ def sesames
11
+ get_sesames['sesames'].collect do |sesame_attrs|
12
+ Sesame.new(sesame_attrs).auth_token(@auth_token)
13
+ end
14
+ end
15
+
16
+ def sesame(device_id:)
17
+ Sesame.new('device_id' => device_id).auth_token(@auth_token).refresh!
18
+ end
19
+
20
+ def inspect
21
+ %(#<#{self.class.name}:#{format('0x%.14x', (object_id << 1))} user: #{@email}>)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,16 @@
1
+ module Sesame
2
+ class Error < StandardError
3
+ attr_reader :status, :code
4
+
5
+ def initialize(status, data)
6
+ super(data['message'])
7
+ @data = data
8
+ @status = status
9
+ @code = data['code'].to_i
10
+ end
11
+
12
+ def to_s
13
+ %(#{@data['message']} HTTP: #{status}, API: #{@data['code']})
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,65 @@
1
+ module Sesame
2
+ class Sesame
3
+ include Api
4
+
5
+ def initialize(attrs = {})
6
+ @state = attrs
7
+ end
8
+
9
+ def device_id
10
+ @state['device_id']
11
+ end
12
+
13
+ def nickname
14
+ @state['nickname']
15
+ end
16
+
17
+ def unlocked?
18
+ truthy?(@state['is_unlocked'])
19
+ end
20
+ alias is_unlocked unlocked?
21
+
22
+ def state
23
+ unlocked? ? 'unlocked' : 'locked'
24
+ end
25
+
26
+ def api_enabled?
27
+ truthy?(@state['api_enabled'])
28
+ end
29
+ alias api_enabled api_enabled?
30
+
31
+ def battery
32
+ @state['battery'].to_i
33
+ end
34
+
35
+ def lock
36
+ control(type: 'lock')
37
+ end
38
+
39
+ def unlock
40
+ control(type: 'unlock')
41
+ end
42
+
43
+ def inspect
44
+ details = @state.keys.collect { |k| "#{k}: #{send(k.to_sym)}" }.join(', ')
45
+ %(#<#{self.class.name}:#{format('0x%.14x', (object_id << 1))} #{details}>)
46
+ end
47
+
48
+ def refresh!
49
+ @state.merge!(get_sesame(device_id: device_id))
50
+ self
51
+ end
52
+
53
+ private
54
+
55
+ def truthy?(value)
56
+ (value == true) || (value == 'true')
57
+ end
58
+
59
+ def control(type:)
60
+ control_sesame(device_id: device_id, type: type)
61
+ refresh!
62
+ true
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module Sesame
2
+ VERSION = '0.1.1'.freeze
3
+ end
@@ -0,0 +1,35 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sesame/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sesame-ruby'
8
+ spec.version = Sesame::VERSION
9
+ spec.authors = ['Michael B. Klein']
10
+ spec.email = ['mbklein@gmail.com']
11
+
12
+ spec.summary = 'Ruby wrapper for the CANDYHOUSE Sesame API (https://docs.candyhouse.co/)'
13
+ spec.homepage = 'https://github.com/mbklein/sesame-ruby'
14
+ spec.license = 'Apache2'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'json'
22
+ spec.add_dependency 'faraday'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.15'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'pry'
28
+ spec.add_development_dependency 'pry-byebug'
29
+ spec.add_development_dependency 'rubocop'
30
+ spec.add_development_dependency 'rubocop-rspec'
31
+ spec.add_development_dependency 'simplecov'
32
+ spec.add_development_dependency 'webmock'
33
+ spec.add_development_dependency 'coveralls'
34
+ spec.add_development_dependency 'faraday-detailed_logger'
35
+ end
metadata ADDED
@@ -0,0 +1,243 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sesame-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael B. Klein
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.15'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.15'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: webmock
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: coveralls
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: faraday-detailed_logger
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ description:
196
+ email:
197
+ - mbklein@gmail.com
198
+ executables: []
199
+ extensions: []
200
+ extra_rdoc_files: []
201
+ files:
202
+ - ".gitignore"
203
+ - ".rspec"
204
+ - ".rubocop.yml"
205
+ - ".rubocop_todo.yml"
206
+ - ".travis.yml"
207
+ - Gemfile
208
+ - LICENSE.txt
209
+ - README.md
210
+ - Rakefile
211
+ - lib/sesame-ruby.rb
212
+ - lib/sesame.rb
213
+ - lib/sesame/api.rb
214
+ - lib/sesame/client.rb
215
+ - lib/sesame/error.rb
216
+ - lib/sesame/sesame.rb
217
+ - lib/sesame/version.rb
218
+ - sesame.gemspec
219
+ homepage: https://github.com/mbklein/sesame-ruby
220
+ licenses:
221
+ - Apache2
222
+ metadata: {}
223
+ post_install_message:
224
+ rdoc_options: []
225
+ require_paths:
226
+ - lib
227
+ required_ruby_version: !ruby/object:Gem::Requirement
228
+ requirements:
229
+ - - ">="
230
+ - !ruby/object:Gem::Version
231
+ version: '0'
232
+ required_rubygems_version: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - ">="
235
+ - !ruby/object:Gem::Version
236
+ version: '0'
237
+ requirements: []
238
+ rubyforge_project:
239
+ rubygems_version: 2.5.1
240
+ signing_key:
241
+ specification_version: 4
242
+ summary: Ruby wrapper for the CANDYHOUSE Sesame API (https://docs.candyhouse.co/)
243
+ test_files: []