Class: Lono::Registration::Temp

Inherits:
Base
  • Object
show all
Defined in:
lib/lono/registration/temp.rb

Instance Method Summary collapse

Methods inherited from Base

#api, #get_temp_key, #initialize, #request_verification, #say, #with_safety

Constructor Details

This class inherits a constructor from Lono::Registration::Base

Instance Method Details

#checkObject



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/lono/registration/temp.rb', line 4

def check
  info = read_registration
  if info
    resp = request_verification(info)
    puts "request_verification resp #{resp.inspect}" if ENV['LONO_DEBUG_REGISTRATION']
    # resp nil means non-200 http response. Failsafe behavior is to continue.
    return true if resp.nil?
  end

  prompt unless resp && resp[:valid]
  true
end

#promptObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/lono/registration/temp.rb', line 21

def prompt
  return if ENV['LONO_TEST']

  # We get the api first before the prompt to check if api is up
  resp = get_temp_key
  # resp nil means non-200 http response. Failsafe behavior is to continue.
  if resp.nil?
    return true
  end

  puts <<~EOL
    Lono is not registered. To remove this prompt, please set up your registration
    info in .lono/registration.yml. Registration is free. You can register at:

        https://register.lono.cloud

    More info: https://lono.cloud/docs/register/

    Continue temporarily without registration? (y/N)
  EOL

  answer = $stdin.gets.to_s.strip # nil on CI
  if answer !~ /^y/i
    puts "Exiting."
    exit 1
  end

  save_temp_key(resp) # save key if user confirms
end

#read_registrationObject



17
18
19
# File 'lib/lono/registration/temp.rb', line 17

def read_registration
  YAML.load_file(temp_path) if File.exist?(temp_path)
end

#save_temp_key(info) ⇒ Object



51
52
53
54
# File 'lib/lono/registration/temp.rb', line 51

def save_temp_key(info)
  FileUtils.mkdir_p(File.dirname(temp_path))
  IO.write(temp_path, YAML.dump(info.deep_stringify_keys))
end

#temp_pathObject



56
57
58
# File 'lib/lono/registration/temp.rb', line 56

def temp_path
  "#{ENV['HOME']}/.lono/temp.yml"
end