Class: Bro::BroState

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

Constant Summary collapse

COLOR_OFF =
"nocolor"

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_colorObject

true/false if color should be used



6
7
8
9
# File 'lib/bro/bro_state.rb', line 6

def check_color
  state = read_state
  return state[:color] != COLOR_OFF
end

#check_emailObject



28
29
30
31
32
33
34
35
# File 'lib/bro/bro_state.rb', line 28

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



11
12
13
14
15
16
17
18
19
20
# File 'lib/bro/bro_state.rb', line 11

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

#is_invalid_code(code, email) ⇒ Object



74
75
76
77
# File 'lib/bro/bro_state.rb', line 74

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

#is_invalid_email(email) ⇒ Object



79
80
81
82
# File 'lib/bro/bro_state.rb', line 79

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bro/bro_state.rb', line 37

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

  begin
    while is_invalid_email email
      email = ask "What's your email address?".colored.green
    end
    begin
      email_param = CGI.escape(email)
      res = RestClient.post URL + '/users.json', { :user => { :email => email_param }, :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
  rescue Interrupt
    say "Canceled email verification".status
    raise
  end
end

#reset_lookup_idsObject



22
23
24
25
26
# File 'lib/bro/bro_state.rb', line 22

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