adafruit-io 2.0.0.beta.7 → 2.0.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 +4 -4
- data/README.md +65 -18
- data/lib/adafruit/io/client/dashboards.rb +8 -0
- data/lib/adafruit/io/client/permissions.rb +38 -15
- data/lib/adafruit/io/client/triggers.rb +1 -1
- data/lib/adafruit/io/mqtt.rb +19 -6
- data/lib/adafruit/io/request_handler.rb +13 -8
- data/lib/adafruit/io/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c9d6fead5f73917e3333bd69c70965d1e4d23c31
|
4
|
+
data.tar.gz: 1cef173f825142a4ca7c7c4c3eeaf44d11fc1b7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea27ad2d2ef07fcbcbde215ace2d8e2e46960eb8f05d9fc5d26d0186f9ee10519e80b832237f7793b472e7e8f26a3d760a440613fed3f7b78d05d0c8e7c9d57c
|
7
|
+
data.tar.gz: fbf7c7ef1880a396ceb5aa2e54e1bea2b345a8b610482f35bf3afab6db2c97615fb5e14a8b0f04c8f141dce71d6474dd00d64559c0798be70140bfc2856f6924
|
data/README.md
CHANGED
@@ -32,20 +32,22 @@ It is our goal to eventually support all API V2 methods, but that will happen in
|
|
32
32
|
- [x] Activities `2.0.0.beta.5`
|
33
33
|
- [x] Permissions `2.0.0.beta.5`
|
34
34
|
- [x] Triggers `2.0.0.beta.6`
|
35
|
+
- [x] Feeds `2.0.0`
|
36
|
+
- [x] Data `2.0.0`
|
37
|
+
- [x] Tokens `2.0.0`
|
38
|
+
- [x] Blocks `2.0.0`
|
39
|
+
- [x] Dashboards `2.0.0`
|
40
|
+
- [x] Groups `2.0.0`
|
41
|
+
- [x] Activities `2.0.0`
|
42
|
+
- [x] Permissions `2.0.0`
|
43
|
+
- [x] Triggers `2.0.0`
|
35
44
|
|
36
45
|
Still needing complete tests:
|
37
46
|
|
38
|
-
- [x] Feeds
|
39
|
-
- [x] Data
|
40
|
-
- [x] Tokens
|
41
|
-
- [x] Blocks
|
42
|
-
- [ ] Dashboards *has pending*
|
43
|
-
- [ ] Groups *has pending*
|
44
|
-
- [ ] Activities
|
45
|
-
- [ ] Permissions
|
46
|
-
- [ ] Triggers
|
47
47
|
- [ ] MQTT
|
48
48
|
|
49
|
+
|
50
|
+
|
49
51
|
## Installation
|
50
52
|
|
51
53
|
Add this line to your application's Gemfile:
|
@@ -60,9 +62,12 @@ Or install it yourself as:
|
|
60
62
|
|
61
63
|
$ gem install adafruit-io
|
62
64
|
|
65
|
+
|
66
|
+
|
63
67
|
## Basic Usage
|
64
68
|
|
65
|
-
Each time you use the library, you'll
|
69
|
+
Each time you use the library, you'll have to pass your [Adafruit IO Key][4] to the client.
|
70
|
+
|
66
71
|
|
67
72
|
```ruby
|
68
73
|
require 'adafruit/io'
|
@@ -73,6 +78,7 @@ aio = Adafruit::IO::Client.new key: 'KEY'
|
|
73
78
|
|
74
79
|
Since every API request requires a username, you can also pass a username to the client initializer to use it for every request.
|
75
80
|
|
81
|
+
|
76
82
|
```ruby
|
77
83
|
require 'adafruit/io'
|
78
84
|
|
@@ -80,15 +86,50 @@ require 'adafruit/io'
|
|
80
86
|
aio = Adafruit::IO::Client.new key: 'KEY', username: 'USERNAME'
|
81
87
|
```
|
82
88
|
|
83
|
-
|
89
|
+
|
90
|
+
|
91
|
+
### Environment Variables
|
92
|
+
|
93
|
+
Whenever possible, we recommend you keep your Adafruit IO API credentials out of your application code by using environment variables. All the examples
|
94
|
+
|
95
|
+
[Others](http://blog.honeybadger.io/ruby-guide-environment-variables/) have written about using environment variables in Ruby, so we're not going to go into detail. We recommend the [dotenv](https://github.com/bkeepers/dotenv) gem if you're building a Ruby project.
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
### API Response Values
|
100
|
+
|
101
|
+
All return values are **plain Ruby hashes** based on the JSON response returned by the API. Most basic requests should get back a Hash with a `key` field. The key can be used in subsequent requests. API requests that return a list of objects will return a simple array of hashes. Feeds, Groups, and Dashboards all rely on the `key` value, other endpoints (Blocks, Permissions, Tokens, Triggers) use `id`.
|
102
|
+
|
103
|
+
You can find the current API documentation at [https://io.adafruit.com/api/docs/](https://io.adafruit.com/api/docs/). This library implements v2 of the Adafruit IO API.
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
### API Error Responses
|
108
|
+
|
109
|
+
As of **v2.0.0**, this library raises an `Adafruit::IO::RequestError` on any non HTTP 200 status responses. Generally, this means your code should wrap API calls in `begin...rescue...end` blocks.
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
require 'adafruit/io'
|
113
|
+
|
114
|
+
api_key = ENV['IO_KEY']
|
115
|
+
username = ENV['IO_USER']
|
116
|
+
|
117
|
+
api = Adafruit::IO::Client.new key: api_key, username: username
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
```
|
122
|
+
|
123
|
+
## Example
|
84
124
|
|
85
125
|
Here's an example of creating, adding data to, and deleting a feed.
|
86
126
|
|
127
|
+
|
87
128
|
```ruby
|
88
129
|
require 'adafruit/io'
|
89
130
|
|
90
|
-
api_key = ENV['
|
91
|
-
username = ENV['
|
131
|
+
api_key = ENV['IO_KEY']
|
132
|
+
username = ENV['IO_USER']
|
92
133
|
|
93
134
|
api = Adafruit::IO::Client.new key: api_key, username: username
|
94
135
|
|
@@ -120,19 +161,25 @@ puts "read?"
|
|
120
161
|
puts api.feed(garbage['key']).inspect
|
121
162
|
```
|
122
163
|
|
164
|
+
|
165
|
+
This code and more is available in [the examples/ directory](examples/).
|
166
|
+
|
123
167
|
## License
|
124
168
|
|
125
|
-
Copyright (c)
|
169
|
+
Copyright (c) 2018 Adafruit Industries. Licensed under the MIT license.
|
126
170
|
|
127
171
|
[Adafruit](https://adafruit.com) invests time and resources providing this open source code. Please support Adafruit and open-source hardware by purchasing products from [Adafruit](https://adafruit.com).
|
128
172
|
|
129
173
|
## Contributing
|
130
174
|
|
131
175
|
1. Fork it ( http://github.com/adafruit/io-client-ruby/fork )
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
176
|
+
1. Create your feature branch (`git checkout -b my-new-feature`)
|
177
|
+
1. Write tests, write code, and run the tests (`bundle exec rspec`)
|
178
|
+
1. Commit your changes (`git commit -am 'Add some feature'`)
|
179
|
+
1. Push to the branch (`git push origin my-new-feature`)
|
180
|
+
1. Create a new Pull Request
|
181
|
+
|
182
|
+
If you'd like to contribute and don't know where to start, [reach out on the Adafruit IO forum](https://forums.adafruit.com/viewforum.php?f=56) or in the [adafruit-io channel on our Discord server](https://discord.gg/adafruit).
|
136
183
|
|
137
184
|
[1]: https://www.ruby-lang.org
|
138
185
|
[2]: https://io.adafruit.com
|
@@ -40,6 +40,14 @@ module Adafruit
|
|
40
40
|
|
41
41
|
put api_url(username, 'dashboards', dashboard_key), query
|
42
42
|
end
|
43
|
+
|
44
|
+
def update_dashboard_layouts(*args)
|
45
|
+
username, arguments = extract_username(args)
|
46
|
+
dashboard_key = get_key_from_arguments(arguments)
|
47
|
+
query = get_query_from_arguments(arguments, %w(layouts))
|
48
|
+
|
49
|
+
post api_url(username, 'dashboards', dashboard_key, 'update_layouts'), query
|
50
|
+
end
|
43
51
|
end
|
44
52
|
end
|
45
53
|
end
|
@@ -2,6 +2,7 @@ module Adafruit
|
|
2
2
|
module IO
|
3
3
|
class Client
|
4
4
|
module Permissions
|
5
|
+
VALID_TYPES = %(feed group dashboard)
|
5
6
|
|
6
7
|
# Get all permissions for a resource.
|
7
8
|
#
|
@@ -9,40 +10,62 @@ module Adafruit
|
|
9
10
|
def permissions(*args)
|
10
11
|
username, arguments = extract_username(args)
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
end
|
13
|
+
assert_argument_size(arguments, 2)
|
14
|
+
assert_resource_type(arguments[0])
|
15
15
|
|
16
|
-
get api_url(username, arguments[0], arguments[1], 'acl')
|
16
|
+
get api_url(username, pluralize_type(arguments[0]), arguments[1], 'acl')
|
17
17
|
end
|
18
18
|
|
19
19
|
def permission(*args)
|
20
20
|
username, arguments = extract_username(args)
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
end
|
22
|
+
assert_argument_size(arguments, 3)
|
23
|
+
assert_resource_type(arguments[0])
|
25
24
|
|
26
|
-
get api_url(username, arguments[0], arguments[1], 'acl', arguments[2])
|
25
|
+
get api_url(username, pluralize_type(arguments[0]), arguments[1], 'acl', arguments[2])
|
27
26
|
end
|
28
27
|
|
29
28
|
def create_permission(*args)
|
30
29
|
username, arguments = extract_username(args)
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
|
31
|
+
assert_argument_size(arguments, 2)
|
32
|
+
assert_resource_type(arguments[0])
|
33
|
+
|
34
34
|
permission_attrs = arguments.pop
|
35
|
-
post api_url(username, arguments[0], arguments[1], 'acl'), permission_attrs
|
35
|
+
post api_url(username, pluralize_type(arguments[0]), arguments[1], 'acl'), permission_attrs
|
36
36
|
end
|
37
37
|
|
38
38
|
def delete_permission(*args)
|
39
39
|
username, arguments = extract_username(args)
|
40
40
|
|
41
|
-
|
42
|
-
|
41
|
+
assert_argument_size(arguments, 3)
|
42
|
+
assert_resource_type(arguments[0])
|
43
|
+
|
44
|
+
delete api_url(username, pluralize_type(arguments[0]), arguments[1], 'acl', arguments[2])
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def assert_resource_type(resource_type)
|
50
|
+
if !VALID_TYPES.include?(resource_type)
|
51
|
+
raise Adafruit::IO::Arguments::ArgumentError.new('permission resource type must be one of: feed, group, or dashboard')
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def assert_argument_size(arguments, size)
|
56
|
+
if size === 3
|
57
|
+
if arguments.size < 3
|
58
|
+
raise Adafruit::IO::Arguments::ArgumentError.new('permission requires resource type (string), key (string), and permission id (integer) values. valid types are feeds, groups, or dashboards')
|
59
|
+
end
|
60
|
+
elsif size == 2
|
61
|
+
if arguments.size < size
|
62
|
+
raise Adafruit::IO::Arguments::ArgumentError.new('permissions requires resource type (string) and resource key (string) values. valid types are feed, group, or dashboard')
|
63
|
+
end
|
43
64
|
end
|
65
|
+
end
|
44
66
|
|
45
|
-
|
67
|
+
def pluralize_type(resource_type)
|
68
|
+
"#{resource_type}s"
|
46
69
|
end
|
47
70
|
end
|
48
71
|
end
|
data/lib/adafruit/io/mqtt.rb
CHANGED
@@ -95,11 +95,18 @@ module Adafruit
|
|
95
95
|
|
96
96
|
# Subscribe to the feed with the given key. Use .get to retrieve messages
|
97
97
|
# from subscribed feeds.
|
98
|
-
|
98
|
+
#
|
99
|
+
# Include the { last_value: true } option if you'd like the feed to
|
100
|
+
# receive the last value immediately. (like MQTT retain)
|
101
|
+
def subscribe(key, options={})
|
99
102
|
raise 'client is not connected' unless @client.connected?
|
100
103
|
|
101
104
|
topic = key_to_feed_topic(key)
|
102
105
|
@client.subscribe(topic)
|
106
|
+
|
107
|
+
if options[:last_value]
|
108
|
+
@client.publish(topic + '/get', '')
|
109
|
+
end
|
103
110
|
end
|
104
111
|
|
105
112
|
def unsubscribe(key)
|
@@ -143,15 +150,21 @@ module Adafruit
|
|
143
150
|
# # do something
|
144
151
|
# end
|
145
152
|
#
|
146
|
-
# NOTE: if a feed already has a value, subscribing and calling get will
|
147
|
-
# immediately return the most recent value for the subscription,
|
148
|
-
# regardless of when it was received by IO.
|
149
153
|
def get(&block)
|
150
154
|
@client.get(&block)
|
151
155
|
end
|
152
156
|
|
153
157
|
private
|
154
158
|
|
159
|
+
def encode_json(record)
|
160
|
+
begin
|
161
|
+
JSON.generate record
|
162
|
+
rescue JSON::GeneratorError => ex
|
163
|
+
puts "failed to generate JSON from record: #{record.inspect}"
|
164
|
+
raise ex
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
155
168
|
def key_to_feed_topic(key)
|
156
169
|
"%s/f/%s" % [@options[:username], key]
|
157
170
|
end
|
@@ -169,7 +182,7 @@ module Adafruit
|
|
169
182
|
end
|
170
183
|
end
|
171
184
|
|
172
|
-
|
185
|
+
encode_json payload
|
173
186
|
end
|
174
187
|
|
175
188
|
def payload_from_values_with_location(values, location)
|
@@ -183,7 +196,7 @@ module Adafruit
|
|
183
196
|
end
|
184
197
|
end
|
185
198
|
|
186
|
-
|
199
|
+
encode_json payload
|
187
200
|
end
|
188
201
|
|
189
202
|
def indifferent_keys(hash)
|
@@ -2,6 +2,15 @@ require 'open-uri'
|
|
2
2
|
|
3
3
|
module Adafruit
|
4
4
|
module IO
|
5
|
+
class RequestError < StandardError
|
6
|
+
attr_reader :response
|
7
|
+
|
8
|
+
def initialize(message, response)
|
9
|
+
super(message)
|
10
|
+
@response = response
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
5
14
|
module RequestHandler
|
6
15
|
|
7
16
|
attr_reader :last_response, :pagination
|
@@ -70,11 +79,7 @@ module Adafruit
|
|
70
79
|
update_pagination(response)
|
71
80
|
|
72
81
|
if response.status < 200 || response.status > 299
|
73
|
-
|
74
|
-
nil
|
75
|
-
else
|
76
|
-
raise "GET error: #{ response.body }"
|
77
|
-
end
|
82
|
+
raise Adafruit::IO::RequestError.new("GET error: #{ response.body }", response)
|
78
83
|
else
|
79
84
|
parsed_response response
|
80
85
|
end
|
@@ -87,7 +92,7 @@ module Adafruit
|
|
87
92
|
end
|
88
93
|
|
89
94
|
if response.status < 200 || response.status > 299
|
90
|
-
raise "POST error: #{ response.body }"
|
95
|
+
raise Adafruit::IO::RequestError.new("POST error: #{ response.body }", response)
|
91
96
|
else
|
92
97
|
parsed_response response
|
93
98
|
end
|
@@ -100,7 +105,7 @@ module Adafruit
|
|
100
105
|
end
|
101
106
|
|
102
107
|
if response.status < 200 || response.status > 299
|
103
|
-
raise "PUT error: #{ response.body }"
|
108
|
+
raise Adafruit::IO::RequestError.new("PUT error: #{ response.body }", response)
|
104
109
|
else
|
105
110
|
parsed_response response
|
106
111
|
end
|
@@ -115,7 +120,7 @@ module Adafruit
|
|
115
120
|
if response.status === 404
|
116
121
|
nil
|
117
122
|
else
|
118
|
-
raise "DELETE error: #{ response.body }"
|
123
|
+
raise Adafruit::IO::RequestError.new("DELETE error: #{ response.body }", response)
|
119
124
|
end
|
120
125
|
else
|
121
126
|
parsed_response response
|
data/lib/adafruit/io/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: adafruit-io
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.0
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Cooper
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-
|
12
|
+
date: 2018-05-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|
@@ -138,9 +138,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
138
138
|
version: '0'
|
139
139
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
140
|
requirements:
|
141
|
-
- - "
|
141
|
+
- - ">="
|
142
142
|
- !ruby/object:Gem::Version
|
143
|
-
version:
|
143
|
+
version: '0'
|
144
144
|
requirements: []
|
145
145
|
rubyforge_project:
|
146
146
|
rubygems_version: 2.5.2
|