dynport_tools 0.2.9 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.9
1
+ 0.2.10
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dynport_tools}
8
- s.version = "0.2.9"
8
+ s.version = "0.2.10"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tobias Schwab"]
@@ -33,6 +33,7 @@ Gem::Specification.new do |s|
33
33
  "bin/xmldiff",
34
34
  "dynport_tools.gemspec",
35
35
  "lib/dynport_tools.rb",
36
+ "lib/dynport_tools/ascii_table.rb",
36
37
  "lib/dynport_tools/deep_merger.rb",
37
38
  "lib/dynport_tools/differ.rb",
38
39
  "lib/dynport_tools/eta.rb",
@@ -41,6 +42,7 @@ Gem::Specification.new do |s|
41
42
  "lib/dynport_tools/redis_dumper.rb",
42
43
  "lib/dynport_tools/redis_q.rb",
43
44
  "lib/dynport_tools/xml_file.rb",
45
+ "spec/dynport_tools/ascii_table_spec.rb",
44
46
  "spec/dynport_tools/deep_merger_spec.rb",
45
47
  "spec/dynport_tools/differ_spec.rb",
46
48
  "spec/dynport_tools/eta_spec.rb",
data/lib/dynport_tools.rb CHANGED
@@ -6,4 +6,4 @@ require "typhoeus"
6
6
  require "nokogiri"
7
7
  require "cgi"
8
8
 
