[from now] 2014/03/09 19:23:27

diff --git a/lib/octogate.rb b/lib/octogate.rb
index af82d29..cd38b23 100644
--- a/lib/octogate.rb
+++ b/lib/octogate.rb
@@ -28,15 +28,13 @@ module Octogate
     end

     def add_sent(target)
-      @sent ||= []
-      @sent.pop if @sent.length > REC_LIMIT
-      @sent.unshift target
+      sent.pop if sent.length > REC_LIMIT
+      sent.unshift target
     end

     def add_received(event)
-      @received ||= []
-      @received.pop if @received.length > REC_LIMIT
-      @received.unshift event
+      received.pop if received.length > REC_LIMIT
+      received.unshift event
     end
   end
 end
@@ -47,4 +45,5 @@ require "octogate/config_loader"
 require "octogate/target"
 require "octogate/target_builder"
 require "octogate/events"
+require "octogate/sent_event"
 require "octogate/gh"
diff --git a/lib/octogate/client.rb b/lib/octogate/client.rb
index 8cf1354..6afacf0 100644
--- a/lib/octogate/client.rb
+++ b/lib/octogate/client.rb
@@ -38,8 +38,8 @@ class Octogate::Client
         .do_request(url: uri.path, params: params, parameter_type: target.parameter_type)
     end

-    target.sent_at = Time.now
-    Octogate.add_sent(target)
+    sent_event = Octogate::SentEvent.build(event, target, params)
+    Octogate.add_sent(sent_event)
   end

   private
diff --git a/lib/octogate/events/base.rb b/lib/octogate/events/base.rb
index 73fba26..6a7e8d1 100644
--- a/lib/octogate/events/base.rb
+++ b/lib/octogate/events/base.rb
@@ -1,3 +1,5 @@
+require "oj"
+
 module Octogate
   class Event::Base < Octogate::Model
     class << self
@@ -9,6 +11,12 @@ module Octogate
           end
         end
       end
+
+      def parse(delivery_id, json)
+        payload = Oj.load(json).deep_symbolize_keys
+
+        new(payload.merge(delivery_id: delivery_id))
+      end
     end

     def default_condition
diff --git a/lib/octogate/events/issue.rb b/lib/octogate/events/issue.rb
index 7b4609f..d943fcb 100644
--- a/lib/octogate/events/issue.rb
+++ b/lib/octogate/events/issue.rb
@@ -8,13 +8,5 @@ module Octogate
     coerce_key :issue,        GH::Issue
     coerce_key :repository,   GH::Repository
     coerce_key :sender,       GH::User
-
-    class << self
-      def parse(json)
-        payload = Oj.load(json).deep_symbolize_keys
-
-        new(payload)
-      end
-    end
   end
 end
diff --git a/lib/octogate/events/issue_comment.rb b/lib/octogate/events/issue_comment.rb
index ec5c4e4..9ce439b 100644
--- a/lib/octogate/events/issue_comment.rb
+++ b/lib/octogate/events/issue_comment.rb
@@ -9,13 +9,5 @@ module Octogate
     coerce_key :comment,      GH::IssueComment
     coerce_key :repository,   GH::Repository
     coerce_key :sender,       GH::User
-
-    class << self
-      def parse(json)
-        payload = Oj.load(json).deep_symbolize_keys
-
-        new(payload)
-      end
-    end
   end
 end
diff --git a/lib/octogate/events/pull_request.rb b/lib/octogate/events/pull_request.rb
index 6bf53fc..1077d0f 100644
--- a/lib/octogate/events/pull_request.rb
+++ b/lib/octogate/events/pull_request.rb
@@ -9,14 +9,6 @@ module Octogate
     coerce_key :repository,   GH::Repository
     coerce_key :sender,       GH::User

-    class << self
-      def parse(json)
-        payload = Oj.load(json).deep_symbolize_keys
-
-        new(payload)
-      end
-    end
-
     def default_condition
       action != "closed"
     end
diff --git a/lib/octogate/events/pull_request_review_comment.rb b/lib/octogate/events/pull_request_review_comment.rb
index fdbf1d7..2a7601b 100644
--- a/lib/octogate/events/pull_request_review_comment.rb
+++ b/lib/octogate/events/pull_request_review_comment.rb
@@ -8,13 +8,5 @@ module Octogate
     coerce_key :comment,      GH::ReviewComment
     coerce_key :repository,   GH::Repository
     coerce_key :sender,       GH::User
