Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prepared the new public interface for version 2
changed the Nori module to a class, removed any global configuration, introduced simple options, removed the Nori::Parser module.
- Loading branch information
Showing
15 changed files
with
201 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,55 @@ | ||
require "nori/version" | ||
require "nori/core_ext" | ||
require "nori/parser" | ||
require "nori/xml_utility_node" | ||
|
||
module Nori | ||
extend self | ||
class Nori | ||
|
||
# Translates the given +xml+ to a Hash. Accepts an optional +parser+ to use. | ||
def parse(xml, parser = nil) | ||
return {} if xml.blank? | ||
Parser.parse xml, parser, self | ||
end | ||
PARSERS = { :rexml => "REXML", :nokogiri => "Nokogiri" } | ||
|
||
# Sets the +parser+ to use. | ||
def parser=(parser) | ||
Parser.use = parser | ||
end | ||
def initialize(globals = {}) | ||
defaults = { | ||
:strip_namespaces => false, | ||
:convert_tags_to => nil | ||
} | ||
|
||
# Yields +self+ for configuration. | ||
def configure | ||
yield self | ||
validate_options! defaults.keys, globals.keys | ||
@globals = defaults.merge(globals) | ||
end | ||
|
||
# Sets whether to use advanced typecasting. | ||
attr_writer :advanced_typecasting | ||
def parse(xml, locals = {}) | ||
validate_xml! xml | ||
|
||
# Returns whether to use advanced typecasting. | ||
# Defaults to +true+. | ||
def advanced_typecasting? | ||
@advanced_typecasting != false | ||
end | ||
defaults = { | ||
:advanced_typecasting => true, | ||
:parser => :rexml | ||
} | ||
|
||
# Sets whether to strip namespaces. | ||
attr_writer :strip_namespaces | ||
validate_options! defaults.keys, locals.keys | ||
options = @globals.merge defaults.merge(locals) | ||
|
||
# Returns whether to strip namespaces. | ||
# Defaults to +false+. | ||
def strip_namespaces? | ||
@strip_namespaces | ||
parser = load_parser options[:parser] | ||
parser.parse(xml, options) | ||
end | ||
|
||
# Expects a +block+ which receives a tag to convert. | ||
# Accepts +nil+ for a reset to the default behavior of not converting tags. | ||
def convert_tags_to(reset = nil, &block) | ||
@convert_tag = reset || block | ||
private | ||
|
||
def load_parser(parser) | ||
require "nori/parser/#{parser}" | ||
Parser.const_get PARSERS[parser] | ||
end | ||
|
||
# Transforms a given +tag+ using the specified conversion formula. | ||
def convert_tag(tag) | ||
@convert_tag.call(tag) | ||
def validate_xml!(xml) | ||
return if xml.kind_of? String | ||
raise ArgumentError, "Expected a String to parse, got: #{xml.inspect}" | ||
end | ||
|
||
# Returns whether to convert tags. | ||
def convert_tags? | ||
@convert_tag | ||
def validate_options!(available_options, options) | ||
spurious_options = options - available_options | ||
|
||
unless spurious_options.empty? | ||
raise ArgumentError, "Spurious options: #{spurious_options.inspect}\n" \ | ||
"Available options are: #{available_options.inspect}" | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
require "uri" | ||
|
||
module Nori | ||
class Nori | ||
module CoreExt | ||
module Hash | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Nori | ||
class Nori | ||
module CoreExt | ||
module Object | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Nori | ||
class Nori | ||
module CoreExt | ||
module String | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Nori | ||
class Nori | ||
class StringIOFile < StringIO | ||
|
||
attr_accessor :original_filename, :content_type | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Nori | ||
class Nori | ||
class StringWithAttributes < String | ||
|
||
attr_accessor :attributes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Nori | ||
class Nori | ||
|
||
VERSION = "1.1.3" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.