Class: BattleshipTournament::Submit

Inherits:
Object
  • Object
show all
Defined in:
lib/battleship_tournament/submit.rb

Instance Method Summary collapse

Constructor Details

#initialize(gem_name) ⇒ Submit

Returns a new instance of Submit.



9
10
11
12
13
# File 'lib/battleship_tournament/submit.rb', line 9

def initialize(gem_name)
  @gem_name = gem_name
  @input = $stdin
  @output = $stdout
end

Instance Method Details

#collect_dataObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/battleship_tournament/submit.rb', line 33

def collect_data
  spec = gem_spec
  data = {}
  data[:name] = spec.summary[18..-1]
  data[:author] = spec.author
  data[:email] = spec.email
  data[:description] = spec.description
  data[:gem_file_name] = spec.file_name     
  gem_file_path = File.expand_path(File.join(spec.full_gem_path, "..", "..", 'cache', spec.file_name))
  data[:gem_content] = IO.read(gem_file_path)
  return data
end

#gem_specObject



46
47
48
49
50
51
52
53
54
# File 'lib/battleship_tournament/submit.rb', line 46

def gem_spec
  if @spec.nil?
    @spec = Gem.source_index.latest_specs.find do |spec|
      spec.name == @gem_name
    end
  end
  quit "Could not find installed gem named '#{@gem_name}'" if @spec.nil?
  return @spec
end

#get_passwordObject



56
57
58
59
60
61
62
# File 'lib/battleship_tournament/submit.rb', line 56

def get_password
  @output.puts "Password?"
  @output.print "> "

  password = @input.readline.strip
  return password
end

#quit(message) ⇒ Object



64
65
66
67
# File 'lib/battleship_tournament/submit.rb', line 64

def quit(message)
  puts message
  exit -1
end

#submitObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/battleship_tournament/submit.rb', line 15

def submit
  data = collect_data
  password = get_password
  password = Digest::SHA1.hexdigest(password)
  data[:password] = password

  begin
    server = DRbObject.new(nil, 'druby://micahmartin.com:9696')
    authorized = server.authorize(data[:name], password)
    quit "Incorrect password.  You are not authorized to submit this player." unless authorized
    result = server.submit_profile(data)
    quit "Failed to submit: #{result}" if result != true
    puts "Player #{data[:name]} has been submitted."
  rescue DRb::DRbConnError => e
    quit "Could not connect to battleship server!"
  end
end