From 710dcbe164c7c240085fccc94c4064cea0272856 Mon Sep 17 00:00:00 2001 From: Gabriel Mazetto Date: Fri, 21 Aug 2015 23:28:18 -0300 Subject: [PATCH 1/3] DNS responses are now rendered by templates. Added specific formatting for slack --- lib/lita-dig.rb | 6 +++++- lib/lita/handlers/dig.rb | 8 ++------ templates/compact.erb | 3 +++ templates/compact.slack.erb | 3 +++ templates/full.erb | 1 + templates/full.slack.erb | 1 + 6 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 templates/compact.erb create mode 100644 templates/compact.slack.erb create mode 100644 templates/full.erb create mode 100644 templates/full.slack.erb diff --git a/lib/lita-dig.rb b/lib/lita-dig.rb index 98a1906..9538763 100644 --- a/lib/lita-dig.rb +++ b/lib/lita-dig.rb @@ -5,5 +5,9 @@ )] require 'net/dns' - require 'lita/handlers/dig' + +Lita::Handlers::Dig.template_root File.expand_path( + File.join("..", "..", "templates"), + __FILE__ +) diff --git a/lib/lita/handlers/dig.rb b/lib/lita/handlers/dig.rb index a1d9d30..599da34 100644 --- a/lib/lita/handlers/dig.rb +++ b/lib/lita/handlers/dig.rb @@ -49,15 +49,11 @@ def lookup(argument, type, server = nil) end def format_lookup(lookup, compact = false) - result = '' if compact - lookup.each_address do |ip| - result += "#{ip}\n" - end + render_template('compact', ips: lookup.each_address.to_a) else - result = lookup.to_s + render_template('full', lookup: lookup) end - result end end diff --git a/templates/compact.erb b/templates/compact.erb new file mode 100644 index 0000000..f6011d4 --- /dev/null +++ b/templates/compact.erb @@ -0,0 +1,3 @@ +<% @ips.each do |ip| %> + <%= ip %> +<% end %> diff --git a/templates/compact.slack.erb b/templates/compact.slack.erb new file mode 100644 index 0000000..a991275 --- /dev/null +++ b/templates/compact.slack.erb @@ -0,0 +1,3 @@ +<% @ips.each do |ip| %> + `<%= ip %>` +<% end %> diff --git a/templates/full.erb b/templates/full.erb new file mode 100644 index 0000000..1b7fee5 --- /dev/null +++ b/templates/full.erb @@ -0,0 +1 @@ +<%= @lookup %> diff --git a/templates/full.slack.erb b/templates/full.slack.erb new file mode 100644 index 0000000..63813a7 --- /dev/null +++ b/templates/full.slack.erb @@ -0,0 +1 @@ +```<%= @lookup %>``` From 05e62100247c8c4d5c844fcdf057ecda35648d48 Mon Sep 17 00:00:00 2001 From: Gabriel Mazetto Date: Sat, 22 Aug 2015 10:38:22 -0300 Subject: [PATCH 2/3] Fixed failing specs, fixed code style issues --- lib/lita-dig.rb | 3 +-- lib/lita/handlers/dig.rb | 2 +- spec/lita/handlers/dig_spec.rb | 6 ++++-- templates/compact.erb | 4 ++-- templates/compact.slack.erb | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/lita-dig.rb b/lib/lita-dig.rb index 9538763..b9fcc9d 100644 --- a/lib/lita-dig.rb +++ b/lib/lita-dig.rb @@ -8,6 +8,5 @@ require 'lita/handlers/dig' Lita::Handlers::Dig.template_root File.expand_path( - File.join("..", "..", "templates"), - __FILE__ + File.join('..', '..', 'templates'), __FILE__ ) diff --git a/lib/lita/handlers/dig.rb b/lib/lita/handlers/dig.rb index 599da34..3ff4625 100644 --- a/lib/lita/handlers/dig.rb +++ b/lib/lita/handlers/dig.rb @@ -50,7 +50,7 @@ def lookup(argument, type, server = nil) def format_lookup(lookup, compact = false) if compact - render_template('compact', ips: lookup.each_address.to_a) + render_template('compact', lookup: lookup) else render_template('full', lookup: lookup) end diff --git a/spec/lita/handlers/dig_spec.rb b/spec/lita/handlers/dig_spec.rb index dc40d68..fcc1bf3 100644 --- a/spec/lita/handlers/dig_spec.rb +++ b/spec/lita/handlers/dig_spec.rb @@ -31,7 +31,9 @@ let(:resolve_short) do list = double - allow(list).to receive(:each_address).and_yield('1.2.3.4') + allow(list).to receive(:each_address) + .and_yield('1.2.3.4') + .and_yield('5.6.7.8') client = double allow(client).to receive(:nameservers=) { '' } expect(client).to receive(:query) { list } @@ -58,7 +60,7 @@ it 'shows a short record if the domain exists' do expect(Net::DNS::Resolver).to receive(:new) { resolve_short } send_command('dig example.com +short') - expect(replies.last).to eq("1.2.3.4\n") + expect(replies.last).to eq("1.2.3.4\n5.6.7.8\n") end it 'shows a warning if the domain does not exist' do diff --git a/templates/compact.erb b/templates/compact.erb index f6011d4..32246e8 100644 --- a/templates/compact.erb +++ b/templates/compact.erb @@ -1,3 +1,3 @@ -<% @ips.each do |ip| %> - <%= ip %> +<% @lookup.each_address do |ip| %> +<%= "#{ip}\n" %> <% end %> diff --git a/templates/compact.slack.erb b/templates/compact.slack.erb index a991275..dd55025 100644 --- a/templates/compact.slack.erb +++ b/templates/compact.slack.erb @@ -1,3 +1,3 @@ -<% @ips.each do |ip| %> +<% @lookup.each_address do |ip| %> `<%= ip %>` <% end %> From f38cee5036eb8d5f6dac31c48765a7d9be7f9eea Mon Sep 17 00:00:00 2001 From: Gabriel Mazetto Date: Sat, 22 Aug 2015 10:38:39 -0300 Subject: [PATCH 3/3] Added better .rspec defaults --- .rspec | 1 + 1 file changed, 1 insertion(+) create mode 100644 .rspec diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..02f19df --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--color --profile 5