burp 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +28 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +59 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/lib/burp.rb +31 -0
- data/spec/burp_spec.rb +40 -0
- data/spec/spec_helper.rb +12 -0
- metadata +134 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec", "~> 2.3.0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.5.2"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.5.2)
|
7
|
+
bundler (~> 1.0.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rake (0.8.7)
|
11
|
+
rcov (0.9.9)
|
12
|
+
rspec (2.3.0)
|
13
|
+
rspec-core (~> 2.3.0)
|
14
|
+
rspec-expectations (~> 2.3.0)
|
15
|
+
rspec-mocks (~> 2.3.0)
|
16
|
+
rspec-core (2.3.1)
|
17
|
+
rspec-expectations (2.3.0)
|
18
|
+
diff-lcs (~> 1.1.2)
|
19
|
+
rspec-mocks (2.3.0)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
bundler (~> 1.0.0)
|
26
|
+
jeweler (~> 1.5.2)
|
27
|
+
rcov
|
28
|
+
rspec (~> 2.3.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Dave Martin
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
= burp
|
2
|
+
|
3
|
+
Oh yum, look at this collection of hashes I have to manage. Each with a unique id to represent it:
|
4
|
+
|
5
|
+
yummy = [
|
6
|
+
{:id => :a, :data => "A"},
|
7
|
+
{:id => :b, :data => "B"},
|
8
|
+
{:id => :c, :data => "C"}
|
9
|
+
]
|
10
|
+
|
11
|
+
I feel like checking out :b today:
|
12
|
+
|
13
|
+
Yes, you could just select what you need the boring, ugly way
|
14
|
+
b_data = yummy.select{|node| node[:id] == :b}.first
|
15
|
+
That's not hard, but rather than manually select it, let's just let out a hearty burp
|
16
|
+
|
17
|
+
burped = Burp.new(yummy, :id)
|
18
|
+
|
19
|
+
and get good old fashioned hash in return
|
20
|
+
|
21
|
+
#=> { :a => {:id => :a, :data => "A" },
|
22
|
+
:b => {:id => :b, :data => "B" },
|
23
|
+
:c => {:id => :c, :data => "C" }
|
24
|
+
|
25
|
+
meaning that
|
26
|
+
b_data = burped[:b]
|
27
|
+
|
28
|
+
As a special side-dish, Burp can handle things that are a bit messier as well
|
29
|
+
|
30
|
+
fly_in_my_soup = [
|
31
|
+
{:id => :a, :data => "A"},
|
32
|
+
{:id => :b, :data => "B"},
|
33
|
+
{:id => :c, :data => "C"},
|
34
|
+
{:fly => :d, :data => "D"}
|
35
|
+
]
|
36
|
+
|
37
|
+
yummy_soup = Burped.new(fly_in_my_soup)
|
38
|
+
#=> { :a => {:id => :a, :data => "A" },
|
39
|
+
:b => {:id => :b, :data => "B" },
|
40
|
+
:c => {:id => :c, :data => "C" }
|
41
|
+
|
42
|
+
fly = yummy_soup.left_overs
|
43
|
+
#=> [{:fly => :d, :data => "D"}]
|
44
|
+
|
45
|
+
== Contributing to burp
|
46
|
+
|
47
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
48
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
49
|
+
* Fork the project
|
50
|
+
* Start a feature/bugfix branch
|
51
|
+
* Commit and push until you are happy with your contribution
|
52
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
53
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
54
|
+
|
55
|
+
== Copyright
|
56
|
+
|
57
|
+
Copyright (c) 2011 Dave Martin. See LICENSE.txt for
|
58
|
+
further details.
|
59
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "burp"
|
16
|
+
gem.homepage = "http://github.com/forforf/burp"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Turns an array of hashes into a hash of hashes}
|
19
|
+
gem.description = %Q{Tell it which hash key in the array of hashes to use as the hash id, and voila a hash is made from your array}
|
20
|
+
gem.email = "dmarti21@gmail.com"
|
21
|
+
gem.authors = ["Dave M"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
25
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rspec/core'
|
30
|
+
require 'rspec/core/rake_task'
|
31
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
32
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
33
|
+
end
|
34
|
+
|
35
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
36
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
37
|
+
spec.rcov = true
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
require 'rake/rdoctask'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "burp #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/burp.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
class Burp < Hash
|
2
|
+
attr_reader :left_overs
|
3
|
+
#Converts an array of hashes into a hash
|
4
|
+
#It takes as arguments,the array of hashes (each hash representing node data)
|
5
|
+
#and a field in the hash (node data) to use as the key for the returned hash
|
6
|
+
def initialize(node_list, id)
|
7
|
+
@left_overs = []
|
8
|
+
node_list.map do |node|
|
9
|
+
#better error information and eliminate nil keys
|
10
|
+
node = validate(node, id)
|
11
|
+
#it's left over if the primary hash key doesn't exist in the node
|
12
|
+
add_leftovers(self, @left_overs, node) unless ( node[id] && node.keys.include?(id) )
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate(node, id)
|
17
|
+
begin
|
18
|
+
self[node[id]] = node
|
19
|
+
rescue TypeError #replacing an unhelpful error with something more helpful
|
20
|
+
raise ArgumentError, "Can't use #{id.inspect} as a valid key in #{node.inspect}. "\
|
21
|
+
"Maybe array elements are not hashes?"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def add_leftovers(main_hash, left_overs, node)
|
26
|
+
#remove nil hash from main_hash
|
27
|
+
main_hash.delete(nil)
|
28
|
+
#add data to left overs (excluding nil data)
|
29
|
+
left_overs << node if node
|
30
|
+
end
|
31
|
+
end
|
data/spec/burp_spec.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
|
4
|
+
describe "Burp" do
|
5
|
+
before(:each) do
|
6
|
+
@nice_array = [{:id => :a, :data => "A"},
|
7
|
+
{:id => :b, :data => "B"},
|
8
|
+
{:id => :c, :data => "C"}]
|
9
|
+
@nice_burped = {:a => {:id => :a, :data => "A"},
|
10
|
+
:b => {:id => :b, :data => "B"},
|
11
|
+
:c => {:id => :c, :data => "C"}}
|
12
|
+
|
13
|
+
@not_an_array = @nice_burped #its a hash now
|
14
|
+
|
15
|
+
#notice the last id typo
|
16
|
+
@dirty_array = [{:id => :a, :data => "A"},
|
17
|
+
{:id => :b, :data => "B"},
|
18
|
+
{:id => :c, :data => "C"},
|
19
|
+
{:iidd => :d, :data => "D"}]
|
20
|
+
|
21
|
+
@dirty_burped = @nice_burped
|
22
|
+
|
23
|
+
@dirty_left_overs = [{:iidd => :d, :data => "D"}]
|
24
|
+
end
|
25
|
+
|
26
|
+
it "makes a hash out of a nicely formed array" do
|
27
|
+
my_burp = Burp.new(@nice_array, :id)
|
28
|
+
my_burp.should == @nice_burped
|
29
|
+
end
|
30
|
+
|
31
|
+
it "raises an informative error message when it panics" do
|
32
|
+
expect { Burp.new(@not_an_array) }.to raise_error(ArgumentError)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "handles a dirty hash" do
|
36
|
+
my_dirty_burp = Burp.new(@dirty_array, :id)
|
37
|
+
my_dirty_burp.should == @dirty_burped
|
38
|
+
my_dirty_burp.left_overs.should == @dirty_left_overs
|
39
|
+
end
|
40
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'burp'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: burp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Dave M
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-04-12 00:00:00 +00:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 2
|
29
|
+
- 3
|
30
|
+
- 0
|
31
|
+
version: 2.3.0
|
32
|
+
type: :development
|
33
|
+
prerelease: false
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: bundler
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 1
|
44
|
+
- 0
|
45
|
+
- 0
|
46
|
+
version: 1.0.0
|
47
|
+
type: :development
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: jeweler
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ~>
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 1
|
59
|
+
- 5
|
60
|
+
- 2
|
61
|
+
version: 1.5.2
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: rcov
|
67
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
type: :development
|
76
|
+
prerelease: false
|
77
|
+
version_requirements: *id004
|
78
|
+
description: Tell it which hash key in the array of hashes to use as the hash id, and voila a hash is made from your array
|
79
|
+
email: dmarti21@gmail.com
|
80
|
+
executables: []
|
81
|
+
|
82
|
+
extensions: []
|
83
|
+
|
84
|
+
extra_rdoc_files:
|
85
|
+
- LICENSE.txt
|
86
|
+
- README.rdoc
|
87
|
+
files:
|
88
|
+
- .document
|
89
|
+
- .rspec
|
90
|
+
- Gemfile
|
91
|
+
- Gemfile.lock
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.rdoc
|
94
|
+
- Rakefile
|
95
|
+
- VERSION
|
96
|
+
- lib/burp.rb
|
97
|
+
- spec/burp_spec.rb
|
98
|
+
- spec/spec_helper.rb
|
99
|
+
has_rdoc: true
|
100
|
+
homepage: http://github.com/forforf/burp
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
hash: 771572209
|
114
|
+
segments:
|
115
|
+
- 0
|
116
|
+
version: "0"
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
none: false
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
segments:
|
123
|
+
- 0
|
124
|
+
version: "0"
|
125
|
+
requirements: []
|
126
|
+
|
127
|
+
rubyforge_project:
|
128
|
+
rubygems_version: 1.3.7
|
129
|
+
signing_key:
|
130
|
+
specification_version: 3
|
131
|
+
summary: Turns an array of hashes into a hash of hashes
|
132
|
+
test_files:
|
133
|
+
- spec/burp_spec.rb
|
134
|
+
- spec/spec_helper.rb
|