9
- %w(deep_merger differ jenkins redis_dumper xml_file have_attributes redis_q eta).map { |m| require "dynport_tools/#{m}" }
9
+ %w(deep_merger differ jenkins redis_dumper xml_file have_attributes redis_q eta ascii_table).map { |m| require "dynport_tools/#{m}" }
@@ -0,0 +1,44 @@
1
+ require "tempfile"
2
+
3
+ class DynportTools::AsciiTable
4
+ attr_accessor :headers
5
+ attr_accessor :rows
6
+
7
+ def initialize(attributes = {})
8
+ self.headers = attributes[:headers] || []
9
+ self.rows = attributes[:rows] || []
10
+ end
11
+
12
+ def to_tsv
13
+ ([headers] + rows).map { |line| line.join("\t") }.join("\n")
14
+ end
15
+
16
+ def to_html
17
+ html = "<table border=1 align=center>"
18
+ html << "<tr>" + headers.map { |header| html_table_cell(header, "th") }.join("") if headers.any?
19
+ html << rows.map { |row| "<tr>" + row.map { |r| html_table_cell(r, "td") }.join("") }.join("")
20
+ html + "</table>"
21
+ end
22
+
23
+ def html_table_cell(text_or_array, node = "td")
24
+ text, options = text_or_array
25
+ "<#{node}#{options ? options.map { |key, value| " #{key}=#{value}" }.join("") : ""}>#{text}"
26
+ end
27
+
28
+ def to_ascii
29
+ html2ascii(to_html)
30
+ end
31
+
32
+ def to(format)
33
+ send(:"to_#{format}")
34
+ end
35
+
36
+ def html2ascii(html)
37
+ tempfile = Tempfile.new("html2ascii")
38
+ tempfile.print(html)
39
+ tempfile.close
40
+ ascii = Kernel.send(:`, "links -dump #{tempfile.path}")
41
+ tempfile.delete
42
+ ascii
43
+ end
44
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe DynportTools::AsciiTable do
4
+ let(:tempfile) { double("tmpfile").as_null_object }
5
+
6
+ before(:each) do
7
+ Tempfile.stub(:new).and_return tempfile
8
+ Kernel.stub!(:`).and_return ""
9
+ end
10
+
11
+ describe "#to_tsv" do
12
+ it "returns the correct tsv" do
13
+ DynportTools::AsciiTable.new(:headers => %w(A B), :rows => [%w(1 2), %w(3 4)]).to_tsv.should == "A\tB\n1\t2\n3\t4"
14
+ end
15
+ end
16
+
17
+ describe "#to_html" do
18
+ it "returns the correct html" do
19
+ result = "<table border=1 align=center><tr><th>A<th>B<tr><td>1<td>2<tr><td>3<td>4</table>"
20
+ DynportTools::AsciiTable.new(:headers => %w(A B), :rows => [%w(1 2), %w(3 4)]).to_html.should == result
21
+ end
22
+
23
+ it "does not include a header when not set" do
24
+ DynportTools::AsciiTable.new(:rows => [%w(1 2), %w(3 4)]).to_html.should_not include("th")
25
+ end
26
+
27
+ it "uses options for headers" do
28
+ DynportTools::AsciiTable.new(:headers => [["A", {:colspan => 1}], "B"]).to_html.should include("<tr><th colspan=1>A<th>B")
29
+ end
30
+
31
+ it "uses options for rows" do
32
+ DynportTools::AsciiTable.new(:rows => [[["A", {:colspan => 1}], "B"]]).to_html.should include("<tr><td colspan=1>A<td>B")
33
+ end
34
+ end
35
+
36
+ describe "#to_ascii" do
37
+ it "calls html2ascii with response of to_html" do
38
+ table = DynportTools::AsciiTable.new
39
+ html = double("html")
40
+ table.should_receive(:to_html).and_return html
41
+ ascii = double("ascii")
42
+ table.should_receive(:html2ascii).with(html).and_return ascii
43
+ table.to_ascii.should == ascii
44
+ end
45
+ end
46
+
47
+ describe "#html2ascii" do
48
+ it "writes the html to a tempfile, closes and deletes it" do
49
+ Tempfile.should_receive(:new).with("html2ascii").and_return tempfile
50
+ tempfile.should_receive(:print).with("text")
51
+ tempfile.should_receive(:close)
52
+ tempfile.should_receive(:delete)
53
+ DynportTools::AsciiTable.new.html2ascii("text")
54
+ end
55
+
56
+ it "calls links with correct params" do
57
+ tempfile.should_receive(:path).and_return "/some/path"
58
+ Kernel.should_receive(:`).with("links -dump /some/path").and_return "result"
59
+ DynportTools::AsciiTable.new.html2ascii("test")
60
+ end
61
+ end
62
+
63
+ describe "#to" do
64
+ { "html" => :to_html, :html => :to_html, "ascii" => :to_ascii }.each do |from, to|
65
+ it "calls #{to} for #{from}" do
66
+ table = DynportTools::AsciiTable.new
67
+ table.should_receive(to).and_return ""
68
+ table.to(from)
69
+ end
70
+ end
71
+ end
72
+ end
@@ -1,7 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- require "dynport_tools/jenkins"
4
-
5
3
  describe "DynportTools::Jenkins" do
6
4
  let(:url) { "http://some.url.com:8098" }
7
5
  let(:jenkins) { DynportTools::Jenkins.new(url) }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynport_tools
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 9
10
- version: 0.2.9
9
+ - 10
10
+ version: 0.2.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tobias Schwab
@@ -220,6 +220,7 @@ files:
220
220
  - bin/xmldiff
221
221
  - dynport_tools.gemspec
222
222
  - lib/dynport_tools.rb
223
+ - lib/dynport_tools/ascii_table.rb
223
224
  - lib/dynport_tools/deep_merger.rb
224
225
  - lib/dynport_tools/differ.rb
225
226
  - lib/dynport_tools/eta.rb
@@ -228,6 +229,7 @@ files:
228
229
  - lib/dynport_tools/redis_dumper.rb
229
230
  - lib/dynport_tools/redis_q.rb
230
231
  - lib/dynport_tools/xml_file.rb
232
+ - spec/dynport_tools/ascii_table_spec.rb
231
233
  - spec/dynport_tools/deep_merger_spec.rb
232
234
  - spec/dynport_tools/differ_spec.rb
233
235
  - spec/dynport_tools/eta_spec.rb