picobrew-api 0.1.2 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fef7a8adfcad93dbe7c7288eafd32ef7af4a3856
4
- data.tar.gz: 70567e4d2d24ffd86afd38c57f19ac15a2497245
3
+ metadata.gz: a21c0e24b603c93a6a456d26b1b575a969b92bfb
4
+ data.tar.gz: 1c69315a7b05aae042c831fbce1ce5944c54034f
5
5
  SHA512:
6
- metadata.gz: a0723f5f3c833c43f0533191f1d5073491d264a1dc76ef7752a7480fbb0bff7fb928c29a545dcef7516e16c4dc686cdaf3aadde27b9fd3cb13aed17ab1e8e2ed
7
- data.tar.gz: 06515f97fcd2ae797b3d5e86afc04e223e56f465e70f6bcae3a5f395b425088ddc1dc74df012c03d0e761bbc2d8b29b002dbbd0dd3b8f90a1dde02b12808d2e2
6
+ metadata.gz: f315fc25da97ba0c09b9dde09bb9c83dde6d036c39b3a937006dc6f756c11d8b4ca84a4754f35fc287dfa97168f083fc82f19b36f6cbea735db6058d91baa0cb
7
+ data.tar.gz: 7ee6bebaaaf2f95b3f3b6d0df5176cac91642574d3b9e3be135b5e451cb3d51563f0aff37e548ba06b6762538895a870ed7bbdbaad1133874af10a4e612504d9
data/README.md CHANGED
@@ -9,7 +9,7 @@ The first version of this api provides some simple getters for various data from
9
9
  Add this line to your application's Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'picobrew-api', :git => 'https://github.com/toddq/picobrew-api.git'
12
+ gem 'picobrew-api'
13
13
  ```
14
14
 
15
15
  And then execute:
@@ -29,6 +29,8 @@ session_notes = picobrew.get_session_notes(session['id'])
29
29
  session_log = picobrew.get_session_log(session['id'])
30
30
  ```
31
31
 
32
+ See my REST api at https://github.com/toddq/pico-proxy for more example usage, or just use that if it's all you need.
33
+
32
34
  ## Future
33
35
 
34
36
  * Post notes to session
@@ -7,53 +7,54 @@ end
7
7
 
8
8
  class Picobrew::Api
9
9
 
10
- HOST = 'picobrew.com'
11
- include HTTParty
12
- base_uri 'https://picobrew.com'
13
- # debug_output
10
+ HOST = 'picobrew.com'
11
+ include HTTParty
12
+ base_uri 'https://picobrew.com'
13
+ # debug_output
14
14
 
15
15
  attr_reader :cookies
16
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
17
+ def initialize(username, password, cookies: nil, enableLogging: false)
18
+ @username = username
19
+ @password = password
20
+ @enableLogging = enableLogging
21
+ @http = Net::HTTP.new(HOST, 443)
22
+ @http.use_ssl = true
23
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
23
24
  @cached_sessions = []
24
25
 
25
- log "Created Picobrew object for #{username}"
26
+ log "Created Picobrew object for #{username}"
26
27
  if !cookies.nil?
27
28
  log "Using provided cookies instead of logging in"
28
29
  @cookies = cookie_from_hash(cookies)
29
30
  else
30
31
  login()
31
32
  end
32
- end
33
+ end
33
34
 
34
- def login()
35
- log "Logging in"
36
- begin
35
+ def login()
36
+ log "Logging in"
37
+ begin
37
38
  options = {
38
39
  :body => {'username' => @username, 'Password' => @password},
39
40
  :headers => {'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'},
40
41
  :follow_redirects => false
41
42
  }
42
- response = self.class.post('/account/loginAjax.cshtml', options)
43
+ response = self.class.post('/account/loginAjax.cshtml', options)
43
44
  raise "No Set-Cookie in response" if response.get_fields('Set-Cookie').nil?
44
- @cookies = parse_cookie(response)
45
+ @cookies = parse_cookie(response)
45
46
  rescue Exception => e
46
47
  raise "Authentication error: #{e}"
47
48
  end
48
49
  log "logged in"
49
- end
50
+ end
50
51
 
51
- def logged_in?()
52
- !@cookies.nil?
53
- end
52
+ def logged_in?()
53
+ !@cookies.nil?
54
+ end
54
55
 
55
- def get_all_recipes()
56
- log "Get All Recipes"
56
+ def get_all_recipes()
57
+ log "Get All Recipes"
57
58
  begin
58
59
  options = options({:body => {'option' => 'getAllRecipesForUser'}})
