Class: Bro::BroState

Inherits:
State
  • Object
show all
Defined in:
lib/bro/bro_state.rb

Instance Method Summary collapse

Methods inherited from State

#initialize, #read_state, #write_state

Constructor Details

This class inherits a constructor from Bro::State

Instance Method Details

#check_emailObject



17
18
19
20
21
22
23
24
# File 'lib/bro/bro_state.rb', line 17

def check_email
  begin
    is_invalid_code read_state[:code], read_state[:email]
  rescue => e
    prompt_email
  end
  {code: read_state[:code], email: read_state[:email]}
end

#get_arg_or_last_command(args) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/bro/bro_state.rb', line 3

def get_arg_or_last_command args
  cmd = args.join(" ")
  if args.empty?
    state = read_state
    cmd = state[:cmd].strip
  end
  cmd
end

#is_invalid_code(code, email) ⇒ Object



59
60
61
# File 'lib/bro/bro_state.rb', line 59

def is_invalid_code code, email
  res = RestClient.get URL + "/users/verify?code=#{code}&email=#{email}"
end

#is_invalid_email(email) ⇒ Object



63
64
65
66
# File 'lib/bro/bro_state.rb', line 63

def is_invalid_email email
  regex = /^[a-zA-Z0-9_.+\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-.]+$/
  email.scan(regex).empty?
end

#prompt_emailObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bro/bro_state.rb', line 26

def prompt_email
  say "Bropages.org requires an email address verification to do this".colored.yellow
  
  email = ""

  while is_invalid_email email
    email = ask "What's your email address?".colored.green
  end

  begin
    res = RestClient.post URL + '/users.json', { user: { email: email }, format: 'json', multipart: true }
  rescue => e
    say "There was an error delivering to your email address. Please try again later".colored.yellow.on_red
    raise e
  else
    say "Great! We're sending an email to #{email}".success

    invalid_code = true
    begin
      code = ask "Please enter the verification code: "
      begin
        is_invalid_code code, email
        invalid_code = false
        say "Great! You're verified! FYI, your email and code are stored locally in ~/.bro".success
        write_state({ email: email, code: code })
      rescue => e
        say "Woops, there was a problem verifying your email. Please try again".colored.yellow.on_red
      end
    end while invalid_code
  end
  
end

#reset_lookup_idsObject



12
13
14
15
# File 'lib/bro/bro_state.rb', line 12

def reset_lookup_ids
  # drop all lookup ids
  write_state read_state().keep_if { |x| !!!(x =~ /\d+/) }, true
end