gcp_directory 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 +4 -4
- data/README.md +11 -6
- data/exe/gcp_listen +0 -3
- data/lib/gcp_directory.rb +2 -1
- data/lib/gcp_directory/listener.rb +14 -8
- data/lib/gcp_directory/printer.rb +4 -10
- data/lib/gcp_directory/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ce54e300dfbdb346b011998f657976c127ef3be
|
4
|
+
data.tar.gz: e4ee507ea20d1428a1f482dbf7aa76d6ba5fb3ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e23183f2ab7a6f6ceed5177988ea6d86e402f942fd83125ccaf23d989dd6cdc2daed31fd18bd060f435f7c8ab650bd8510a56e4230c37285373e432d2e12a1f
|
7
|
+
data.tar.gz: 2d1f8f1f66429dac5796043d2fb04e3b38c5186ccee4ab61e06b1f1581181635b3a99627aaefc6d6d3bd85f9391329272a09247095a45abc7ee5498e0902a749
|
data/README.md
CHANGED
@@ -11,15 +11,20 @@ Listen to a directory for new files and send files to Google Cloud Print.
|
|
11
11
|
### Setup OAUTH
|
12
12
|
|
13
13
|
1. Get secrets from from Google API Console website: https://console.developers.google.com
|
14
|
-
2. <img src='google_api_console.png' />
|
15
|
-
3.
|
16
|
-
4. run `gcp_fetch_token PATH_TO_DIRECTORY`
|
14
|
+
2. Copy client secrets file to `.secrets.json` in listening directory. <img src='google_api_console.png' />
|
15
|
+
3. run `gcp_fetch_token PATH_TO_DIRECTORY`
|
17
16
|
|
18
17
|
If you need to listen to multiple directories startup more than one program.
|
19
18
|
|
20
|
-
### config.
|
21
|
-
```
|
22
|
-
|
19
|
+
### config.json
|
20
|
+
```json
|
21
|
+
{
|
22
|
+
"printerid": "some_printer_id",
|
23
|
+
"ticket": {
|
24
|
+
"version": "1.0",
|
25
|
+
"print": {}
|
26
|
+
}
|
27
|
+
}
|
23
28
|
```
|
24
29
|
|
25
30
|
## Installation
|
data/exe/gcp_listen
CHANGED
data/lib/gcp_directory.rb
CHANGED
@@ -28,7 +28,7 @@ module GcpDirectory
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def self.config
|
31
|
-
JSON.parse(File.read(File.join(directory, '
|
31
|
+
JSON.parse(File.read(File.join(directory, 'config.json'))).symbolize_keys
|
32
32
|
end
|
33
33
|
|
34
34
|
def self.secrets_path
|
@@ -61,6 +61,7 @@ module GcpDirectory
|
|
61
61
|
end
|
62
62
|
|
63
63
|
def self.refresh_token
|
64
|
+
logger.info('Refreshing auth token')
|
64
65
|
auth = auth_client(token_path)
|
65
66
|
auth.refresh!
|
66
67
|
write_token(auth)
|
@@ -4,20 +4,30 @@ module GcpDirectory
|
|
4
4
|
# Watch for changes and send to API
|
5
5
|
class Listener
|
6
6
|
|
7
|
-
def initialize
|
8
|
-
@api = Printer.new
|
9
|
-
|
7
|
+
def initialize
|
8
|
+
@api = Printer.new
|
10
9
|
end
|
11
10
|
|
12
11
|
def logger
|
13
12
|
GcpDirectory.logger
|
14
13
|
end
|
14
|
+
|
15
15
|
def self.logger
|
16
16
|
GcpDirectory.logger
|
17
17
|
end
|
18
18
|
|
19
|
+
def refresh_token_if_needed
|
20
|
+
if @last_refresh.nil? or @last_refresh < 59.minutes.ago
|
21
|
+
GcpDirectory.refresh_token
|
22
|
+
@last_refresh = Time.now
|
23
|
+
@api = Printer.new
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
19
27
|
def print(added)
|
20
|
-
return if added =~ /\.(done|error)$/
|
28
|
+
return if added =~ /\.(done|error|json)$/
|
29
|
+
refresh_token_if_needed
|
30
|
+
|
21
31
|
GcpDirectory.config[:printerid] || raise(ArgumentError, '`printerid` not defined in config!')
|
22
32
|
options = GcpDirectory.config.merge(
|
23
33
|
title: added,
|
@@ -33,11 +43,7 @@ module GcpDirectory
|
|
33
43
|
|
34
44
|
def self.listen
|
35
45
|
instance = GcpDirectory::Listener.new
|
36
|
-
|
37
46
|
listener = Listen.to(GcpDirectory.directory) do |modified, added, removed|
|
38
|
-
logger.debug "modified absolute path: #{modified}"
|
39
|
-
logger.debug "added absolute path: #{added}"
|
40
|
-
logger.debug "removed absolute path: #{removed}"
|
41
47
|
added.each { |file| instance.print(file) } if added
|
42
48
|
end
|
43
49
|
listener.start # not blocking
|
@@ -6,7 +6,7 @@ module GcpDirectory
|
|
6
6
|
|
7
7
|
debug_output(GcpDirectory.logger)
|
8
8
|
|
9
|
-
def initialize(auth = GcpDirectory.
|
9
|
+
def initialize(auth = GcpDirectory.token_client)
|
10
10
|
auth.access_token || raise(ArgumentError, "`access_token` not set in #{auth}")
|
11
11
|
@auth = auth
|
12
12
|
end
|
@@ -15,21 +15,15 @@ module GcpDirectory
|
|
15
15
|
self.class.post('/jobs', with_default_options(options))
|
16
16
|
end
|
17
17
|
|
18
|
-
def submit(printerid:, title:, content:, ticket:
|
18
|
+
def submit(printerid:, title:, content:, ticket:)
|
19
19
|
self.class.post('/submit', with_default_options(body: {
|
20
20
|
printerid: printerid,
|
21
21
|
title: title,
|
22
|
-
ticket: ticket.to_json
|
22
|
+
ticket: ticket.to_json,
|
23
|
+
content: content
|
23
24
|
}))
|
24
25
|
end
|
25
26
|
|
26
|
-
def default_ticket
|
27
|
-
{
|
28
|
-
version: '1.0',
|
29
|
-
print: {}
|
30
|
-
}
|
31
|
-
end
|
32
|
-
|
33
27
|
def with_default_options(**options)
|
34
28
|
{
|
35
29
|
headers: {
|