Class: CargoServer

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

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



11
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
# File 'lib/cargo/cargo_server.rb', line 11

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 `hack #{escape(req['name'])[0..30]} #{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 `ship`
        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



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

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



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

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