Class: Iated::MCP

Inherits:
Object
  • Object
show all
Defined in:
lib/iated/mcp.rb

Overview

The Master Control Progrom

Yes, I’ve been watching Tron lately, why do ask?

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMCP

Returns a new instance of MCP.



23
24
25
26
27
# File 'lib/iated/mcp.rb', line 23

def initialize
  @debug = false
  @prefs = SysPref.new
  @browser_token_db = Iated::BrowserTokenDB.new(@prefs.config_dir + 'browser_tokens.yml')
end

Instance Attribute Details

#browser_token_dbObject (readonly)

Returns the value of attribute browser_token_db.



18
19
20
# File 'lib/iated/mcp.rb', line 18

def browser_token_db
  @browser_token_db
end

#debugObject

Returns the value of attribute debug.



16
17
18
# File 'lib/iated/mcp.rb', line 16

def debug
  @debug
end

#prefsSysPref (readonly)

Returns The current System Preferencs object.

Returns:

  • (SysPref)

    The current System Preferencs object



21
22
23
# File 'lib/iated/mcp.rb', line 21

def prefs
  @prefs
end

Instance Method Details

#begin!Object

Called by the main program to get everything started.



30
31
32
# File 'lib/iated/mcp.rb', line 30

def begin!
  start_server
end

#confirm_secret(guess) ⇒ Boolean

Confirm the secret

Parameters:

  • guess (String)

    The string to check against the secret

Returns:

  • (Boolean)

    Was the guess right?



43
44
45
46
47
# File 'lib/iated/mcp.rb', line 43

def confirm_secret guess
  success = (guess == secret)
  hide_secret
  return success
end

#cucumber_coverage_checkObject



135
136
137
# File 'lib/iated/mcp.rb', line 135

def cucumber_coverage_check
  @cucumber_check = true
end

#generate_secretString

The magic value shown to user to confirm a connection

Returns:

  • (String)

    The string



82
83
84
85
86
# File 'lib/iated/mcp.rb', line 82

def generate_secret
  @secret = (1..4).map{ rand(10).to_s }.join ""
  @is_showing_secret = true
  return @secret
end

#generate_token(user_agent) ⇒ String

The magic value registering a browser.

Returns:

  • (String)

    The token



90
91
92
# File 'lib/iated/mcp.rb', line 90

def generate_token user_agent
  @browser_token_db.add user_agent
end

#hide_secretnil

Hide the secret again

Returns:

  • (nil)


75
76
77
78
# File 'lib/iated/mcp.rb', line 75

def hide_secret
  @is_showing_secret = false
  # TODO secret should be using a JPanel or something, not a dialog.
end

#is_token_valid?(token) ⇒ Boolean

Is the auth token a valid one?

Returns:

  • (Boolean)

    True if the token is valid and exist



96
97
98
# File 'lib/iated/mcp.rb', line 96

def is_token_valid? token
  @browser_token_db.has_token? token
end

#rspec_coverage_checkObject



139
140
141
# File 'lib/iated/mcp.rb', line 139

def rspec_coverage_check
  @rspec_check = true
end

#secretString, Symbol

Returns The currently showing secret or the symbol ‘:notset`.

Returns:

  • (String, Symbol)

    The currently showing secret or the symbol ‘:notset`



35
36
37
# File 'lib/iated/mcp.rb', line 35

def secret
  showing_secret? ? @secret : :notset
end

#show_secretnil

Show the secret to the user

Returns:

  • (nil)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/iated/mcp.rb', line 56

def show_secret
  generate_secret
  # TODO This should do something different for text, vs. gui.
  puts " ** A browser requested authorization: #{@secret}" unless :test == ui
##      require 'java'
##      Thread.new do
##        puts " ** A browser requested authorization: #{@secret}"
### TODO This needs to be abstracted. There is no reason a purely command line version can't be done. Also, loading swing will break CI.
###        import javax.swing.JOptionPane
###        JOptionPane.showMessageDialog(nil,
###                                      "A browser has requested authorization: #{@secret}\nDo not press 'OK' until after you enter the number",
###                                      "Authorize Browser",
###                                      JOptionPane::INFORMATION_MESSAGE)
##        @is_showing_secret = false
##      end
end

#showing_secret?Boolean

Returns Is the secret being shown to the user?.

Returns:

  • (Boolean)

    Is the secret being shown to the user?



50
51
52
# File 'lib/iated/mcp.rb', line 50

def showing_secret?
  @is_showing_secret
end

#start_serverObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/iated/mcp.rb', line 113

def start_server
  # TODO this needs to be in a separate thread.
  set :server, %w[webrick]
  set :bind, 'localhost'
  set :port, prefs.port
  if @debug
    set :show_exceptions, true
    set :environment, :development
  else
    set :show_exceptions, false
    set :environment, :production
  end

  @is_running = true
  Sinatra::Application.run!
end

#stop_serverObject



130
131
132
133
# File 'lib/iated/mcp.rb', line 130

def stop_server
  @is_running = false
  Sinatra::Application.quit!
end

#uiObject



108
109
110
111
# File 'lib/iated/mcp.rb', line 108

def ui
  # Default UI
  @ui ||= (:test == Iated.environment ? :test : :text)
end

#ui=(ui_type) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/iated/mcp.rb', line 100

def ui= ui_type
  if [:text, :test, :gui].include? ui_type
    @ui = ui_type
  else
    raise "Invalid UI type: #{ui_type.inspect}"
  end
end