dynport_tools 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -6,6 +6,7 @@ source "http://rubygems.org"
6
6
  # Add dependencies to develop your gem here.
7
7
  # Include everything needed to run rake, tests, features, etc.
8
8
  gem 'nokogiri'
9
+ gem 'redis'
9
10
 
10
11
  group :development do
11
12
  gem "rspec", "~> 2.3.0"
data/Gemfile.lock CHANGED
@@ -18,6 +18,7 @@ GEM
18
18
  rake (0.9.2)
19
19
  rbx-require-relative (0.0.5)
20
20
  rcov (0.9.9)
21
+ redis (2.2.1)
21
22
  rspec (2.3.0)
22
23
  rspec-core (~> 2.3.0)
23
24
  rspec-expectations (~> 2.3.0)
@@ -43,5 +44,6 @@ DEPENDENCIES
43
44
  jeweler (~> 1.6.4)
44
45
  nokogiri
45
46
  rcov
47
+ redis
46
48
  rspec (~> 2.3.0)
47
49
  ruby-debug
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.2.0
data/bin/redis_dumper ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require "dynport_tools/redis_dumper"
3
+
4
+ DynportTools::RedisDumper.new.run_from_args(ARGV)
data/bin/xmldiff CHANGED
@@ -1,12 +1,12 @@
1
- require "dynport_tools"
1
+ #!/usr/bin/env ruby
2
2
  require "dynport_tools/differ"
3
- require "xml_file"
3
+ require "dynport_tools/xml_file"
4
4
 
5
5
  file_a, file_b = ARGV[0,2]
6
6
 
7
7
  if file_a && File.exists?(file_a) && file_b && File.exists?(file_b)
8
- a = XmlFile.new(file_a)
9
- b = XmlFile.new(file_b)
8
+ a = DynportTools::XmlFile.new(file_a)
9
+ b = DynportTools::XmlFile.new(file_b)
10
10
  differ = DynportTools::Differ.new
11
11
  diff = differ.diff(a.nodes_hash, b.nodes_hash)
12
12
  puts differ.diff_to_message_lines(diff)
@@ -5,15 +5,14 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dynport_tools}
8
- s.version = "0.1.3"
8
+ s.version = "0.2.0"
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"]
12
- s.date = %q{2011-08-15}
13
- s.default_executable = %q{xmldiff}
12
+ s.date = %q{2011-08-16}
14
13
  s.description = %q{Collection of various tools}
15
14
  s.email = %q{tobias.schwab@dynport.de}
16
- s.executables = ["xmldiff"]
15
+ s.executables = ["xmldiff", "redis_dumper"]
17
16
  s.extra_rdoc_files = [
18
17
  "LICENSE.txt",
19
18
  "README.rdoc"
@@ -28,17 +27,21 @@ Gem::Specification.new do |s|
28
27
  "Rakefile",
29
28
  "VERSION",
30
29
  "autotest/discover.rb",
30
+ "bin/redis_dumper",
31
31
  "bin/xmldiff",
32
32
  "dynport_tools.gemspec",
33
33
  "lib/dynport_tools.rb",
34
34
  "lib/dynport_tools/differ.rb",
35
+ "lib/dynport_tools/redis_dumper.rb",
36
+ "lib/dynport_tools/xml_file.rb",
35
37
  "lib/have_attributes.rb",
36
- "lib/xml_file.rb",
37
38
  "spec/dynport_tools/differ_spec.rb",
39
+ "spec/dynport_tools/redis_dumper_spec.rb",
40
+ "spec/dynport_tools/xml_file_spec.rb",
38
41
  "spec/dynport_tools_spec.rb",
39
42
  "spec/fixtures/file_a.xml",
40
43
  "spec/spec_helper.rb",
41
- "spec/xml_file_spec.rb"
44
+ "spec/xml_diff_spec.rb"
42
45
  ]