59
60
  response = self.class.post('/JSONAPI/Zymatic/ZymaticRecipe.cshtml', options)
@@ -61,7 +62,7 @@ class Picobrew::Api
61
62
  rescue Exception => e
62
63
  log "Error: #{e}"
63
64
  end
64
- end
65
+ end
65
66
 
66
67
  def get_recipe(recipe_id)
67
68
  log "Scrape Recipe #{recipe_id}"
@@ -145,23 +146,23 @@ class Picobrew::Api
145
146
  end
146
147
  end
147
148
 
148
- def get_session_log(session_id)
149
- log "Get Session Log for #{session_id}"
149
+ def get_session_log(session_id)
150
+ log "Get Session Log for #{session_id}"
150
151
  # the sessions list contains references for guids, but the log api
151
152
  # wants a *different* id, so need to lookup one from the other
152
153
  if session_id.length > 6
153
154
  session_id = get_short_session_id_for_guid(session_id)
154
- raise Exception "No short session id for guid" if session_id.nil?
155
+ raise Exception, "No short session id for guid" if session_id.nil?
155
156
  log "Get Session Log for #{session_id}"
156
157
  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
158
+ begin
159
+ options = options({:body => {'option' => 'getSessionLogs', 'sessionID' => session_id}})
160
+ response = self.class.post('/JSONAPI/Zymatic/ZymaticSession.cshtml', options)
161
+ body = JSON.parse(response.body)
162
+ rescue Exception => e
163
+ log "Error: #{e}"
164
+ end
165
+ end
165
166
 
166
167
  def get_session_notes(session_id)
167
168
  log "Get Session Notes for #{session_id}"
@@ -179,22 +180,22 @@ class Picobrew::Api
179
180
  end
180
181
  end
181
182
 
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
183
+ def get_recipe_id_for_session_id(session_guid)
184
+ session = find_session(session_guid)
185
+ return session['RecipeGUID'] if !session.nil?
186
+ end
186
187
 
187
188
  def get_short_session_id_for_guid(session_guid)
188
- session = find_session(session_guid)
189
+ session = find_session(session_guid)
189
190
  return session['ID'] if !session.nil?
190
191
  end
191
192
 
192
- def find_session(session_guid)
193
+ def find_session(session_guid)
193
194
  log "Looking up short session id for #{session_guid}"
194
195
  # quick and dirty cache expiration
195
196
  cache_sessions if @cached_sessions.empty? || @cached_at.to_i + 5 * 60 < Time.now.to_i
196
197
  return @cached_sessions.find {|session| session['GUID'] == session_guid}
197
- end
198
+ end
198
199
 
199
200
  def cache_sessions()
200
201
  log "Caching sesions"
@@ -202,17 +203,17 @@ class Picobrew::Api
202
203
  @cached_at = Time.now
203
204
  end
204
205
 
205
- def get_active_session()
206
+ def get_active_session()
206
207
  log "Get Active Session"
207
208
  begin
208
209
  options = options({:body => {'option' => 'getZymaticsForUser', 'getActiveSession' => 'true'}})
209
210
  response = self.class.post('/JSONAPI/Zymatic/ZymaticSession.cshtml', options)
210
- # TOOD: return json
211
+ # TODO: return json?
211
212
  response.body
212
213
  rescue Exception => e
213
214
  log "Error: #{e}"
214
215
  end
215
- end
216
+ end
216
217
 
217
218
  # don't really understand this one, not sure if id is required
218
219
  def check_active(session_id)
@@ -255,11 +256,11 @@ class Picobrew::Api
255
256
  }.merge params
256
257
  end
257
258
 
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
259
+ def parse_cookie(resp)
260
+ cookie_hash = CookieHash.new
261
+ resp.get_fields('Set-Cookie').each { |c| cookie_hash.add_cookies(c) }
262
+ cookie_hash
263
+ end
263
264
 
264
265
  def cookie_from_hash(hsh)
265
266
  cookie_hash = CookieHash.new
@@ -268,6 +269,6 @@ class Picobrew::Api
268
269
  end
269
270
 
270
271
  def log(msg)
271
- # puts msg
272
+ puts msg if @enableLogging
272
273
  end
273
274
  end
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "picobrew-api"
7
- spec.version = "0.1.2"
7
+ spec.version = "0.1.3"
8
8
  spec.authors = ["Todd Quessenberry"]
9
9
  spec.email = ["todd@quessenberry.com"]
10
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: picobrew-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Todd Quessenberry
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-28 00:00:00.000000000 Z
11
+ date: 2017-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler