Class: ZohoHub::Cli::ReadModules

Inherits:
Object
  • Object
show all
Defined in:
lib/zoho_hub/cli/read_modules.rb

Instance Method Summary collapse

Constructor Details

#initializeReadModules

Returns a new instance of ReadModules.



13
14
15
# File 'lib/zoho_hub/cli/read_modules.rb', line 13

def initialize
  @options = {}
end

Instance Method Details

#cache_module_fields(info) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# 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) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
# 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) ⇒ Object



114
115
116
117
118
119
# 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) ⇒ Object



53
54
55
56
57
# 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) ⇒ Object



107
108
109
110
111
112
# 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

#parserObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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_connectionObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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