picobrew-api 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -1
- data/lib/picobrew/api.rb +52 -51
- data/picobrew-api.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a21c0e24b603c93a6a456d26b1b575a969b92bfb
|
4
|
+
data.tar.gz: 1c69315a7b05aae042c831fbce1ce5944c54034f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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'
|
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
|
data/lib/picobrew/api.rb
CHANGED
@@ -7,53 +7,54 @@ end
|
|
7
7
|
|
8
8
|
class Picobrew::Api
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
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
|
-
|
33
|
+
end
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
50
|
+
end
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
52
|
+
def logged_in?()
|
53
|
+
!@cookies.nil?
|
54
|
+
end
|
54
55
|
|
55
|
-
|
56
|
-
|
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
|
-
|
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
|
-
|
149
|
-
|
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
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
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
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
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
|
-
|
189
|
+
session = find_session(session_guid)
|
189
190
|
return session['ID'] if !session.nil?
|
190
191
|
end
|
191
192
|
|
192
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
211
|
+
# TODO: return json?
|
211
212
|
response.body
|
212
213
|
rescue Exception => e
|
213
214
|
log "Error: #{e}"
|
214
215
|
end
|
215
|
-
|
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
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
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
|
-
|
272
|
+
puts msg if @enableLogging
|
272
273
|
end
|
273
274
|
end
|
data/picobrew-api.gemspec
CHANGED
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.
|
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-
|
11
|
+
date: 2017-11-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|