crackr 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2012 Hakan Ensari
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,24 @@
1
+ # Crackr
2
+
3
+ [![travis] [1]] [2]
4
+
5
+ Really simple XML parsing with Nokogiri.
6
+
7
+ ```ruby
8
+ require 'crackr'
9
+
10
+ str = '<?xml version="1.0" encoding="UTF-8"?>' +
11
+ '<Book>' +
12
+ '<ISBN>9780816614028</ISBN>' +
13
+ '<Creator Role="Author">Gilles Deleuze</Creator>' +
14
+ '<Creator Role="Contributor">Felix Guattari</Creator>' +
15
+ '<Title>Thousand Plateaus</Title>' +
16
+ '</Book>'
17
+
18
+ hsh = Crackr::XML.parse str
19
+ p hsh['Book']['ISBN'] # => '9780816614028'
20
+ p hsh['Book']['Creator'].map { |c| c['Role'] } # => ['Author', 'Contributor']
21
+ ```
22
+
23
+ [1]: https://secure.travis-ci.org/hakanensari/crackr.png
24
+ [2]: http://travis-ci.org/hakanensari/crackr
@@ -0,0 +1 @@
1
+ require 'crackr/xml'
@@ -0,0 +1,3 @@
1
+ module Crackr
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,52 @@
1
+ require 'nokogiri'
2
+
3
+ # Really simple XML parsing.
4
+ module Crackr
5
+ module XML
6
+ # Builds a hash from an XML document.
7
+ #
8
+ # @param [Nokogiri::XML::Document, Nokogiri::XML::Element, String] xml A
9
+ # Nokogiri XML document, an element thereof, or a string representation of
10
+ # an XML document.
11
+ # @return [Hash]
12
+ def self.parse(xml)
13
+ case xml
14
+ when String
15
+ parse Nokogiri::XML(xml)
16
+ when Nokogiri::XML::Document
17
+ parse xml.root
18
+ when Nokogiri::XML::Element
19
+ hsh = {}
20
+
21
+ xml.attributes.each_pair do |key, attribute|
22
+ hsh[key] = attribute.value
23
+ end
24
+
25
+ xml.children.each do |child|
26
+ result = parse child
27
+
28
+ if child.name == 'text'
29
+ if hsh.empty?
30
+ return result
31
+ else
32
+ hsh['__content__'] = result
33
+ end
34
+ elsif hsh[child.name]
35
+ case hsh[child.name]
36
+ when Array
37
+ hsh[child.name] << result
38
+ else
39
+ hsh[child.name] = [hsh[child.name]] << result
40
+ end
41
+ else
42
+ hsh[child.name] = result
43
+ end
44
+ end
45
+
46
+ hsh
47
+ else
48
+ xml.content.to_s
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ require 'crackr/xml'
4
+
5
+ describe Crackr do
6
+ let(:str) do
7
+ <<-XML.gsub!(/>\s+</, '><').strip!
8
+ <?xml version=\"1.0\" ?>
9
+ <ItemAttributes>
10
+ <Title>Anti-Oedipus</Title>
11
+ <Author>Gilles Deleuze</Author>
12
+ <Author>Felix Guattari</Author>
13
+ <Creator Role="Translator">Robert Hurley</Creator>
14
+ </ItemAttributes>
15
+ XML
16
+ end
17
+
18
+ let(:xml) do
19
+ Nokogiri::XML str
20
+ end
21
+
22
+ describe '.parse' do
23
+ context "when given an XML document" do
24
+ it 'returns a hash' do
25
+ Crackr::XML.parse(xml).should be_an_instance_of Hash
26
+ end
27
+ end
28
+
29
+ context "when given a string representation of an XML document" do
30
+ it 'returns a hash' do
31
+ Crackr::XML.parse(str).should be_an_instance_of Hash
32
+ end
33
+ end
34
+
35
+ it 'handles an only child' do
36
+ Crackr::XML.parse(xml)['Title'].should eql 'Anti-Oedipus'
37
+ end
38
+
39
+ it 'handles arrays' do
40
+ Crackr::XML.parse(xml)['Author'].should be_a Array
41
+ end
42
+
43
+ it 'handles attributes' do
44
+ node = Crackr::XML.parse(xml)['Creator']
45
+ node['Role'].should eql 'Translator'
46
+ node['__content__'].should eql 'Robert Hurley'
47
+ end
48
+ end
49
+ end
@@ -0,0 +1 @@
1
+ require 'rspec'
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crackr
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hakan Ensari
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: &70132416322520 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.4'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70132416322520
25
+ description: Really simple XML parsing with Nokogiri.
26
+ email:
27
+ - hakan.ensari@papercavalier.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/crackr/version.rb
33
+ - lib/crackr/xml.rb
34
+ - lib/crackr.rb
35
+ - LICENSE
36
+ - README.md
37
+ - spec/crackr_spec.rb
38
+ - spec/spec_helper.rb
39
+ homepage: http://github.com/hakanensari/crackr
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.11
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Really simple XML parsing
63
+ test_files:
64
+ - spec/crackr_spec.rb
65
+ - spec/spec_helper.rb