farmbot-resource 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8e30f1034ba61c2be980b07431ad78f18c16ba05
4
+ data.tar.gz: dd69a2001d79e5cface4c0473c11660f18c09475
5
+ SHA512:
6
+ metadata.gz: fc0a073cc0d2f47328ae4408d745c43f45717af800953b97cf34ea973797e6e450033877bdfabaf1db6ac918ab24392100e62037b8962fdc58f99196928458f0
7
+ data.tar.gz: 30500e2196c07aac086e6bc9d285dd53af5bcce65d0a55e513083e859cff8fdd2fbb08b2af6f48414c1cafcac774d70cb78eba35f3dbbce74fad3af8975ce2c9
@@ -0,0 +1,48 @@
1
+ *.pstore
2
+ *.gem
3
+ *.rbc
4
+ *.swp
5
+ .bundle/*
6
+ .DS_Store
7
+ .idea/*
8
+ .loadpath
9
+ .project
10
+ .ruby-gemset
11
+ .ruby-version
12
+ /.bundle
13
+ /.bundle/
14
+ /.yardoc/
15
+ /_yardoc/
16
+ /coverage/
17
+ /coverage/
18
+ /db/*.sqlite3
19
+ /db/*.sqlite3-journal
20
+ /doc/
21
+ /InstalledFiles
22
+ /lib/.build
23
+ /lib/bundler/man/
24
+ /log/*.log
25
+ /pkg/
26
+ /rdoc/
27
+ /spec/reports/
28
+ /test/tmp/
29
+ /test/version_tmp/
30
+ /tmp
31
+ /tmp/
32
+ config.yml
33
+ coverage/**
34
+ credentials.yml
35
+ doc/*
36
+ doc/**/*
37
+ Gemfile.lock
38
+ log/*
39
+ notes.rb
40
+ settings.rb
41
+ /lib/.build
42
+ write_db_settings.rb
43
+ snippet.coffee
44
+ snippets.rb
45
+ tmp/**/*
46
+ *.rb~
47
+ serial_port.txt
48
+ *fiddle*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rspec'
4
+ gem 'simplecov'
5
+ gem 'rest-client'
@@ -0,0 +1,29 @@
1
+ # Farmbot Resource
2
+
3
+ Adapter gem for Farmbot Web App API. Currently used in Raspberry Pi controller to support scheduling operations.
4
+
5
+ # Available Resources
6
+
7
+ * Sequences
8
+ * Schedules
9
+
10
+ Need more than what's available? Raise an issue.
11
+
12
+ # Usage
13
+
14
+ ```ruby
15
+ client = FbResource::Client.new do |config|
16
+ config.uuid = 'xyxyxyxy-1234-5678-5345-453453453453'
17
+ config.token = '229458cgsdfgsdfgsdfaasdfasdfasdfasdfasda'
18
+ config.url = 'http://my.farmbot.it'
19
+ end
20
+
21
+ # Simple use case: get all schedules
22
+ # Returns array of hashes
23
+ schedules = client.schedules.all
24
+
25
+ # Another use case: cache busting with fetch()
26
+ # Results are always cached until fetch() is called
27
+ sequences = client.sequences.fetch.all
28
+
29
+ ```
File without changes
@@ -0,0 +1,20 @@
1
+ require_relative 'config'
2
+
3
+ module FbResource
4
+ class Client
5
+ attr_reader :config
6
+
7
+ def initialize(&blk)
8
+ @config = Config.build
9
+ yield(@config)
10
+ end
11
+
12
+ def schedules
13
+ @schedules ||= FbResource::Schedules.new(config)
14
+ end
15
+
16
+ def sequences
17
+ @sequences ||= FbResource::Sequences.new(config)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ require 'ostruct'
2
+
3
+ module FbResource
4
+ class Config < OpenStruct
5
+ def creds
6
+ { BOT_UUID: self.uuid, BOT_TOKEN: self.token }
7
+ end
8
+
9
+ def self.build
10
+ # Set defaults
11
+ self
12
+ .new
13
+ .tap {|obj| obj.url = 'http://localhost:3000'}
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,6 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+
4
+ require_relative 'resources/schedules'
5
+ require_relative 'resources/sequences'
6
+ require_relative 'client'
@@ -0,0 +1,16 @@
1
+ class AbstractResp
2
+ attr_reader :body, :request, :response
3
+
4
+ def initialize(body, request, response)
5
+ @body, @request, @response = body, request, response
6
+ end
7
+
8
+ def obj
9
+ JSON.parse(body.to_s)
10
+ rescue JSON::ParserError
11
+ body
12
+ end
13
+
14
+ def ok; self; end
15
+ def no; self; end
16
+ end
@@ -0,0 +1,5 @@
1
+ class BadHttpResp < AbstractResp
2
+ def no
3
+ yield(body, request, response)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class GoodHttpResp < AbstractResp
2
+ def ok
3
+ yield(obj, request, response)
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'abstract_resp'
2
+ require_relative 'bad_http_resp'
3
+ require_relative 'good_http_resp'
4
+
5
+ module FbResource
6
+ class Http
7
+ def self.get(url, headers)
8
+ i = self.new(nil, nil, nil)
9
+ RestClient.get(url, headers) do |a, b, c|
10
+ i.body, i.request, i.response = a, b, c
11
+ i.run
12
+ end
13
+ rescue => e
14
+ BadHttpResp.new(e, e, e) # ?
15
+ end
16
+
17
+ attr_accessor :body, :request, :response
18
+
19
+ def initialize(body, request, response)
20
+ @body, @request, @response = body, request, response
21
+ end
22
+
23
+ def run
24
+ (success? ? GoodHttpResp : BadHttpResp).new(body, request, response)
25
+ end
26
+
27
+ def success?
28
+ response.code[0].to_i == 2
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,39 @@
1
+ require_relative '../http/http'
2
+
3
+ module FbResource
4
+ class FetchError < Exception; end
5
+ class AbstractResource
6
+ class << self
7
+ attr_writer :path
8
+ def path
9
+ @path or
10
+ raise "Set a resource path on your resource class via self.path="
11
+ end
12
+ end
13
+
14
+ attr_accessor :conf
15
+
16
+ def initialize(conf)
17
+ @conf = conf
18
+ end
19
+
20
+ def fetch
21
+ Http
22
+ .get(conf.url + self.class.path, conf.creds)
23
+ .no { |e| no(e) }
24
+ .ok { |obj, req, res| fetch_ok(obj, req, res) }
25
+ end
26
+
27
+ def no(error)
28
+ raise FetchError, "Error fetching #{self.class.to_s}: #{error.message}"
29
+ end
30
+
31
+ def fetch_ok(obj, request, response)
32
+ @all = obj
33
+ end
34
+
35
+ def all
36
+ @all || fetch
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,7 @@
1
+ require_relative 'abstract_resource'
2
+
3
+ module FbResource
4
+ class Schedules < AbstractResource
5
+ self.path = '/api/schedules/'
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require_relative 'abstract_resource'
2
+
3
+ module FbResource
4
+ class Sequences < AbstractResource
5
+ self.path = '/api/sequences/'
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ require 'simplecov'
2
+ SimpleCov.start { add_filter "/spec/" }
3
+
4
+ Dir['spec/fakes/**/*.rb'].each { |file| load file }
5
+ require_relative '../lib/farmbot-pi'
6
+ require_relative '../lib/controllers/exec_sequence_controller'
7
+ require_relative '../lib/command_objects/commands'
8
+ require_relative '../lib/models/status_storage.rb'
9
+
10
+ RSpec.configure do |config|
11
+ config.expect_with :rspec do |expectations|
12
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
13
+ end
14
+
15
+ config.mock_with :rspec do |mocks|
16
+ mocks.verify_partial_doubles = true
17
+ end
18
+ end
19
+
20
+ # This is used for testing things that require an event loop. Once run, you can
21
+ # observe / make assertions on side effects.
22
+ def within_event_loop(ticks = 20)
23
+ EM.run do
24
+ bot.status[:BUSY] = 0
25
+ EM.tick_loop { (ticks < 1) ? EM.stop : ticks -= 1 }
26
+ yield
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: farmbot-resource
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rick Carlino
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-15 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rest-client
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.8'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.8'
97
+ description: REST API adapter for Farmbot web app.
98
+ email:
99
+ - rick.carlino@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - Gemfile
107
+ - README.md
108
+ - Rakefile
109
+ - lib/client.rb
110
+ - lib/config.rb
111
+ - lib/fb_resource.rb
112
+ - lib/http/abstract_resp.rb
113
+ - lib/http/bad_http_resp.rb
114
+ - lib/http/good_http_resp.rb
115
+ - lib/http/http.rb
116
+ - lib/resources/abstract_resource.rb
117
+ - lib/resources/schedules.rb
118
+ - lib/resources/sequences.rb
119
+ - spec/spec_helper.rb
120
+ homepage: https://github.com/FarmBot/farmbot-resource
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 2.2.0
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.4.6
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Ruby Gem to access resources on Farmbot Web App
144
+ test_files:
145
+ - spec/spec_helper.rb
146
+ has_rdoc: