class ZohoHub::Cli::ReadModules

Public Class Methods

new() click to toggle source
# File lib/zoho_hub/cli/read_modules.rb, line 13
def initialize
  @options = {}
end

Public Instance Methods

cache_module_fields(info) click to toggle source
# File lib/zoho_hub/cli/read_modules.rb, line 96
def cache_module_fields(info)
  fields_array = ZohoHub::Settings::Field.all_json_for(info[:api_name])
  fields_path = File.join(ZohoHub.root, 'cache', 'fields')
  FileUtils.mkdir_p(fields_path)
  file_name = File.join(fields_path, "#{info[:api_name]}.json")

  File.open(file_name, 'w') do |file|
    file.write(JSON.pretty_generate(fields_array))
  end
end
cache_module_info(info) click to toggle source
# File lib/zoho_hub/cli/read_modules.rb, line 82
def cache_module_info(info)
  modules_path = File.join(ZohoHub.root, 'cache', 'modules')
  FileUtils.mkdir_p(modules_path)
  file_name = File.join(modules_path, "#{info[:api_name]}.json")

  File.open(file_name, 'w') do |file|
    file.write(JSON.pretty_generate(info))
  end

  return unless info[:api_supported]

  cache_module_fields(info)
end
configuration_incomplete?(refresh_token) click to toggle source
# File lib/zoho_hub/cli/read_modules.rb, line 76
def configuration_incomplete?(refresh_token)
  return true unless refresh_token

  !ZohoHub.configuration.client_id || !ZohoHub.configuration.secret
end
error_output(error) click to toggle source
# File lib/zoho_hub/cli/read_modules.rb, line 114
def error_output(error)
  warn "Error: #{error}"
  warn "Try `#{parser.program_name} server --help' for more information"

  false
end
good_run(argv, env) click to toggle source
# File lib/zoho_hub/cli/read_modules.rb, line 53
def good_run(argv, env)
  return false unless parse(argv, env)

  true
end
parse(argv, _env) click to toggle source
# File lib/zoho_hub/cli/read_modules.rb, line 107
def parse(argv, _env)
  parser.parse!(argv)
  true
rescue OptionParser::ParseError => e
  error_output(e)
end
parser() click to toggle source
# File lib/zoho_hub/cli/read_modules.rb, line 17
def parser
  @parser ||= OptionParser.new do |op|
    op.banner = "Usage: #{op.program_name} read-modules -c CLIENT_ID -s SECRET"

    op.on('-c', '--client-id=client_id', 'The Zoho client ID') do |client|
      @options[:client_id] = client
    end

    op.on('-s', '--secret=secret', 'The Zoho secret') do |secret|
      @options[:secret] = secret
    end

    op.on('-r', '--refresh-token=token', 'Your refresh token') do |refresh|
      @options[:refresh_token] = refresh
    end
  end
end
run(argv = ARGV, env = ENV) click to toggle source
# File lib/zoho_hub/cli/read_modules.rb, line 35
def run(argv = ARGV, env = ENV)
  exit 1 unless good_run(argv, env)

  setup_connection

  client_id = @options[:client_id] || ENV['ZOHO_CLIENT_ID']
  puts "Reading modules for client ID: #{client_id}..."

  modules_hashes = ZohoHub::Settings::Module.all_json

  puts "Found #{modules_hashes.size} modules"

  modules_hashes.each do |hash|
    puts "- Caching configuration for #{hash[:plural_label]}"
    cache_module_info(hash)
  end
end
setup_connection() click to toggle source
# File lib/zoho_hub/cli/read_modules.rb, line 59
def setup_connection
  ZohoHub.configure do |config|
    config.client_id    = @options[:client_id] || ENV['ZOHO_CLIENT_ID']
    config.secret       = @options[:secret] || ENV['ZOHO_SECRET']
  end

  refresh_token = @options[:refresh_token] || ENV['ZOHO_REFRESH_TOKEN']
  token_params = ZohoHub::Auth.refresh_token(refresh_token)

  if configuration_incomplete?(refresh_token)
    parser.parse %w[--help]
    exit 1
  end

  ZohoHub.setup_connection(token_params)
end