parsing-utils 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/parsing_utils.rb +52 -0
- data/parsing-utils.gemspec +50 -0
- data/spec/parsing_utils_spec.rb +31 -0
- data/spec/spec_helper.rb +10 -0
- metadata +66 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Cyril Rohr
|
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,18 @@
|
|
1
|
+
= parsing-utils
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but
|
13
|
+
bump version in a commit by itself I can ignore when I pull)
|
14
|
+
* Send me a pull request. Bonus points for topic branches.
|
15
|
+
|
16
|
+
== Copyright
|
17
|
+
|
18
|
+
Copyright (c) 2009 Cyril Rohr. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "parsing-utils"
|
8
|
+
gem.summary = %Q{Some classes useful for parsing matters (especially to select a parser based on a given mime type)}
|
9
|
+
gem.description = %Q{Some classes useful for parsing matters (especially to select a parser based on a given mime type)}
|
10
|
+
gem.email = "cyril.rohr@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/crohr/parsing-utils"
|
12
|
+
gem.authors = ["Cyril Rohr"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'spec/rake/spectask'
|
21
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
22
|
+
spec.libs << 'lib' << 'spec'
|
23
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
24
|
+
end
|
25
|
+
|
26
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
27
|
+
spec.libs << 'lib' << 'spec'
|
28
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
29
|
+
spec.rcov = true
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
if File.exist?('VERSION')
|
40
|
+
version = File.read('VERSION')
|
41
|
+
else
|
42
|
+
version = ""
|
43
|
+
end
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "parsing-utils #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# Some classes useful for parsing matters (especially to select a parser based on a given mime type)
|
2
|
+
module ParsingUtils
|
3
|
+
class ParsingUtilsError < StandardError; end
|
4
|
+
class UnsupportedFormat < ParsingUtilsError; end
|
5
|
+
# == Usage
|
6
|
+
# Parser.add(JSONParser, "application/json", "application/vnd.com.example.Object+json")
|
7
|
+
#
|
8
|
+
class Parser
|
9
|
+
# FIXME: A bit ugly
|
10
|
+
@@available_parsers = []
|
11
|
+
class << self
|
12
|
+
def available_parsers; @@available_parsers; end
|
13
|
+
def add(parser, *mime_types)
|
14
|
+
parser.dependencies
|
15
|
+
parser.supported_mime_types = mime_types
|
16
|
+
@@available_parsers << parser
|
17
|
+
self
|
18
|
+
end
|
19
|
+
def select(mime_type)
|
20
|
+
raise UnsupportedFormat, "The content_type cannot be nil." if mime_type.nil?
|
21
|
+
parsers = available_parsers.select{|parser| parser.supported_mime_types.include?(mime_type)}
|
22
|
+
if parsers.empty?
|
23
|
+
raise UnsupportedFormat, "No parser found for '#{mime_type}' content type."
|
24
|
+
else
|
25
|
+
parsers.first
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class JSONParser
|
32
|
+
class << self
|
33
|
+
attr_accessor :supported_mime_types
|
34
|
+
def dependencies
|
35
|
+
require 'json'
|
36
|
+
end
|
37
|
+
def dump(object, options = {})
|
38
|
+
options = options.dup
|
39
|
+
case options.delete(:format)
|
40
|
+
when :pretty
|
41
|
+
JSON.pretty_generate object, options
|
42
|
+
else
|
43
|
+
JSON.generate(object, options)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
def load(object, options = {})
|
47
|
+
JSON.parse(object, options)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{parsing-utils}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Cyril Rohr"]
|
12
|
+
s.date = %q{2009-10-21}
|
13
|
+
s.description = %q{Some classes useful for parsing matters (especially to select a parser based on a given mime type)}
|
14
|
+
s.email = %q{cyril.rohr@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/parsing_utils.rb",
|
27
|
+
"parsing-utils.gemspec",
|
28
|
+
"spec/parsing_utils_spec.rb",
|
29
|
+
"spec/spec_helper.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/crohr/parsing-utils}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.5}
|
35
|
+
s.summary = %q{Some classes useful for parsing matters (especially to select a parser based on a given mime type)}
|
36
|
+
s.test_files = [
|
37
|
+
"spec/parsing_utils_spec.rb",
|
38
|
+
"spec/spec_helper.rb"
|
39
|
+
]
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
+
s.specification_version = 3
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
46
|
+
else
|
47
|
+
end
|
48
|
+
else
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe ParsingUtils do
|
4
|
+
it "should add the parser to the list of available parsers" do
|
5
|
+
ParsingUtils::Parser.add(ParsingUtils::JSONParser, "application/json", "application/vnd.com.example.Object+json;level=1")
|
6
|
+
ParsingUtils::Parser.available_parsers.should == [ParsingUtils::JSONParser]
|
7
|
+
ParsingUtils::JSONParser.supported_mime_types.should == ["application/json", "application/vnd.com.example.Object+json;level=1"]
|
8
|
+
end
|
9
|
+
|
10
|
+
describe ParsingUtils::JSONParser do
|
11
|
+
it "should correctly pretty generate an object" do
|
12
|
+
object = {1=>2, 3=>4}
|
13
|
+
ParsingUtils::JSONParser.dependencies
|
14
|
+
ParsingUtils::JSONParser.dump(object, :format => :pretty).should == "{\n \"1\": 2,\n \"3\": 4\n}"
|
15
|
+
end
|
16
|
+
it "should correctly generate an object in the raw format" do
|
17
|
+
object = {1=>2, 3=>4}
|
18
|
+
ParsingUtils::JSONParser.dependencies
|
19
|
+
ParsingUtils::JSONParser.dump(object).should == "{\"1\":2,\"3\":4}"
|
20
|
+
end
|
21
|
+
it "should correctly generate an object having a custom to_json function" do
|
22
|
+
class Foo
|
23
|
+
def to_json(*a)
|
24
|
+
"{\"1\":3,\"3\":5}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
ParsingUtils::JSONParser.dependencies
|
28
|
+
ParsingUtils::JSONParser.dump(Foo.new).should == "{\"1\":3,\"3\":5}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: parsing-utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cyril Rohr
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-21 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Some classes useful for parsing matters (especially to select a parser based on a given mime type)
|
17
|
+
email: cyril.rohr@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- lib/parsing_utils.rb
|
33
|
+
- parsing-utils.gemspec
|
34
|
+
- spec/parsing_utils_spec.rb
|
35
|
+
- spec/spec_helper.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/crohr/parsing-utils
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --charset=UTF-8
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.3.5
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Some classes useful for parsing matters (especially to select a parser based on a given mime type)
|
64
|
+
test_files:
|
65
|
+
- spec/parsing_utils_spec.rb
|
66
|
+
- spec/spec_helper.rb
|