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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4f187d03c42f511e3f0bb076dab0468ea38b4a1
4
- data.tar.gz: dee1130a879c71ae58e9b627fe1dd989cc1978a6
3
+ metadata.gz: 2ce54e300dfbdb346b011998f657976c127ef3be
4
+ data.tar.gz: e4ee507ea20d1428a1f482dbf7aa76d6ba5fb3ad
5
5
  SHA512:
6
- metadata.gz: f56487af955d5b25492ec2a5371bfa4bb37fa14b32b852f32ef66d560eb3e22e89198f9552df301041c24a865325ea735c29e705b85334d670ae2b25bfab7c73
7
- data.tar.gz: dd83bb1cc382b0a061478cac485213dff488ec591e487239a8b242ba9ce1eeafec1804bb1b3bf61eff8038a39ed08a55bdd4f15e787cf68e08f01520abb1b8aa
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. Copy client secrets file to `.secrets.json` in listening directory.
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.yml
21
- ```yaml
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
@@ -5,8 +5,5 @@ require 'gcp_directory'
5
5
 
6
6
  GcpDirectory.directory = ARGV[0] || raise('first argument must by listening directory')
7
7
 
8
- # always update token
9
- GcpDirectory.refresh_token
10
-
11
8
  # list for files
12
9
  GcpDirectory::Listener.listen
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, 'printer.json'))).symbolize_keys
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(auth=GcpDirectory.token_client)
8
- @api = Printer.new(auth)
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.auth_client(GcpDirectory.token_path))
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: default_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: {
@@ -1,3 +1,3 @@
1
1
  module GcpDirectory
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gcp_directory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Pierce