-
-    class << self
-      def parse(json)
-        payload = Oj.load(json).deep_symbolize_keys
-
-        new(payload)
-      end
-    end
   end
 end
diff --git a/lib/octogate/events/push.rb b/lib/octogate/events/push.rb
index 39fcb0b..73192c4 100644
--- a/lib/octogate/events/push.rb
+++ b/lib/octogate/events/push.rb
@@ -9,14 +9,14 @@ module Octogate
     coerce_key :repository,  GH::Repository

     class << self
-      def parse(json)
+      def parse(delivery_id, json)
         payload = Oj.load(json).deep_symbolize_keys

         commits = payload[:commits].nil? ? [] : payload.delete(:commits).map do |c|
           GH::Commit.new(c.symbolize_keys)
         end

-        attrs = payload.merge(commits: commits)
+        attrs = payload.merge(delivery_id: delivery_id, commits: commits)

         new(attrs)
       end
diff --git a/lib/octogate/sent_event.rb b/lib/octogate/sent_event.rb
new file mode 100644
index 0000000..ad340fa
--- /dev/null
+++ b/lib/octogate/sent_event.rb
@@ -0,0 +1,21 @@
+require "oj"
+
+module Octogate
+  class SentEvent < Model
+    class << self
+      def build(event, target, params, sent_at = Time.now)
+        new(
+          event: event,
+          target_name: target.name,
+          target_url: target.url,
+          sent_payload: params,
+          sent_at: sent_at
+        )
+      end
+    end
+
+    def delivery_id
+      event.delivery_id
+    end
+  end
+end
diff --git a/lib/octogate/server.rb b/lib/octogate/server.rb
index 5bfe847..b0924f8 100644
--- a/lib/octogate/server.rb
+++ b/lib/octogate/server.rb
@@ -16,25 +16,31 @@ class Octogate::Server < Sinatra::Base
     haml :received_events, locals: {received: received}
   end

-  get '/sent_targets' do
+  get '/sent_events' do
     sent = Octogate.sent
-    haml :sent_targets, locals: {sent: sent}
+    haml :sent_events, locals: {sent: sent}
   end

   post '/:token' do
-    unless Octogate.config.token == params[:token]
+    unless Octogate.conffig.token == params[:token]
       status 403
       body "Access forbidden"
       return
     end

-    event_name = request.env["HTTP_X_GITHUB_EVENT"]
-    event_klass = Octogate::Event.get(event_name)
-    event = event_klass.parse(params[:payload])
-    event.received_at = Time.now
+    event = build_event_from(request)
     Octogate.add_received(event)
     Octogate::Client.new(event).request_to_targets

     return
   end
+
+  def build_event_from(request)
+    delivery_id = request.env["HTTP_X_GITHUB_DELIVERY"]
+    event_name = request.env["HTTP_X_GITHUB_EVENT"]
+    event_klass = Octogate::Event.get(event_name)
+    event = event_klass.parse(delivery_id, params[:payload])
+    event.received_at = Time.now
+    event
+  end
 end
diff --git a/spec/lib/octogate/client_spec.rb b/spec/lib/octogate/client_spec.rb
index fefd6ed..305a6a9 100644
--- a/spec/lib/octogate/client_spec.rb
+++ b/spec/lib/octogate/client_spec.rb
@@ -2,7 +2,7 @@ require 'spec_helper'

 describe Octogate::Client do
   describe "#request_to_targets" do
-    let(:event) { Octogate::Event::Push.parse(read_payload(:push)) }
+    let(:event) { Octogate::Event::Push.parse("delivery_id", read_payload(:push)) }

     before do
       stub_request(:post, "http://targethost.dev/job/JobName")
diff --git a/spec/lib/octogate/events/issue_comment_spec.rb b/spec/lib/octogate/events/issue_comment_spec.rb
index bd5596d..e002491 100644
--- a/spec/lib/octogate/events/issue_comment_spec.rb
+++ b/spec/lib/octogate/events/issue_comment_spec.rb
@@ -2,7 +2,7 @@ require 'spec_helper'

 describe Octogate::Event::IssueComment do
   describe ".parse" do
-    subject { described_class.parse(read_payload(:issue_comment)) }
+    subject { described_class.parse("delivery_id", read_payload(:issue_comment)) }
     it { should be_a described_class }

     it { expect(subject.action).to eq "created" }