43
46
  s.homepage = %q{http://github.com/tobstarr/dynport_tools}
44
47
  s.licenses = ["MIT"]
@@ -51,6 +54,7 @@ Gem::Specification.new do |s|
51
54
 
52
55
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
56
  s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
57
+ s.add_runtime_dependency(%q<redis>, [">= 0"])
54
58
  s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
55
59
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
56
60
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
@@ -61,6 +65,7 @@ Gem::Specification.new do |s|
61
65
  s.add_development_dependency(%q<ruby-debug>, [">= 0"])
62
66
  else
63
67
  s.add_dependency(%q<nokogiri>, [">= 0"])
68
+ s.add_dependency(%q<redis>, [">= 0"])
64
69
  s.add_dependency(%q<rspec>, ["~> 2.3.0"])
65
70
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
66
71
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
@@ -72,6 +77,7 @@ Gem::Specification.new do |s|
72
77
  end
73
78
  else
74
79
  s.add_dependency(%q<nokogiri>, [">= 0"])
80
+ s.add_dependency(%q<redis>, [">= 0"])
75
81
  s.add_dependency(%q<rspec>, ["~> 2.3.0"])
76
82
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
77
83
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
@@ -0,0 +1,55 @@
1
+ require "dynport_tools"
2
+ require "redis"
3
+
4
+ class DynportTools::RedisDumper
5
+ attr_accessor :redis
6
+
7
+ def initialize(redis = nil)
8
+ self.redis = redis
9
+ end
10
+
11
+ def zset_to_hash(key, window = 10_000)
12
+ hash = {}
13
+ offset = 0
14
+ while true
15
+ values = redis.zrevrange(key, offset, offset + (window - 1), :with_scores => true)
16
+ current_key = nil
17
+ values.each do |value|
18
+ if current_key.nil?
19
+ current_key = value
20
+ else
21
+ hash[current_key] = value
22
+ current_key = nil
23
+ end
24
+ end
25
+ offset += window
26
+ break if values.length < window * 2
27
+ end
28
+ hash
29
+ end
30
+
31
+ def dump_hash(hash)
32
+ hash.each do |key, value|
33
+ puts [key, value].join("\t")
34
+ end
35
+ end
36
+
37
+ def run_from_args(args)
38
+ host, port, key = args
39
+ if key
40
+ self.redis = Redis.new(:host => host, :port => port)
41
+ key_type = redis.type(key)
42
+ if key_type == "zset"
43
+ dump_hash(zset_to_hash(key))
44
+ else
45
+ puts "only zsets are supported for now"
46
+ end
47
+ else
48
+ print_usage
49
+ end
50
+ end
51
+
52
+ def print_usage
53
+ puts "USAGE: redis_dumper <redis_host> <redis_port> <key>"
54
+ end
55
+ end
@@ -1,6 +1,6 @@
1
1
  require "nokogiri"
2
2
 
3
- class XmlFile
3
+ class DynportTools::XmlFile
4
4
  attr_accessor :path
5
5
 
6
6
  def initialize(path)
data/lib/dynport_tools.rb CHANGED
@@ -1,2 +1,7 @@
1
1
  module DynportTools
2
+ class << self
3
+ def xmldiff(file1, file2)
4
+ throw :print_usage
5
+ end
6
+ end
2
7
  end
@@ -0,0 +1,70 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require "dynport_tools/redis_dumper"
3
+
4
+ describe "DynportTools::RedisDumper" do
5
+ let(:redis) { double("redis", :type => "zset") }
6
+ let(:dumper) { DynportTools::RedisDumper.new(redis) }
7
+
8
+ describe "#initialize" do
9
+ it "can be initialized with redis" do
10
+ redis = double("redis")
11
+ dumper = DynportTools::RedisDumper.new(redis)
12
+ dumper.redis.should == redis
13
+ end
14
+ end
15
+
16
+ describe "#zset_to_hash" do
17
+ it "calls zrevrange the correct number of times" do
18
+ redis.should_receive(:zrevrange).with("redis_key", 0, 1, :with_scores => true).and_return(["1", "2", "2", "4"])
19
+ redis.should_receive(:zrevrange).with("redis_key", 2, 3, :with_scores => true).and_return(["3", "2"])
20
+ dumper.zset_to_hash("redis_key", 2).should == { "1" => "2", "2" => "4", "3" => "2" }
21
+ end
22
+
23
+ it "only calls once when window is big enough" do
24
+ redis.should_receive(:zrevrange).with("redis_key", 0, 9999, :with_scores => true).and_return(["1", "2", "2", "4"])
25
+ dumper.zset_to_hash("redis_key").should == { "1" => "2", "2" => "4" }
26
+ end
27
+ end
28
+
29
+ describe "#dump_hash" do
30
+ it "calls puts the correct lines" do
31
+ dumper.should_receive(:puts).with("a\t1")
32
+ dumper.should_receive(:puts).with("b\t3")
33
+ dumper.dump_hash("a" => "1", "b" => "3")
34
+ end
35
+ end
36
+
37
+ describe "#run_from_args" do
38
+ before(:each) do
39
+ Redis.stub!(:new).and_return redis
40
+ dumper.stub(:zset_to_hash).and_return({})
41
+ dumper.stub(:dump_hash).and_return true
42
+ end
43
+
44
+ it "initializes a new redis instance" do
45
+ Redis.should_receive(:new).with(:host => "host", :port => "port").and_return redis
46
+ dumper.redis = nil
47
+ dumper.run_from_args(["host", "port", "key"])
48
+ dumper.redis.should == redis
49
+ end
50
+
51
+ it "calls dump_hash with zset_to_hash when type is zset" do
52
+ redis.should_receive(:type).with("key").and_return "zset"
53
+ hash = { "a" => 1 }
54
+ dumper.should_receive(:zset_to_hash).and_return(hash)
55
+ dumper.should_receive(:dump_hash).with(hash)
56
+ dumper.run_from_args(["host", "port", "key"])
57
+ end
58
+
59
+ it "prints a message when type is not zset" do
60
+ redis.should_receive(:type).with("key").and_return "hash"
61
+ dumper.should_receive(:puts).with("only zsets are supported for now")
62
+ dumper.run_from_args(["host", "port", "key"])
63
+ end
64
+
65
+ it "calls print_usage when not enough parameters" do
66
+ dumper.should_receive(:print_usage)
67
+ dumper.run_from_args(["host", "port"])
68
+ end
69
+ end
70
+ end
@@ -1,16 +1,16 @@
1
1
  require 'spec_helper'
2
- require "xml_file"
2
+ require "dynport_tools/xml_file"
3
3
 
4
4
  FILE1 = ROOT.join("spec/fixtures/file_a.xml")
5
5
  FILE2 = ROOT.join("spec/fixtures/file_b.xml")
6
6
 
7
7
 
8
- describe XmlFile do
9
- let(:file) { XmlFile.new(FILE1) }
8
+ describe "DynportTools::XmlFile" do
9
+ let(:file) { DynportTools::XmlFile.new(FILE1) }
10
10
 
11
11
  describe "#initialize" do
12
12
  it "sets the correct path" do
13
- XmlFile.new(FILE1).path.should == FILE1.to_s
13
+ DynportTools::XmlFile.new(FILE1).path.should == FILE1.to_s
14
14
  end
15
15
  end
16
16
 
@@ -1,4 +1,12 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "DynportTools" do
4
+ describe "#xmldiff" do
5
+ it "raises print usage when first file does not exist" do
6
+ catch(:print_usage) do
7
+ DynportTools.xmldiff(nil, nil)
8
+ violated "print_usage should been thrown"
9
+ end
10
+ end
11
+ end
4
12
  end
@@ -0,0 +1,4 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "XmlDiff" do
4
+ end
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: 29
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 3
10
- version: 0.1.3
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tobias Schwab
@@ -15,8 +15,8 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-15 00:00:00 +02:00
19
- default_executable: xmldiff
18
+ date: 2011-08-16 00:00:00 +02:00
19
+ default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: nokogiri
@@ -33,9 +33,23 @@ dependencies:
33
33
  type: :runtime
34
34
  requirement: *id001
35
35
  - !ruby/object:Gem::Dependency
36
- name: rspec
36
+ name: redis
37
37
  prerelease: false
38
38
  version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ requirement: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ prerelease: false
52
+ version_requirements: &id003 !ruby/object:Gem::Requirement
39
53
  none: false
40
54
  requirements:
41
55
  - - ~>
@@ -47,11 +61,11 @@ dependencies:
47
61
  - 0
48
62
  version: 2.3.0
49
63
  type: :development
50
- requirement: *id002
64
+ requirement: *id003
51
65
  - !ruby/object:Gem::Dependency
52
66
  name: bundler
53
67
  prerelease: false
54
- version_requirements: &id003 !ruby/object:Gem::Requirement
68
+ version_requirements: &id004 !ruby/object:Gem::Requirement
55
69
  none: false
56
70
  requirements:
57
71
  - - ~>
@@ -63,11 +77,11 @@ dependencies:
63
77
  - 0
64
78
  version: 1.0.0
65
79
  type: :development
66
- requirement: *id003
80
+ requirement: *id004
67
81
  - !ruby/object:Gem::Dependency
68
82
  name: jeweler
69
83
  prerelease: false
70
- version_requirements: &id004 !ruby/object:Gem::Requirement
84
+ version_requirements: &id005 !ruby/object:Gem::Requirement
71
85
  none: false
72
86
  requirements:
73
87
  - - ~>
@@ -79,11 +93,11 @@ dependencies:
79
93
  - 4
80
94
  version: 1.6.4
81
95
  type: :development
82
- requirement: *id004
96
+ requirement: *id005
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rcov
85
99
  prerelease: false
86
- version_requirements: &id005 !ruby/object:Gem::Requirement
100
+ version_requirements: &id006 !ruby/object:Gem::Requirement
87
101
  none: false
88
102
  requirements:
89
103
  - - ">="
@@ -93,11 +107,11 @@ dependencies:
93
107
  - 0
94
108
  version: "0"
95
109
  type: :development
96
- requirement: *id005
110
+ requirement: *id006
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: ZenTest
99
113
  prerelease: false
100
- version_requirements: &id006 !ruby/object:Gem::Requirement
114
+ version_requirements: &id007 !ruby/object:Gem::Requirement
101
115
  none: false
102
116
  requirements:
103
117
  - - "="
@@ -109,11 +123,11 @@ dependencies:
109
123
  - 0
110
124
  version: 4.5.0
111
125
  type: :development
112
- requirement: *id006
126
+ requirement: *id007
113
127
  - !ruby/object:Gem::Dependency
114
128
  name: autotest
115
129
  prerelease: false
116
- version_requirements: &id007 !ruby/object:Gem::Requirement
130
+ version_requirements: &id008 !ruby/object:Gem::Requirement
117
131
  none: false
118
132
  requirements:
119
133
  - - ">="
@@ -123,11 +137,11 @@ dependencies:
123
137
  - 0
124
138
  version: "0"
125
139
  type: :development
126
- requirement: *id007
140
+ requirement: *id008
127
141
  - !ruby/object:Gem::Dependency
128
142
  name: autotest-growl
129
143
  prerelease: false
130
- version_requirements: &id008 !ruby/object:Gem::Requirement
144
+ version_requirements: &id009 !ruby/object:Gem::Requirement
131
145
  none: false
132
146
  requirements:
133
147
  - - ">="
@@ -137,11 +151,11 @@ dependencies:
137
151
  - 0
138
152
  version: "0"
139
153
  type: :development
140
- requirement: *id008
154
+ requirement: *id009
141
155
  - !ruby/object:Gem::Dependency
142
156
  name: ruby-debug
143
157
  prerelease: false
144
- version_requirements: &id009 !ruby/object:Gem::Requirement
158
+ version_requirements: &id010 !ruby/object:Gem::Requirement
145
159
  none: false
146
160
  requirements:
147
161
  - - ">="
@@ -151,11 +165,12 @@ dependencies:
151
165
  - 0
152
166
  version: "0"
153
167
  type: :development
154
- requirement: *id009
168
+ requirement: *id010
155
169
  description: Collection of various tools
156
170
  email: tobias.schwab@dynport.de
157
171
  executables:
158
172
  - xmldiff
173
+ - redis_dumper
159
174
  extensions: []
160
175
 
161
176
  extra_rdoc_files:
@@ -171,17 +186,21 @@ files:
171
186
  - Rakefile
172
187
  - VERSION
173
188
  - autotest/discover.rb
189
+ - bin/redis_dumper
174
190
  - bin/xmldiff
175
191
  - dynport_tools.gemspec
176
192
  - lib/dynport_tools.rb
177
193
  - lib/dynport_tools/differ.rb
194
+ - lib/dynport_tools/redis_dumper.rb
195
+ - lib/dynport_tools/xml_file.rb
178
196
  - lib/have_attributes.rb
179
- - lib/xml_file.rb
180
197
  - spec/dynport_tools/differ_spec.rb
198
+ - spec/dynport_tools/redis_dumper_spec.rb
199
+ - spec/dynport_tools/xml_file_spec.rb
181
200
  - spec/dynport_tools_spec.rb
182
201
  - spec/fixtures/file_a.xml
183
202
  - spec/spec_helper.rb
184
- - spec/xml_file_spec.rb
203
+ - spec/xml_diff_spec.rb
185
204
  has_rdoc: true
186
205
  homepage: http://github.com/tobstarr/dynport_tools
187
206
  licenses: