Class: CargoServer

Inherits:
Object
  • Object
show all
Defined in:
lib/cargo/cargo_server.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
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
50
51
52
53
54
55
# File 'lib/cargo/cargo_server.rb', line 12

def call(env)
  if env['REQUEST_METHOD'] == 'OPTIONS'
    [200, {
      'Access-Control-Allow-Origin' => '*', # TODO get this working with restrictions
      'Access-Control-Allow-Methods' => 'GET, OPTIONS',
      'Access-Control-Allow-Headers' => 'X-Prototype-Version, X-Requested-With, Accept',
      'Access-Control-Max-Age' => '1728000',
      'Access-Control' => 'allow <*>'
      }, '']
  else
    req = Rack::Request.new(env)
    puts req.path_info
    case req.path_info

    when '/start' # start a story
      puts Cargo::Commands::Hack.new(req['name'], req['id'])
      notify("Starting Story", "Starting story '#{req['name']}' [#{req['id']}]", 0)
      
      [200, { 'Content-Type' => 'application/json; charset=utf-8'},
        "{success: 'starting story'}"]

    when '/finish' # finish a story
      branch = git_branch
      story_id = branch.split('-').last
      if story_id == req['id']
        puts Cargo::Commands::Ship.new
        notify("Finishing Story", "Finishing story '#{req['name']}' [#{req['id']}]", 0)
      else
        notify("Story id doesn't match current branch", "Story id doesn't match current branch '#{branch}' [#{req['id']}]", 1)
      end
      
      [200, {'Content-Type' => 'application/json; charset=utf-8'},
        "{success: 'finishing story'}"]

    when '/cargo.user.js' # send the user script file
      [200, {'Content-Type' => 'text/javascript'},
        File.read("#{File.dirname(__FILE__)}/../../files/cargo.user.js")]

    else # handle everything else like a 404
      [404, {'Content-Type' => 'text/html'}, '404 Not Found']

    end
  end
end

#escape(name) ⇒ Object



57
58
59
60
61
62
# File 'lib/cargo/cargo_server.rb', line 57

def escape(name)
  name.gsub!(/\W+/, ' ') # all non-word chars to spaces
  name.strip!            # ohh la la
  name.downcase!         #
  name.gsub!(/\ +/, '_')
end

#notify(title, message, priority) ⇒ Object



64
65
66
67
# File 'lib/cargo/cargo_server.rb', line 64

def notify(title, message, priority)
  puts message
  system "growlnotify -n Cargo --image #{File.dirname(__FILE__)}/../../files/cargo.png -p #{priority} -m #{message} #{title}"
end