Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First scratch of rexml implementation #46

Merged
merged 2 commits into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ rvm:
- 2.3.1
- 2.2.2
- 2.1.6
- 2.0.0

5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* [#36](https://github.com/soulcutter/saxerator/issues/36): Rubocop config (@fanantoxa)
* [#45](https://github.com/soulcutter/saxerator/issues/45): Drop support ruby 1.9.3 (@fanantoxa)
* [#2](https://github.com/soulcutter/saxerator/issues/2): Implement Ox support (@soulcutter)
* [#43](https://github.com/soulcutter/saxerator/issues/43): Update XmlBuilder to use REXML class (@fanantoxa)
* [#46](https://github.com/soulcutter/saxerator/pull/46): Bump ruby version requirement to 2.1+ (@soulcutter)

### Bug fixes

Expand All @@ -18,5 +20,6 @@
* [#33](https://github.com/soulcutter/saxerator/pull/33): Extract an adapter interface for supporting other parsers. (@soulcutter)
* [#38](https://github.com/soulcutter/saxerator/pull/38): Ox adapter. (@soulcutter)
* [#39](https://github.com/soulcutter/saxerator/pull/39): Added validation for output_type validation for difference parsers. (@fanantoxa)
* [#46](https://github.com/soulcutter/saxerator/pull/46): First scratch of rexml implementation. (@fanantoxa)

## 0.9.5 (2014-11-07)
## 0.9.5 (2014-11-07)
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ end
| Setting | Default | Values | Description
|:------------------|:--------|-----------------|------------
| `adapter` | `:nokogiri` | `:nokogiri`, `:ox` | The XML parser used by Saxerator |
| `output_type` | `:hash` | `:hash`, `:xml` | The type of object generated by Saxerator's parsing. `:hash` should be self-explanatory, `:xml` generates a `Nokogiri::XML::Document`
| `output_type` | `:hash` | `:hash`, `:xml` | The type of object generated by Saxerator's parsing. `:hash` should be self-explanatory, `:xml` generates a `REXML::Document`
| `symbolize_keys!` | n/a | n/a | Call this method if you want the hash keys to be symbols rather than strings
| `ignore_namespaces!`| n/a | n/a | Call this method if you want to treat the XML document as if it has no namespace information. It differs slightly from `strip_namespaces!` since it deals with how the XML is processed rather than how it is output
| `strip_namespaces!`| n/a | user-specified | Called with no arguments this strips all namespaces, or you may specify an arbitrary number of namespaces to strip, i.e. `config.strip_namespaces! :rss, :soapenv`
Expand Down Expand Up @@ -139,6 +139,10 @@ When I fetch a tag that has one or more elements, sometimes I get an `Array`, an
> occasionally comes up is for elements that are sometimes-empty. Empty elements behave mostly like an
> empty `Hash`, however you may convert it to a more `String`-like object via `#to_s`

### Contribution ###

For running tests for all parsers run `rake spec:adapters`

### Acknowledgements ###
Saxerator was inspired by - but not affiliated with - [nori](https://github.com/savonrb/nori) and [Gregory Brown](http://majesticseacreature.com/)'s
[Practicing Ruby](http://practicingruby.com/)
Expand Down
16 changes: 10 additions & 6 deletions lib/saxerator/builder/xml_builder.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'rexml/document'

module Saxerator
module Builder
class XmlBuilder
Expand All @@ -17,19 +19,21 @@ def add_node(node)
end

def to_xml(builder)
element = REXML::Element.new(name, nil, attribute_quote: :quote)
element.add_attributes(@attributes)
if @text
builder.send("#{name}_", @attributes, @children.join)
element.add_text(@children.join)
else
builder.send("#{name}_", @attributes) do |xml|
@children.each { |child| child.to_xml(xml) }
end
@children.each { |child| child.to_xml(element) }
end
builder.elements << element
end

def block_variable
builder = Nokogiri::XML::Builder.new
builder = REXML::Document.new
builder << REXML::XMLDecl.new('1.0')
to_xml(builder)
builder.doc
builder
end
end
end
Expand Down
13 changes: 4 additions & 9 deletions lib/saxerator/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ class Configuration
attr_writer :hash_key_generator
attr_reader :output_type

ALLOWED_OUTPUT_TYPES = {
nokogiri: [:hash, :xml],
ox: [:hash]
}.freeze
ALLOWED_OUTPUT_TYPES = [:ox, :nokogiri]

def initialize
@adapter = :nokogiri
Expand All @@ -16,7 +13,9 @@ def initialize
end

def adapter=(name)
raise ArgumentError.new("Unknown adapter '#{name.inspect}'") unless ALLOWED_OUTPUT_TYPES.has_key?(name)
unless ALLOWED_OUTPUT_TYPES.include?(name)
raise ArgumentError, "Unknown adapter '#{name.inspect}'"
end
@adapter = name
end

Expand All @@ -27,10 +26,6 @@ def adapter

def output_type=(val)
raise ArgumentError, "Unknown output_type '#{val.inspect}'" unless Builder.valid?(val)
unless ALLOWED_OUTPUT_TYPES[@adapter].include?(val)
raise ArgumentError, "Adapter '#{adapter.inspect}' not allow to use \
output_type '#{val.inspect}'"
end
@output_type = val
raise_error_if_using_put_attributes_in_hash_with_xml
end
Expand Down
2 changes: 1 addition & 1 deletion saxerator.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |s|

s.rubyforge_project = 'saxerator'

s.required_ruby_version = '>= 2.0.0'
s.required_ruby_version = '>= 2.1.0'

s.files = [
'LICENSE',
Expand Down
28 changes: 4 additions & 24 deletions spec/lib/builder/xml_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding: utf-8
require 'spec_helper'

describe 'Saxerator xml format', :nokogiri_only do
describe 'Saxerator xml format' do
let(:xml) { fixture_file('nested_elements.xml') }

subject(:entry) do
Expand All @@ -10,29 +10,9 @@
end.for_tag(:entry).first
end

it { is_expected.to be_a(Nokogiri::XML::Node) }
it { is_expected.to be_a(REXML::Document) }
it 'looks like the original document' do
expected_xml = <<-eos
<?xml version="1.0"?>
<entry>
<id>1</id>
<published>2012-01-01T16:17:00-06:00</published>
<updated>2012-01-01T16:17:00-06:00</updated>
<link href="https://example.com/blog/how-to-eat-an-airplane"/>
<title>How to eat an airplane</title>
<content type="html">&lt;p&gt;Airplanes are very large &#x2014; this can present difficulty in digestion.&lt;/p&gt;</content>
<media:thumbnail url="http://www.gravatar.com/avatar/a9eb6ba22e482b71b266daadf9c9a080?s=80"/>
<author>
<name>Soul&lt;utter</name>
</author>
<contributor type="primary">
<name>Jane Doe</name>
</contributor>
<contributor>
<name>Leviticus Alabaster</name>
</contributor>
</entry>
eos
expect(entry.to_xml).to eq(expected_xml)
expected_xml = '<?xml version=\'1.0\'?><entry><id>1</id><published>2012-01-01T16:17:00-06:00</published><updated>2012-01-01T16:17:00-06:00</updated><link href="https://example.com/blog/how-to-eat-an-airplane"/><title>How to eat an airplane</title><content type="html">&lt;p&gt;Airplanes are very large — this can present difficulty in digestion.&lt;/p&gt;</content><media:thumbnail url="http://www.gravatar.com/avatar/a9eb6ba22e482b71b266daadf9c9a080?s=80"/><author><name>Soul&lt;utter</name></author><contributor type="primary"><name>Jane Doe</name></contributor><contributor><name>Leviticus Alabaster</name></contributor></entry>'
expect(entry.to_s).to eq(expected_xml)
end
end
4 changes: 2 additions & 2 deletions spec/lib/saxerator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
specify { expect(parser.all).to eq('bar' => 'baz') }
end

context 'with config.output_type = :xml', :nokogiri_only do
context 'with config.output_type = :xml' do
let(:output_type) { :xml }
specify { expect(parser.all).to be_a Nokogiri::XML::Document }
specify { expect(parser.all).to be_a REXML::Document }
specify { expect(parser.all.to_s).to include '<bar foo="bar">' }
end

Expand Down