Class: Bobette::GitHub

Inherits:
Object
  • Object
show all
Defined in:
lib/bobette/github.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, &block) ⇒ GitHub

Returns a new instance of GitHub.



5
6
7
8
# File 'lib/bobette/github.rb', line 5

def initialize(app, &block)
  @app  = app
  @head = block || Proc.new { false }
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bobette/github.rb', line 10

def call(env)
  payload = Rack::Request.new(env).POST["payload"] || ""
  payload = JSON.parse(payload)
  payload["scm"]    = "git"
  payload["uri"]    = uri(payload.delete("repository"))
  payload["branch"] = payload.delete("ref").split("/").last
  if (head = payload.delete("after")) && @head.call
    payload["commits"] = [{"id" => head}]
  end

  @app.call(env.update("bobette.payload" => payload))
rescue JSON::JSONError
  Rack::Response.new("Unparsable payload", 400).finish
end

#uri(repository) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/bobette/github.rb', line 25

def uri(repository)
  if repository["private"]
    "[email protected]:#{URI(repository["url"]).path[1..-1]}"
  else
    URI(repository["url"]).tap { |u| u.scheme = "git" }.to_s
  end
end