ketra 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 37720a5e1d8c838fe7032637baf15b4a1d2f6dca
4
- data.tar.gz: 584b3a99ba32c4bd5cf2c1ab2a3342acb3322c78
3
+ metadata.gz: 95b00ea816d131398c046ead4a0195e55befd07a
4
+ data.tar.gz: 43b3ce8604a0a3b0c58c07f415bd516e9fe63328
5
5
  SHA512:
6
- metadata.gz: 52dd7bc1e83bf51bdc8946c56bddbb0da3e1e99c7f217ec0b769b0d2b8b8b1ca7919623c1d767f66f985cc4d608089f521f4791c67693b54aa86aad28b6f105a
7
- data.tar.gz: 708f84945cbc504c80592aa1226b1d6b3d188963aa1a56c5aae5f16902329c89707c0d9e6546d14edf800fb56d32af31b1acefc308456a33b5b9995d51d04187
6
+ metadata.gz: 5176169e0039713e7ef100a0aadceb3b35d627f66c4a1febd58f7b59f4645e9d7c3ef33ae480975ef685e3f02d49ee53102748c01f314ce1c5396cbf6c1fc55f
7
+ data.tar.gz: 37871765f712c252c80d9fedb40192db0f94212fa5c5795d1e25cd58ecd9c838170a2550eeebafa9a609baa0999782e25fade900c6496bbebb93dd4553c92830
data/.gitignore CHANGED
@@ -7,3 +7,6 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ *~
11
+ *.gem
12
+ .byebug_history
data/README.md CHANGED
@@ -38,13 +38,27 @@ Ketra.authorize(credentials)
38
38
  access_token = Ketra.access_token
