brunt_api 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +49 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/brunt_api.gemspec +33 -0
- data/lib/brunt_api.rb +6 -0
- data/lib/brunt_api/client.rb +98 -0
- data/lib/brunt_api/error.rb +28 -0
- data/lib/brunt_api/version.rb +3 -0
- metadata +142 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 18c32c1169c48c012604e9c9518fb9eda9e0a2ac3d96d6472fff6e3cb1745c2c
|
4
|
+
data.tar.gz: 86f73a0a50cd84ffb317323785b56bbb4b4ca39a06592344e8967597feb35135
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e3d60081ebfd796ef6f16c825daef25e7d39475a479c0fd44f3ae43aea65d5231c449ee136b07df157636f6604cf6f2f80d8eacc0a18d1c83b88e6f69ab99f3
|
7
|
+
data.tar.gz: 7ba348cff7975e32a9cecee84d497077748e03985342274ee015d3de7a4e79093bdc5434e5b7e2e75cbbf04a83b828bc7f9a3ef281f2399dca4339492294f80e
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 mmck328
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# BruntAPI
|
2
|
+
|
3
|
+
This is an unofficial Ruby binding for Brunt API. Ported from [JS binding by MattJeanes](https://github.com/MattJeanes/brunt-api).
|
4
|
+
|
5
|
+
Currently this gem support operations only for Brunt Blind Engine.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'brunt_api'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install brunt_api
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
### Preparation
|
23
|
+
```ruby
|
24
|
+
client = BruntAPI::Client.new
|
25
|
+
client.login('brunt_account_id', 'password')
|
26
|
+
```
|
27
|
+
### Get things infomation
|
28
|
+
```ruby
|
29
|
+
client.get_things
|
30
|
+
```
|
31
|
+
### Get thing state
|
32
|
+
```ruby
|
33
|
+
client.get_state('thing_uri')
|
34
|
+
```
|
35
|
+
`'thing_uri'` can be obtained by `BruntAPI::Client#get_things`.
|
36
|
+
|
37
|
+
### Set blind position via Blind Engine
|
38
|
+
```ruby
|
39
|
+
client.set_position('thing_uri', position)
|
40
|
+
```
|
41
|
+
`position` must be `Numeric` in range 0-100.
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mmck328/brunt_api.rb.
|
46
|
+
|
47
|
+
## License
|
48
|
+
|
49
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "brunt_api"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/brunt_api.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "brunt_api/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "brunt_api"
|
8
|
+
spec.version = BruntAPI::VERSION
|
9
|
+
spec.authors = ["mmck328"]
|
10
|
+
spec.email = ["edamame1010@hotmail.co.jp"]
|
11
|
+
|
12
|
+
spec.summary = "An unofficial Ruby binding for Brunt API (e.g. Blind Engine)"
|
13
|
+
spec.description = "This is an unofficial Ruby binding of Brunt API (e.g. Blind Engine). Ported from JS binding by MattJeanes https://github.com/MattJeanes/brunt-api"
|
14
|
+
spec.homepage = "https://github.com/mmck328/brunt_api.rb"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
29
|
+
|
30
|
+
spec.add_dependency "faraday", "~> 0.15.4"
|
31
|
+
spec.add_dependency "faraday_middleware", "~> 0.12.2"
|
32
|
+
spec.add_dependency "faraday-cookie_jar", "~> 0.0.6"
|
33
|
+
end
|
data/lib/brunt_api.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
require 'faraday-cookie_jar'
|
4
|
+
require 'json'
|
5
|
+
require 'brunt_api/error'
|
6
|
+
|
7
|
+
module BruntAPI
|
8
|
+
class Client
|
9
|
+
def initialize
|
10
|
+
@conn = Faraday.new do |faraday|
|
11
|
+
faraday.request :json
|
12
|
+
faraday.use :cookie_jar
|
13
|
+
faraday.adapter Faraday.default_adapter
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def login(username, password)
|
18
|
+
res = @conn.post do |req|
|
19
|
+
req.url 'https://sky.brunt.co/session'
|
20
|
+
req.body = { ID: username, PASS: password }
|
21
|
+
end
|
22
|
+
|
23
|
+
if res.success?
|
24
|
+
JSON.parse(res.body)
|
25
|
+
else
|
26
|
+
handle_failed_response(res, 'Failed to login.')
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_things
|
31
|
+
res = @conn.get do |req|
|
32
|
+
req.url 'https://sky.brunt.co/thing'
|
33
|
+
end
|
34
|
+
|
35
|
+
if res.success?
|
36
|
+
JSON.parse(res.body)
|
37
|
+
else
|
38
|
+
handle_failed_response(res, 'Failed to get things.')
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def get_state(thing_uri)
|
43
|
+
res = @conn.get do |req|
|
44
|
+
req.url 'https://thing.brunt.co/thing' + thing_uri
|
45
|
+
end
|
46
|
+
|
47
|
+
if res.success?
|
48
|
+
JSON.parse(res.body)
|
49
|
+
else
|
50
|
+
handle_failed_response(res, 'Failed to get state of thing.')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def set_position(thing_uri, position)
|
55
|
+
unless position.kind_of?(Numeric)
|
56
|
+
raise ArgumentError, 'Position must be Numeric.'
|
57
|
+
end
|
58
|
+
if position.kind_of?(Complex)
|
59
|
+
raise ArgumentError, 'Position must not be Complex.'
|
60
|
+
end
|
61
|
+
if position < 0 or position > 100
|
62
|
+
raise ArgumentError, 'Position must be in range (0.0..100.0).'
|
63
|
+
end
|
64
|
+
|
65
|
+
res = @conn.put do |req|
|
66
|
+
req.url 'https://thing.brunt.co/thing' + thing_uri
|
67
|
+
req.body = { requestPosition: position.to_f.to_s }
|
68
|
+
end
|
69
|
+
|
70
|
+
if res.success?
|
71
|
+
begin
|
72
|
+
JSON.parse(res.body)
|
73
|
+
rescue
|
74
|
+
'success'
|
75
|
+
end
|
76
|
+
else
|
77
|
+
handle_failed_response(res, 'Failed to set position of thing.')
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
private
|
83
|
+
def handle_failed_response(res, default_message)
|
84
|
+
case res.status
|
85
|
+
when 401
|
86
|
+
raise BruntAPI::UnauthorizedError
|
87
|
+
when 403
|
88
|
+
raise BruntAPI::ForbiddenError
|
89
|
+
when 404
|
90
|
+
raise BruntAPI::NotFoundError
|
91
|
+
when 503
|
92
|
+
raise BruntAPI::ServiceUnavailableError
|
93
|
+
else
|
94
|
+
raise BruntAPI::ClientError, default_message + " (API response status: #{res.status})"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module BruntAPI
|
2
|
+
class ClientError < StandardError
|
3
|
+
end
|
4
|
+
|
5
|
+
class UnauthorizedError < ClientError
|
6
|
+
def initialize
|
7
|
+
super('Unauthorized or authorization expired. Login and retry.')
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class ForbiddenError < ClientError
|
12
|
+
def initialize
|
13
|
+
super('Access forbidden.')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class NotFoundError < ClientError
|
18
|
+
def initialize
|
19
|
+
super('Resource not found.')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class ServiceUnavailableError < ClientError
|
24
|
+
def initialize
|
25
|
+
super('API is currently unavailable.')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: brunt_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- mmck328
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: faraday
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.15.4
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.15.4
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: faraday_middleware
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.12.2
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.12.2
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: faraday-cookie_jar
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.0.6
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.0.6
|
97
|
+
description: This is an unofficial Ruby binding of Brunt API (e.g. Blind Engine).
|
98
|
+
Ported from JS binding by MattJeanes https://github.com/MattJeanes/brunt-api
|
99
|
+
email:
|
100
|
+
- edamame1010@hotmail.co.jp
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- brunt_api.gemspec
|
114
|
+
- lib/brunt_api.rb
|
115
|
+
- lib/brunt_api/client.rb
|
116
|
+
- lib/brunt_api/error.rb
|
117
|
+
- lib/brunt_api/version.rb
|
118
|
+
homepage: https://github.com/mmck328/brunt_api.rb
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.7.7
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: An unofficial Ruby binding for Brunt API (e.g. Blind Engine)
|
142
|
+
test_files: []
|