diff --git a/spec/lib/octogate/events/issue_spec.rb b/spec/lib/octogate/events/issue_spec.rb
index 72a073c..30ce0de 100644
--- a/spec/lib/octogate/events/issue_spec.rb
+++ b/spec/lib/octogate/events/issue_spec.rb
@@ -2,7 +2,7 @@ require 'spec_helper'

 describe Octogate::Event::Issue do
   describe ".parse" do
-    subject { described_class.parse(read_payload(:issues)) }
+    subject { described_class.parse("delivery_id", read_payload(:issues)) }
     it { should be_a described_class }

     it { expect(subject.action).to eq "opened" }
diff --git a/spec/lib/octogate/events/pull_request_review_comment_spec.rb b/spec/lib/octogate/events/pull_request_review_comment_spec.rb
index a6019be..e99189a 100644
--- a/spec/lib/octogate/events/pull_request_review_comment_spec.rb
+++ b/spec/lib/octogate/events/pull_request_review_comment_spec.rb
@@ -2,7 +2,7 @@ require 'spec_helper'

 describe Octogate::Event::PullRequestReviewComment do
   describe ".parse" do
-    subject { described_class.parse(read_payload(:pull_request_review_comment)) }
+    subject { described_class.parse("delivery_id", read_payload(:pull_request_review_comment)) }
     it { should be_a described_class }

     it { expect(subject.comment).to be_a Octogate::GH::ReviewComment }
diff --git a/spec/lib/octogate/events/pull_request_spec.rb b/spec/lib/octogate/events/pull_request_spec.rb
index 9b953c6..e9d0f02 100644
--- a/spec/lib/octogate/events/pull_request_spec.rb
+++ b/spec/lib/octogate/events/pull_request_spec.rb
@@ -2,7 +2,7 @@ require 'spec_helper'

 describe Octogate::Event::PullRequest do
   describe ".parse" do
-    subject { described_class.parse(read_payload(:pull_request)) }
+    subject { described_class.parse("delivery_id", read_payload(:pull_request)) }
     it { should be_a described_class }

     it { expect(subject.action).to eq "opened" }
diff --git a/spec/lib/octogate/events/push_spec.rb b/spec/lib/octogate/events/push_spec.rb
index ab965c1..be4b04f 100644
--- a/spec/lib/octogate/events/push_spec.rb
+++ b/spec/lib/octogate/events/push_spec.rb
@@ -2,7 +2,7 @@ require 'spec_helper'

 describe Octogate::Event::Push do
   describe ".parse" do
-    subject { described_class.parse(read_payload(:push)) }
+    subject { described_class.parse("delivery_id", read_payload(:push)) }
     it { should be_a described_class }

     it { expect(subject.ref).to eq "refs/heads/master" }
diff --git a/web/views/received_events.haml b/web/views/received_events.haml
index 83c36c0..3197c91 100644
--- a/web/views/received_events.haml
+++ b/web/views/received_events.haml
@@ -9,12 +9,14 @@
   %table.table
     %thead
       %tr
+        %th Delivery ID
         %th Event Type
         %th Repository
         %th Received at
     %tbody
       - received.each do |r|
         %tr
+          %td= r.delivery_id
           %td= r.name
           %td= r.repository.full_name || r.repository.name
           %td= r.received_at.strftime("%Y/%m/%d %H:%M:%S")
diff --git a/web/views/sent_targets.haml b/web/views/sent_targets.haml
index 3c67050..a81ffbb 100644
--- a/web/views/sent_targets.haml
+++ b/web/views/sent_targets.haml
@@ -1,20 +1,24 @@
 !!! 5
 %html
   %head
-    %title Octogate Sent Events (Recently 100 Targets)
+    %title Octogate Sent Events
     %link{rel: "stylesheet", href: "/bootstrap/css/bootstrap.css"}

 %body
-  %h2 Octogate Received Events
+  %h2 Octogate Sent Events (Recently 100 Targets)
   %table.table
     %thead
       %tr
+        %th Delivery ID
+        %th Event Name
         %th Target Name
         %th URL
         %th Sent at
     %tbody
       - sent.each do |s|
         %tr
+          %td= s.delivery_id
           %td= s.name
-          %td= s.url
+          %td= s.target_name
+          %td= s.target_url
           %td= s.sent_at.strftime("%Y/%m/%d %H:%M:%S")