39
39
  ```
40
40
 
41
+ #### Set Access Token Directly
42
+
43
+ ```ruby
44
+ Ketra.access_token = 'YOUR ACCESS TOKEN'
45
+ ```
46
+
47
+ ### Commands
48
+
49
+ All commands require authentication.
50
+
51
+ #### Activate Button
52
+
53
+ ```ruby
54
+ Ketra.hub_serial = 'YOUR HUB SERIAL NUMBER'
55
+ Ketra::Commands.activate_button('YOUR KEYPAD NAME', 'YOUR BUTTON NAME')
56
+ ```
41
57
 
42
58
  ## Development
43
59
 
44
60
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
45
61
 
46
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
47
-
48
62
  ## Contributing
49
63
 
50
64
  Bug reports and pull requests are welcome on GitHub at https://github.com/kennyjpowers/ketra. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
@@ -27,9 +27,12 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ["lib"]
29
29
 
30
- spec.add_dependency "oauth2", "~> 1.3.1"
30
+ spec.add_runtime_dependency 'oauth2', '~> 1.3', '>= 1.3.1'
31
+ spec.add_runtime_dependency 'rest-client', '~> 2.0', '>= 2.0.2'
32
+ spec.add_dependency 'json', '~> 2.1', '~> 2.1.0'
33
+ #spec.add_dependency "byebug", "~> 9.0.6"
31
34
 
32
- spec.add_development_dependency "bundler", "~> 1.10"
33
- spec.add_development_dependency "rake", "~> 10.0"
34
- spec.add_development_dependency "rspec"
35
+ spec.add_development_dependency 'bundler', '~> 1.10'
36
+ spec.add_development_dependency 'rake', '~> 10.0'
37
+ spec.add_development_dependency 'rspec', '~> 3.5'
35
38
  end
@@ -1,13 +1,17 @@
1
1
  require 'oauth2'
2
+ require 'rest-client'
3
+ #require 'byebug'
2
4
 
3
5
  require "ketra/version"
4
6
  require "ketra/ketra_client"
7
+ require "ketra/commands"
5
8
 
6
9
  module Ketra
7
10
  class Error < RuntimeError; end
8
11
 
9
12
  class << self
10
13
  attr_writer :callback_url
14
+ attr_accessor :hub_serial
11
15
  #TODO add permission_scopes
12
16
  end
13
17
 
@@ -32,7 +36,6 @@ module Ketra
32
36
  end
33
37
 
34
38
  PRODUCTION_HOST = 'https://my.goketra.com'
35
- API_VERSION = 'v4'
36
39
 
37
40
  # Set the environment, accepts :production (TODO add :sandbox environment).
38
41
  # Defaults to :production
@@ -66,18 +69,6 @@ module Ketra
66
69
  def self.authorization_grant
67
70
  @authorization_grant || :code
68
71
  end
69
-
70
- # Allow throwing API errors
71
- def self.silent_errors=(bool)
72
- unless [TrueClass, FalseClass].include?(bool.class)
73
- raise(ArguementError, "Silent errors must be set to either true or false")
74
- end
75
- @silent_errors = bool
76
- end
77
-
78
- def self.silenet_errors
79
- @silent_errors.nil? ? false : @silent_errors
80
- end
81
72
 
82
73
  def self.callback_url
83
74
  @callback_url || 'urn:ietf:wg:oauth:2.0:oob'
@@ -96,10 +87,12 @@ module Ketra
96
87
  end
97
88
 
98
89
  def self.access_token
99
- client.access_token.token
90
+ client.access_token.token if client.access_token
100
91
  end
101
92
 
102
- private
93
+ def self.access_token=(token)
94
+ client.access_token = token
95
+ end
103
96
 
104
97
  def self.client
105
98
  @client ||= KetraClient.new
@@ -0,0 +1,9 @@
1
+ module Ketra
2
+ module Commands
3
+ def self.activate_button(keypad_name, button_name, level=65535)
4
+ endpoint = "activateButton?keypadName=#{keypad_name}&buttonName=#{button_name}"
5
+ body_params = { "Level": level }
6
+ Ketra.client.post(endpoint, body_params)
7
+ end
8
+ end
9
+ end
@@ -1,8 +1,41 @@
1
1
  module Ketra
2
2
  class KetraClient
3
+ LOCAL_ENDPOINT_PREFIX = 'ketra.cgi/api/v1'
4
+ attr_reader :access_token
3
5
 
4
- attr_accessor :access_token
6
+ def access_token=(token)
7
+ @access_token = OAuth2::AccessToken.new(auth_client, token)
8
+ end
9
+
10
+ # set the hub discovery mode, accepts: :cloud TODO implement :ssdp
11
+
12
+ def hub_discovery_mode=(mode)
13
+ unless [:cloud].include?(mode)
14
+ raise(ArgumentError, "hub discovery mode must be set to :cloud")
15
+ end
16
+ @hub_discovery_mode = mode
17
+ end
18
+
19
+ def hub_discovery_mode
20
+ @hub_discovery_mode || :cloud
21
+ end
5
22
 
23
+ # set the api mode, accepts: :local TODO implement :remote
24
+ # :local mode makes calls directly to the hub
25
+ def api_mode=(mode)
26
+ unless [:local].include?(mode)
27
+ raise(ArgumentError, "api mode must be set to :local")
28
+ end
29
+ @api_mode = mode
30
+ end
31
+
32
+ def api_mode
33
+ @api_mode || :local
34
+ end
35
+
36
+
37
+
38
+
6
39
  # Authorization
7
40
 
8
41
  def authorization_url
@@ -15,18 +48,65 @@ module Ketra
15
48
  def authorize(credentials)
16
49
  case Ketra.authorization_grant
17
50
  when :code
18
- @access_token ||= client.auth_code.get_token(credentials[:authorization_code], :redirect_uri => Ketra.callback_url)
51
+ @access_token ||= auth_client.auth_code.get_token(credentials[:authorization_code], :redirect_uri => Ketra.callback_url)
19
52
  when :password
20
- @access_token ||= client.password.get_token(credentials[:username], credentials[:password])
53
+ @access_token ||= auth_client.password.get_token(credentials[:username], credentials[:password])
54
+ end
55
+ end
56
+
57
+ def get(endpoint, url_params={})
58
+ case api_mode
59
+ when :local
60
+ RestClient::Request.execute(verify_ssl: false,
61
+ method: :get,
62
+ url: "#{local_url}/#{endpoint}",
63
+ user: '',
64
+ password: @access_token.token,
65
+ content_type: :json,
66
+ headers: {params: url_params})
67
+ end
68
+ end
69
+
70
+ def post(endpoint, body_params={})
71
+ case api_mode
72
+ when :local
73
+ RestClient::Request.execute(verify_ssl: false,
74
+ method: :post,
75
+ url: "#{local_url}/#{endpoint}",
76
+ user: '',
77
+ password: @access_token.token,
78
+ content_type: :json,
79
+ payload: JSON.generate(body_params))
21
80
  end
22
81
  end
23
82
 
24
83
  private
25
84
 
26
- def client
27
- @client ||= OAuth2::Client.new(Ketra.client_id,
28
- Ketra.client_secret,
29
- :site => Ketra.host)
85
+ def hub_ip
86
+ @hub_ip || discover_hub(Ketra.hub_serial)
87
+ end
88
+
89
+ def local_url
90
+ "https://#{hub_ip}/#{LOCAL_ENDPOINT_PREFIX}"
91
+ end
92
+
93
+
94
+ def discover_hub(serial_number)
95
+ case hub_discovery_mode
96
+ when :cloud
97
+ response = RestClient.get "#{Ketra.host}/api/n4/v1/query"
98
+ object = JSON.parse(response)
99
+ hub_info = object["content"].select{ |h| h["serial_number"] == serial_number }.first
100
+ unless hub_info.nil?
101
+ @hub_ip = hub_info["internal_ip"]
102
+ end
103
+ end
104
+ end
105
+
106
+ def auth_client
107
+ @auth_client ||= OAuth2::Client.new(Ketra.client_id,
108
+ Ketra.client_secret,
109
+ :site => Ketra.host)
30
110
  end
31
111
 
32
112
  end
@@ -1,3 +1,3 @@
1
1
  module Ketra
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ketra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenneth Priester
@@ -15,6 +15,9 @@ dependencies:
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ - - ">="
18
21
  - !ruby/object:Gem::Version
19
22
  version: 1.3.1
20
23
  type: :runtime
@@ -22,8 +25,51 @@ dependencies:
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - - ">="
25
31
  - !ruby/object:Gem::Version
26
32
  version: 1.3.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: rest-client
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 2.0.2
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '2.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.0.2
53
+ - !ruby/object:Gem::Dependency
54
+ name: json
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '2.1'
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: 2.1.0
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '2.1'
70
+ - - "~>"
71
+ - !ruby/object:Gem::Version
72
+ version: 2.1.0
27
73
  - !ruby/object:Gem::Dependency
28
74
  name: bundler
29
75
  requirement: !ruby/object:Gem::Requirement
@@ -56,16 +102,16 @@ dependencies:
56
102
  name: rspec
57
103
  requirement: !ruby/object:Gem::Requirement
58
104
  requirements:
59
- - - ">="
105
+ - - "~>"
60
106
  - !ruby/object:Gem::Version
61
- version: '0'
107
+ version: '3.5'
62
108
  type: :development
63
109
  prerelease: false
64
110
  version_requirements: !ruby/object:Gem::Requirement
65
111
  requirements:
66
- - - ">="
112
+ - - "~>"
67
113
  - !ruby/object:Gem::Version
68
- version: '0'
114
+ version: '3.5'
69
115
  description: A friendly Ruby interface to the Ketra API
70
116
  email:
71
117
  - kennethjpriester@gmail.com
@@ -85,8 +131,8 @@ files:
85
131
  - bin/setup
86
132
  - ketra.gemspec
87
133
  - lib/ketra.rb
134
+ - lib/ketra/commands.rb
88
135
  - lib/ketra/ketra_client.rb
89
- - lib/ketra/ketra_client.rb~
90
136
  - lib/ketra/version.rb
91
137
  homepage: http://github.com/kennyjpowers/ketra
92
138
  licenses:
@@ -1,24 +0,0 @@
1
- module Ketra
2
- class KetraClient
3
-
4
-
5
-
6
-
7
- # Authorization
8
-
9
- def authorization_url
10
- unless authorization_grant == 'implicit'
11
- raise(NotImplementedError, "authorization url is only for grant type :implicit")
12
- end
13
- client.implicit.authorize_url(:redirect_uri => callback_url)
14
- end
15
-
16
-
17
- private
18
-
19
- def client
20
- @client ||= OAuth2::Client.new(client_id, client_secret, :site => host)
21
- end
22
-
23
- end
24
- end