picobrew-api 0.1.2

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fef7a8adfcad93dbe7c7288eafd32ef7af4a3856
4
+ data.tar.gz: 70567e4d2d24ffd86afd38c57f19ac15a2497245
5
+ SHA512:
6
+ metadata.gz: a0723f5f3c833c43f0533191f1d5073491d264a1dc76ef7752a7480fbb0bff7fb928c29a545dcef7516e16c4dc686cdaf3aadde27b9fd3cb13aed17ab1e8e2ed
7
+ data.tar.gz: 06515f97fcd2ae797b3d5e86afc04e223e56f465e70f6bcae3a5f395b425088ddc1dc74df012c03d0e761bbc2d8b29b002dbbd0dd3b8f90a1dde02b12808d2e2
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in picobrew-api.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Todd Quessenberry
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.
@@ -0,0 +1,51 @@
1
+ # Picobrew::Api
2
+
3
+ Back in 2013, Picobrew launched a [Kickstarter](https://www.kickstarter.com/projects/1708005089/picobrew-zymatic-the-automatic-beer-brewing-applia/faqs#project_faq_69315) for the Zymatic, and advertised that they'd open source the firmware and provide web APIs. I haven't seen any indication they plan on following through with that pledge despite many requests from their customers, so this project attempts to begin to fill that void.
4
+
5
+ The first version of this api provides some simple getters for various data from your Picobrew account - recipes, sessions, logs, notes, etc.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'picobrew-api', :git => 'https://github.com/toddq/picobrew-api.git'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'picobrew/api'
23
+
24
+ picobrew = Picobrew::Api.new('your-username', 'your-password')
25
+ recipe = picobrew.get_all_recipes().first
26
+ control_program = picobrew.get_recipe_control_program(recipe['GUID'])
27
+ session = picobrew.get_sessions_for_recipe(recipe['GUID']).first
28
+ session_notes = picobrew.get_session_notes(session['id'])
29
+ session_log = picobrew.get_session_log(session['id'])
30
+ ```
31
+
32
+ ## Future
33
+
34
+ * Post notes to session
35
+ * Create/edit recipes
36
+ * Create/edit recipe control programs
37
+ * Document the api
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ To install this gem onto your local machine, run `gem build picobrew-api.gemspec` and `gem install ./picobrew-api-{version}.gem`.
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/toddq/picobrew-api.
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "picobrew/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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,273 @@
1
+ require 'json'
2
+ require 'httparty'
3
+ require 'nokogiri'
4
+
5
+ module Picobrew
6
+ end
7
+
8
+ class Picobrew::Api
9
+
10
+ HOST = 'picobrew.com'
11
+ include HTTParty
12
+ base_uri 'https://picobrew.com'
13
+ # debug_output
14
+
15
+ attr_reader :cookies
16
+
17
+ def initialize(username, password, cookies = nil)
18
+ @username = username
19
+ @password = password
20
+ @http = Net::HTTP.new(HOST, 443)
21
+ @http.use_ssl = true
22
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
23
+ @cached_sessions = []
24
+
25
+ log "Created Picobrew object for #{username}"
26
+ if !cookies.nil?
27
+ log "Using provided cookies instead of logging in"
28
+ @cookies = cookie_from_hash(cookies)
29
+ else
30
+ login()
31
+ end
32
+ end
33
+
34
+ def login()
35
+ log "Logging in"
36
+ begin
37
+ options = {
38
+ :body => {'username' => @username, 'Password' => @password},
39
+ :headers => {'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'},
40
+ :follow_redirects => false
41
+ }
42
+ response = self.class.post('/account/loginAjax.cshtml', options)
43
+ raise "No Set-Cookie in response" if response.get_fields('Set-Cookie').nil?
44
+ @cookies = parse_cookie(response)
45
+ rescue Exception => e
46
+ raise "Authentication error: #{e}"
47
+ end
48
+ log "logged in"
49
+ end
50
+
51
+ def logged_in?()
52
+ !@cookies.nil?
53
+ end
54
+
55
+ def get_all_recipes()
56
+ log "Get All Recipes"
57
+ begin
58
+ options = options({:body => {'option' => 'getAllRecipesForUser'}})
59
+ response = self.class.post('/JSONAPI/Zymatic/ZymaticRecipe.cshtml', options)
60
+ body = JSON.parse(response.body)
61
+ rescue Exception => e
62
+ log "Error: #{e}"
63
+ end
64
+ end
65
+
66
+ def get_recipe(recipe_id)
67
+ log "Scrape Recipe #{recipe_id}"
68
+ begin
69
+ options = options({})
70
+ response = self.class.get("/Members/Recipes/ParseRecipe.cshtml?id=#{recipe_id}", options)
71
+ page = Nokogiri::HTML(response.body)
72
+ recipe = {'specs' => {}}
73
+ page.css('#user-specs-table tr').each do |element|
74
+ recipe['specs'][element.css('td')[0].text] = element.css('td')[1].text
75
+ end
76
+ page.css('#editForm input').each do |element|
77
+ if is_json? element['value']
78
+ recipe[element['name']] = JSON.parse(element['value'])
79
+ else
80
+ recipe[element['name']] = element['value']
81
+ end
82
+ end
83
+ recipe
84
+ rescue Exception => e
85
+ log "Error: #{e}"
86
+ end
87
+ end
88
+
89
+ def get_recipe_control_program(recipe_id)
90
+ log "Scrape Recipe Advanced Editor"
91
+ begin
92
+ options = options({})
93
+ response = self.class.get("/members/recipes/editctlprogram?id=#{recipe_id}", options)
94
+ page = Nokogiri::HTML(response.body)
95
+ program = {'steps' => []}
96
+ page.css('#stepTable tr').each do |row|
97
+ next if row.at_css('input').nil?
98
+
99
+ step = {
100
+ 'index' => row.at_css('input')['data-index'].to_i,
101
+ 'name' => row.at_css('input[name*=Name]')['value'],
102
+ 'location' => row.at_css('select option[@selected=selected]').text,
103
+ 'targetTemp' => row.at_css('input[name*=Temp]')['value'].to_i,
104
+ 'time' => row.at_css('input[name*=Time]')['value'].to_i,
105
+ 'drain' => row.at_css('input[name*=Drain]')['value'].to_i
106
+ }
107
+
108
+ program['steps'].push(step) if !step['name'].nil?
109
+ end
110
+ program
111
+ rescue Exception => e
112
+ log "Error: #{e}"
113
+ end
114
+ end
115
+
116
+ def get_sessions_for_recipe(recipe_id)
117
+ log "Get Sessions for Recipe #{recipe_id}"
118
+ begin
119
+ options = options({})
120
+ response = self.class.get("/Members/Logs/brewingsessions.cshtml?id=#{recipe_id}", options)
121
+ page = Nokogiri::HTML(response.body)
122
+ sessions = []
123
+ page.css('#BrewingSessions tbody tr').each do |row|
124
+ sessions.push({
125
+ 'name' => row.css('td.name').text,
126
+ 'id' => row.css('td.name a')[0]['href'].gsub(/.*id=/, ''),
127
+ 'date' => row.css('td.date').text,
128
+ 'notes' => row.css('td')[3].text
129
+ } )
130
+ end
131
+ sessions
132
+ rescue Exception => e
133
+ log "Error: #{e}"
134
+ end
135
+ end
136
+
137
+ def get_all_sessions()
138
+ log "Get All Sessions"
139
+ begin
140
+ options = options({:body => {'option' => 'getAllSessionsForUser'}})
141
+ response = self.class.post('/JSONAPI/Zymatic/ZymaticSession.cshtml', options)
142
+ body = JSON.parse(response.body)
143
+ rescue Exception => e
144
+ log "Error: #{e}"
145
+ end
146
+ end
147
+
148
+ def get_session_log(session_id)
149
+ log "Get Session Log for #{session_id}"
150
+ # the sessions list contains references for guids, but the log api
151
+ # wants a *different* id, so need to lookup one from the other
152
+ if session_id.length > 6
153
+ session_id = get_short_session_id_for_guid(session_id)
154
+ raise Exception "No short session id for guid" if session_id.nil?
155
+ log "Get Session Log for #{session_id}"
156
+ end
157
+ begin
158
+ options = options({:body => {'option' => 'getSessionLogs', 'sessionID' => session_id}})
159
+ response = self.class.post('/JSONAPI/Zymatic/ZymaticSession.cshtml', options)
160
+ body = JSON.parse(response.body)
161
+ rescue Exception => e
162
+ log "Error: #{e}"
163
+ end
164
+ end
165
+
166
+ def get_session_notes(session_id)
167
+ log "Get Session Notes for #{session_id}"
168
+ if session_id.length > 6
169
+ session_id = get_short_session_id_for_guid(session_id)
170
+ raise Exception, "No short session id for guid" if session_id.nil?
171
+ log "Get Session Notes for #{session_id}"
172
+ end
173
+ begin
174
+ options = options({ :body => {'option' => 'getSessionNotes', 'sessionID' => session_id} })
175
+ response = self.class.post('/JSONAPI/Zymatic/ZymaticSession.cshtml', options)
176
+ body = JSON.parse(response.body)
177
+ rescue Exception => e
178
+ log "Error: #{e}"
179
+ end
180
+ end
181
+
182
+ def get_recipe_id_for_session_id(session_guid)
183
+ session = find_session(session_guid)
184
+ return session['RecipeGUID'] if !session.nil?
185
+ end
186
+
187
+ def get_short_session_id_for_guid(session_guid)
188
+ session = find_session(session_guid)
189
+ return session['ID'] if !session.nil?
190
+ end
191
+
192
+ def find_session(session_guid)
193
+ log "Looking up short session id for #{session_guid}"
194
+ # quick and dirty cache expiration
195
+ cache_sessions if @cached_sessions.empty? || @cached_at.to_i + 5 * 60 < Time.now.to_i
196
+ return @cached_sessions.find {|session| session['GUID'] == session_guid}
197
+ end
198
+
199
+ def cache_sessions()
200
+ log "Caching sesions"
201
+ @cached_sessions = get_all_sessions()
202
+ @cached_at = Time.now
203
+ end
204
+
205
+ def get_active_session()
206
+ log "Get Active Session"
207
+ begin
208
+ options = options({:body => {'option' => 'getZymaticsForUser', 'getActiveSession' => 'true'}})
209
+ response = self.class.post('/JSONAPI/Zymatic/ZymaticSession.cshtml', options)
210
+ # TOOD: return json
211
+ response.body
212
+ rescue Exception => e
213
+ log "Error: #{e}"
214
+ end
215
+ end
216
+
217
+ # don't really understand this one, not sure if id is required
218
+ def check_active(session_id)
219
+ log "Check if session is active: #{session_id}"
220
+ begin
221
+ options = options({:body => {'option' => 'checkActive', 'sessionId' => session_id}})
222
+ response = self.class.post('/JSONAPI/Zymatic/ZymaticSession.cshtml', options)
223
+ response.body
224
+ rescue Exception => e
225
+ log "Error: #{e}"
226
+ end
227
+ end
228
+
229
+ # API used by Zymatic hardware
230
+ # Does not require auth, but require user id and machine id
231
+ # Move this to a separate class?
232
+ def get_recipe_control_programs(user_id, machine_id)
233
+ log "Get Recipe Control Programs"
234
+ begin
235
+ response = self.class.get("/API/SyncUser?user=#{user_id}&machine=#{machine_id}")
236
+ response.body
237
+ rescue Exception => e
238
+ log "Error: #{e}"
239
+ end
240
+ end
241
+
242
+ def is_json?(json)
243
+ begin
244
+ JSON.parse(json)
245
+ return true
246
+ rescue JSON::ParserError
247
+ return false
248
+ end
249
+ end
250
+
251
+ def options(params)
252
+ { :headers => {
253
+ 'Content-Type' => 'application/x-www-form-urlencoded',
254
+ 'Cookie' => @cookies.to_cookie_string }
255
+ }.merge params
256
+ end
257
+
258
+ def parse_cookie(resp)
259
+ cookie_hash = CookieHash.new
260
+ resp.get_fields('Set-Cookie').each { |c| cookie_hash.add_cookies(c) }
261
+ cookie_hash
262
+ end
263
+
264
+ def cookie_from_hash(hsh)
265
+ cookie_hash = CookieHash.new
266
+ cookie_hash.add_cookies(hsh)
267
+ cookie_hash
268
+ end
269
+
270
+ def log(msg)
271
+ # puts msg
272
+ end
273
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "picobrew-api"
7
+ spec.version = "0.1.2"
8
+ spec.authors = ["Todd Quessenberry"]
9
+ spec.email = ["todd@quessenberry.com"]
10
+
11
+ spec.summary = %q{Provides a library to access your Picobrew data}
12
+ spec.homepage = "https://github.com/toddq/picobrew-api"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.15"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_runtime_dependency "httparty", "~> 0.15"
25
+ spec.add_runtime_dependency "nokogiri", "~> 1.8"
26
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: picobrew-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Todd Quessenberry
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-28 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.15'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.15'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.8'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.8'
69
+ description:
70
+ email:
71
+ - todd@quessenberry.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - lib/picobrew/api.rb
84
+ - picobrew-api.gemspec
85
+ homepage: https://github.com/toddq/picobrew-api
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.6.13
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Provides a library to access your Picobrew data
109
+ test_files: []