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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da6767552fe92e942c25a01f2756e1e30bf3b4c5
4
- data.tar.gz: 2d8a8ca3e9fdcbc4d9b2f22ae3bbf5d820bd00ad
3
+ metadata.gz: bf29be0a07977f8f371dc75c2a0c9932fe0e56ef
4
+ data.tar.gz: 93464e6dc9b378052370e1ff724affd13f606515
5
5
  SHA512:
6
- metadata.gz: 8bfecb0cb718248fd27ad46bbf5fe3c08267e9d6a50fb530a83abc1552ec9343712d5d1c31e73b19e78052d7f9dbf3fba6d4c08b675712d6f4f686a6929cb180
7
- data.tar.gz: 9c65d51f98eef86e557cb2c2c7d6f11dfb3c1b6f41db5689260575240b9dba0120a850724b22811c15dc1260991f2939d0b7703c2fe2300989fb8b4de7fbf510
6
+ metadata.gz: f7f408b098fa8bfb646dd7b79b4580638790354302dc81848da8803475a63052904ce74c9ac7f1dc46af468cac5be75b625e3e56f049d58284747ae7d59da966
7
+ data.tar.gz: 84651b320c0aa518c91662e998fc78188bb4d783407ad3954b84e01a9751671e223041c3660b257ba0a429a7a66251695c1bf8d5a11001dd41af5c2c6bd6ce77
data/Gemfile CHANGED
@@ -1,5 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'rspec'
4
- gem 'simplecov'
3
+ # gem 'rspec'
4
+ # gem 'simplecov'
5
5
  gem 'rest-client'
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.uuid = 'xyxyxyxy-1234-5678-5345-453453453453'
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
@@ -2,8 +2,8 @@ require 'ostruct'
2
2
 
3
3
  module FbResource
4
4
  class Config < OpenStruct
5
- def creds
6
- { BOT_UUID: self.uuid, BOT_TOKEN: self.token }
5
+ def http_headers
6
+ { Authorization: self.token }
7
7
  end
8
8
 
9
9
  def self.build
@@ -19,7 +19,7 @@ module FbResource
19
19
 
20
20
  def fetch
21
21
  Http
22
- .get(conf.url + self.class.path, conf.creds)
22
+ .get(conf.url + self.class.path, conf.http_headers)
23
23
  .no { |obj, req, res| fetch_no(obj, req, res) }
24
24
  .ok { |obj, req, res| fetch_ok(obj, req, res) }
25
25
  end
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.1.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: 2015-10-09 00:00:00.000000000 Z
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