module GcpDirectory

Listen for changes in a directory and send to Google Cloud Print

Constants

CALLBACK_URL
VERSION

Public Class Methods

auth_client(path) click to toggle source
# File lib/gcp_directory.rb, line 47
def self.auth_client(path)
  # check secrets exist
  File.file?(path) || raise("#{path} does not exist! Please download from https://console.developers.google.com/")

  # load them
  client_secrets = Google::APIClient::ClientSecrets.load(path)

  auth = client_secrets.to_authorization
  auth.update!(
    scope: 'https://www.googleapis.com/auth/cloudprint',
    redirect_uri: CALLBACK_URL,
    access_type: 'offline',
    response_type: 'code'
  )
  auth
end
config() click to toggle source
# File lib/gcp_directory.rb, line 31
def self.config
  JSON.parse(File.read(File.join(directory, 'config.json'))).symbolize_keys
end
directory() click to toggle source
# File lib/gcp_directory.rb, line 27
def self.directory
  @directory ||= File.expand_path('.')
end
directory=(dir) click to toggle source
# File lib/gcp_directory.rb, line 23
def self.directory=(dir)
  @directory = dir
end
fetch_token() click to toggle source
# File lib/gcp_directory.rb, line 71
def self.fetch_token
  oauth_url = auth_client(secrets_path).authorization_uri.to_s

  $stderr.puts "Opening the following URL in your default browser. If it doesn't open, please open the link on your own:\n#{oauth_url}"

  if Gem.win_platform?
    puts `start "" "#{oauth_url}"`
  elsif RUBY_PLATFORM =~ /darwin/
    puts `open "#{oauth_url}"`
  else # linux
    puts `xdg-open "#{oauth_url}"`
  end

  start_callback_thread.join
end
logger() click to toggle source
# File lib/gcp_directory.rb, line 19
def self.logger
  SemanticLogger['Listener']
end
refresh_token() click to toggle source
# File lib/gcp_directory.rb, line 64
def self.refresh_token
  logger.info('Refreshing auth token')
  auth = auth_client(token_path)
  auth.refresh!
  write_token(auth)
end
secrets_path() click to toggle source
# File lib/gcp_directory.rb, line 35
def self.secrets_path
  File.join(directory, '.secrets.json')
end
start_callback_thread() click to toggle source
# File lib/gcp_directory.rb, line 87
def self.start_callback_thread
  server = WEBrick::HTTPServer.new Port: 8000, DocumentRoot: File.expand_path('.')

  server.mount_proc '/callback' do |req, res|
    auth = auth_client(secrets_path)
    auth.code = req.query['code']
    auth.fetch_access_token!

    $stderr.puts "writing token to #{token_path}"
    write_token(auth)
    res.body = "Credentials written to #{token_path}. You may close this browser tab."
    server.stop
  end

  $stderr.puts 'Server started in background to receive OAuth callback...'
  Thread.new do
    server.start
  end
end
submit_job(*args) click to toggle source
# File lib/gcp_directory.rb, line 111
def self.submit_job(*args)

  https://www.google.com/cloudprint/submit
end
token_client() click to toggle source
# File lib/gcp_directory.rb, line 43
def self.token_client
  auth_client(token_path)
end
token_path() click to toggle source
# File lib/gcp_directory.rb, line 39
def self.token_path
  File.join(directory, '.token.json')
end
write_token(auth) click to toggle source
# File lib/gcp_directory.rb, line 107
def self.write_token(auth)
  File.open(token_path, 'w') { |f| f << %Q{{"installed":#{auth.to_json}}}}
end