Class: Lh

Inherits:
Object show all
Defined in:
lib/lh.rb

Defined Under Namespace

Classes: Error, Project, Session, Ticket

Class Method Summary collapse

Class Method Details

.ask_dproj(projects) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/lh.rb', line 112

def self.ask_dproj (projects)
  print 'Your account has multiple projects. Set a default (recommended)? [Yn]: '
  if %w(y Y).include?(gets.chomp)
    print 'Enter the project name or id: '
    Lh::Project.id_from_input(gets.chomp, projects)
  else
    puts "Set the project with `--project <id>' in all subsequent commands."
  end
end

.assign_default_project(opts) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/lh.rb', line 88

def self.assign_default_project (opts)
  if opts[:default_project].nil?
    opts[:default_project] = Lh.find_dproj()
  else 
    opts[:default_project] = Lh.parse_proj(opts[:default_project])
  end
  Lh.write_config(opts)
end

.filter_opts(opts) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/lh.rb', line 72

def self.filter_opts (opts)
  { account:         opts[:account],
    token:           opts[:token],
    user:            opts[:user],
    password:        opts[:password],
    default_project: opts[:default_project] }
end

.find_dprojObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/lh.rb', line 101

def self.find_dproj
  projects = Lh::Project.return_all_filtered
  if projects.length == 1
    projects[0][:id].to_i
  elsif projects.length > 1
    Lh.ask_dproj(projects)
  else
    raise "Your account doesn't have any projects."
  end
end

.get_dprojObject



97
98
99
# File 'lib/lh.rb', line 97

def self.get_dproj
  Lh.load_opts[:default_project]
end

.init(opts = {}) ⇒ Object



55
56
57
58
59
60
# File 'lib/lh.rb', line 55

def self.init (opts = {})
  opts_filt = Lh.filter_opts(Lh.validate_opts(opts))
  Lh.write_config(opts_filt)
  Lh::Session.establish(opts_filt)
  Lh.assign_default_project(opts_filt)
end

.load_optsObject



126
127
128
129
130
131
132
133
# File 'lib/lh.rb', line 126

def self.load_opts
  begin
    YAML::load(File.open('.lh/config.yml'))
  rescue
    puts 'Config file not found in this directory.'
    puts 'Are you sure you initialized it here?'
  end
end

.parse_proj(proj) ⇒ Object



122
123
124
# File 'lib/lh.rb', line 122

def self.parse_proj (proj)
  Lh::Project.id_from_input(proj, Lh::Project.return_all_filtered)
end

.validate_opts(opts) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/lh.rb', line 62

def self.validate_opts (opts)
  raise Lh::Error::InvalidOpts, 'Account name required.' if opts[:account].nil?
  if opts[:user].present?
    raise Lh::Error::InvalidOpts, 'Password required.' if opts[:password].nil?
  elsif opts[:token].nil?
    raise Lh::Error::InvalidOpts, 'Token or credentials required.'
  end
  opts
end

.write_config(opts) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/lh.rb', line 80

def self.write_config (opts)
  FileUtils.rm_rf('.lh')
  Dir::mkdir('.lh')
  File.open('.lh/config.yml', 'w') do |file|
    file.write(opts.to_yaml)
  end
end