farmbot-resource 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 +4 -4
- data/Gemfile +2 -2
- data/README.md +22 -4
- data/example.rb +21 -0
- data/lib/client.rb +29 -0
- data/lib/config.rb +2 -2
- data/lib/resources/abstract_resource.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf29be0a07977f8f371dc75c2a0c9932fe0e56ef
|
4
|
+
data.tar.gz: 93464e6dc9b378052370e1ff724affd13f606515
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7f408b098fa8bfb646dd7b79b4580638790354302dc81848da8803475a63052904ce74c9ac7f1dc46af468cac5be75b625e3e56f049d58284747ae7d59da966
|
7
|
+
data.tar.gz: 84651b320c0aa518c91662e998fc78188bb4d783407ad3954b84e01a9751671e223041c3660b257ba0a429a7a66251695c1bf8d5a11001dd41af5c2c6bd6ce77
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -8,17 +8,33 @@ Adapter gem for Farmbot Web App API. Currently used in Raspberry Pi controller t
|
|
8
8
|
* Schedules
|
9
9
|
* Plants
|
10
10
|
|
11
|
-
Need more than what's available? Raise an issue.
|
11
|
+
Need more than what's available? Raise an issue and we can add your resource to the list (PRs are also welcome).
|
12
12
|
|
13
13
|
# Usage
|
14
14
|
|
15
|
+
First, you will need an API token.
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
token = FbResource::Client.get_token(email: 't@g.com',
|
19
|
+
password: 'shhh...',
|
20
|
+
# OPTIONAL: Defaults to "my.farmbot.io"
|
21
|
+
# if not specified.
|
22
|
+
url: "http://localhost:3000")
|
23
|
+
# Returns really long JSON Web Token...
|
24
|
+
# => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9....'
|
25
|
+
```
|
26
|
+
|
27
|
+
Once you have an API token, you can create a 'client'.
|
28
|
+
|
15
29
|
```ruby
|
16
30
|
client = FbResource::Client.new do |config|
|
17
|
-
config.
|
18
|
-
config.token = '229458cgsdfgsdfgsdfaasdfasdfasdfasdfasda'
|
19
|
-
config.url = 'http://my.farmbot.it'
|
31
|
+
config.token = token
|
20
32
|
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Client objects can access API resources such as schedules and sequences.
|
21
36
|
|
37
|
+
```ruby
|
22
38
|
# Simple use case: get all schedules
|
23
39
|
# Returns array of hashes
|
24
40
|
schedules = client.schedules.all
|
@@ -28,3 +44,5 @@ schedules = client.schedules.all
|
|
28
44
|
sequences = client.sequences.fetch.all
|
29
45
|
|
30
46
|
```
|
47
|
+
|
48
|
+
**See example.rb for a runnable sample**.
|
data/example.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative './lib/farmbot-resource'
|
2
|
+
require 'pry'
|
3
|
+
token = FbResource::Client.get_token(email: 'admin@admin.com',
|
4
|
+
password: 'password123',
|
5
|
+
# Defaults to "my.farmbot.io" if not specified.
|
6
|
+
url: "http://localhost:3000")
|
7
|
+
|
8
|
+
client = FbResource::Client.new do |config|
|
9
|
+
# Note for users that self host a Farmbot API:
|
10
|
+
# FbResource will grab the URL from the token's "ISS" claim.
|
11
|
+
config.token = token
|
12
|
+
end
|
13
|
+
|
14
|
+
puts ("Grabbing schedules")
|
15
|
+
client.schedules.all # => []
|
16
|
+
|
17
|
+
puts ("Grabbing plants")
|
18
|
+
client.plants.all # => []
|
19
|
+
|
20
|
+
puts ("Grabbing sequences")
|
21
|
+
client.sequences.all # => []
|
data/lib/client.rb
CHANGED
@@ -1,12 +1,26 @@
|
|
1
1
|
require_relative 'config'
|
2
|
+
require 'base64'
|
2
3
|
|
3
4
|
module FbResource
|
5
|
+
class InvalidConfig < StandardError; end
|
4
6
|
class Client
|
5
7
|
attr_reader :config
|
6
8
|
|
7
9
|
def initialize(&blk)
|
8
10
|
@config = Config.build
|
9
11
|
yield(@config)
|
12
|
+
post_config
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.get_token(url: "http://my.farmbot.io", email:, password:)
|
16
|
+
# TODO handle auth errors in a more civilized manner.
|
17
|
+
resource_url = url + "/api/tokens"
|
18
|
+
payload = {user: {email: email, password: password}}
|
19
|
+
result = RestClient.post(resource_url, payload)
|
20
|
+
json = JSON.parse(result)
|
21
|
+
token = json["token"]
|
22
|
+
string = token["encoded"]
|
23
|
+
string
|
10
24
|
end
|
11
25
|
|
12
26
|
def schedules
|
@@ -20,5 +34,20 @@ module FbResource
|
|
20
34
|
def plants
|
21
35
|
@plants ||= FbResource::Plants.new(config)
|
22
36
|
end
|
37
|
+
|
38
|
+
def api_url
|
39
|
+
JSON.parse(Base64.decode64(config.token.split(".")[1]))["iss"]
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def invalidate(msg)
|
45
|
+
raise InvalidConfig.new, msg
|
46
|
+
end
|
47
|
+
|
48
|
+
def post_config
|
49
|
+
invalidate("config needs `token` attribute") unless @config.token
|
50
|
+
@config.url = api_url
|
51
|
+
end
|
23
52
|
end
|
24
53
|
end
|
data/lib/config.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: farmbot-resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rick Carlino
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -107,6 +107,7 @@ files:
|
|
107
107
|
- Gemfile
|
108
108
|
- README.md
|
109
109
|
- Rakefile
|
110
|
+
- example.rb
|
110
111
|
- lib/client.rb
|
111
112
|
- lib/config.rb
|
112
113
|
- lib/farmbot-resource.rb
|