From bb3af8d24bf700d996471d7e851fefb1020ace72 Mon Sep 17 00:00:00 2001 From: phoet Date: Wed, 21 May 2014 21:33:49 -0400 Subject: [PATCH] add a to_curl to notice and show on summary --- app/models/notice.rb | 10 ++++++++++ app/views/notices/_summary.html.haml | 3 +++ spec/models/notice_spec.rb | 11 +++++++++++ 3 files changed, 24 insertions(+), 0 deletions(-) diff --git a/app/models/notice.rb b/app/models/notice.rb index 9d3b43b..14c2e34 100644 --- a/app/models/notice.rb +++ b/app/models/notice.rb @@ -78,6 +78,16 @@ class Notice "N/A" end + def to_curl + headers = %w(Accept Accept-Encoding Accept-Language Cookie Referer User-Agent).each_with_object([]) do |name, h| + if value = env_vars["HTTP_#{name.underscore.upcase}"] + h << "-H '#{name}: #{value}'" + end + end + + "curl -X #{env_vars['REQUEST_METHOD'] || 'GET'} #{headers.join(' ')} #{request['url']}" + end + def env_vars request['cgi-data'] || {} end diff --git a/app/views/notices/_summary.html.haml b/app/views/notices/_summary.html.haml index 1ca6d54..ed43380 100644 --- a/app/views/notices/_summary.html.haml +++ b/app/views/notices/_summary.html.haml @@ -10,6 +10,9 @@ %tr %th URL %td.nowrap= link_to notice.request['url'], notice.request['url'] + %tr + %th   + %td= notice.to_curl %tr %th Where %td= notice.where diff --git a/spec/models/notice_spec.rb b/spec/models/notice_spec.rb index 2f16353..e0085d5 100644 --- a/spec/models/notice_spec.rb +++ b/spec/models/notice_spec.rb @@ -35,6 +35,17 @@ describe Notice do end end + context "to_curl" do + let(:request) { {'url' => "http://example.com/resource/12", 'cgi-data' => {'HTTP_USER_AGENT' => 'Mozilla/5.0'}} } + let(:notice) { Fabricate.build(:notice, request: request) } + + it 'has a curl representation' do + cmd = notice.to_curl + + cmd.should eql(%q[curl -X GET -H 'User-Agent: Mozilla/5.0' http://example.com/resource/12]) + end + end + describe "user agent" do it "should be parsed and human-readable" do notice = Fabricate.build(:notice, :request => {'cgi-data' => { -- libgit2 0.21.2