sishen-crack 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +26 -0
- data/Rakefile +49 -0
- data/VERSION.yml +4 -0
- data/lib/crack.rb +7 -0
- data/lib/crack/core_extensions.rb +126 -0
- data/lib/crack/json.rb +80 -0
- data/lib/crack/json/backends/jsongem.rb +37 -0
- data/lib/crack/json/backends/yajl.rb +40 -0
- data/lib/crack/json/backends/yaml.rb +81 -0
- data/lib/crack/xml.rb +211 -0
- data/test/crack_test.rb +4 -0
- data/test/data/twittersearch-firefox.json +1 -0
- data/test/data/twittersearch-ie.json +1 -0
- data/test/json_test.rb +75 -0
- data/test/test_helper.rb +12 -0
- data/test/xml_test.rb +458 -0
- metadata +74 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 John Nunemaker
|
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,26 @@
|
|
1
|
+
= crack
|
2
|
+
|
3
|
+
Really simple JSON and XML parsing, ripped from Merb and Rails. The XML parser is ripped from Merb and the JSON parser is ripped from Rails. I take no credit, just packaged them for all to enjoy and easily use.
|
4
|
+
|
5
|
+
= usage
|
6
|
+
|
7
|
+
gem 'crack'
|
8
|
+
require 'crack' # for xml and json
|
9
|
+
require 'crack/json' # for just json
|
10
|
+
require 'crack/xml' # for just xml
|
11
|
+
|
12
|
+
= examples
|
13
|
+
|
14
|
+
Crack::XML.parse("<tag>This is the contents</tag>")
|
15
|
+
# => {'tag' => 'This is the contents'}
|
16
|
+
|
17
|
+
Crack::JSON.parse('{"tag":"This is the contents"}')
|
18
|
+
# => {'tag' => 'This is the contents'}
|
19
|
+
|
20
|
+
== Copyright
|
21
|
+
|
22
|
+
Copyright (c) 2009 John Nunemaker. See LICENSE for details.
|
23
|
+
|
24
|
+
== Docs
|
25
|
+
|
26
|
+
http://rdoc.info/projects/jnunemaker/crack
|
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 = "crack"
|
8
|
+
gem.summary = %Q{Really simple JSON and XML parsing, ripped from Merb and Rails.}
|
9
|
+
gem.email = "nunemaker@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/jnunemaker/crack"
|
11
|
+
gem.authors = ["John Nunemaker"]
|
12
|
+
gem.rubyforge_project = 'crack'
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake/rdoctask'
|
20
|
+
Rake::RDocTask.new do |rdoc|
|
21
|
+
rdoc.rdoc_dir = 'rdoc'
|
22
|
+
rdoc.title = 'crack'
|
23
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
24
|
+
rdoc.rdoc_files.include('README*')
|
25
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
26
|
+
end
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/*_test.rb'
|
32
|
+
test.verbose = false
|
33
|
+
end
|
34
|
+
|
35
|
+
begin
|
36
|
+
require 'rcov/rcovtask'
|
37
|
+
Rcov::RcovTask.new do |test|
|
38
|
+
test.libs << 'test'
|
39
|
+
test.pattern = 'test/**/*_test.rb'
|
40
|
+
test.verbose = true
|
41
|
+
end
|
42
|
+
rescue LoadError
|
43
|
+
task :rcov do
|
44
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
task :default => :test
|
data/VERSION.yml
ADDED
data/lib/crack.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
class Object #:nodoc:
|
2
|
+
# @return <TrueClass, FalseClass>
|
3
|
+
#
|
4
|
+
# @example [].blank? #=> true
|
5
|
+
# @example [1].blank? #=> false
|
6
|
+
# @example [nil].blank? #=> false
|
7
|
+
#
|
8
|
+
# Returns true if the object is nil or empty (if applicable)
|
9
|
+
def blank?
|
10
|
+
nil? || (respond_to?(:empty?) && empty?)
|
11
|
+
end unless method_defined?(:blank?)
|
12
|
+
end # class Object
|
13
|
+
|
14
|
+
class Numeric #:nodoc:
|
15
|
+
# @return <TrueClass, FalseClass>
|
16
|
+
#
|
17
|
+
# Numerics can't be blank
|
18
|
+
def blank?
|
19
|
+
false
|
20
|
+
end unless method_defined?(:blank?)
|
21
|
+
end # class Numeric
|
22
|
+
|
23
|
+
class NilClass #:nodoc:
|
24
|
+
# @return <TrueClass, FalseClass>
|
25
|
+
#
|
26
|
+
# Nils are always blank
|
27
|
+
def blank?
|
28
|
+
true
|
29
|
+
end unless method_defined?(:blank?)
|
30
|
+
end # class NilClass
|
31
|
+
|
32
|
+
class TrueClass #:nodoc:
|
33
|
+
# @return <TrueClass, FalseClass>
|
34
|
+
#
|
35
|
+
# True is not blank.
|
36
|
+
def blank?
|
37
|
+
false
|
38
|
+
end unless method_defined?(:blank?)
|
39
|
+
end # class TrueClass
|
40
|
+
|
41
|
+
class FalseClass #:nodoc:
|
42
|
+
# False is always blank.
|
43
|
+
def blank?
|
44
|
+
true
|
45
|
+
end unless method_defined?(:blank?)
|
46
|
+
end # class FalseClass
|
47
|
+
|
48
|
+
class String #:nodoc:
|
49
|
+
# @example "".blank? #=> true
|
50
|
+
# @example " ".blank? #=> true
|
51
|
+
# @example " hey ho ".blank? #=> false
|
52
|
+
#
|
53
|
+
# @return <TrueClass, FalseClass>
|
54
|
+
#
|
55
|
+
# Strips out whitespace then tests if the string is empty.
|
56
|
+
def blank?
|
57
|
+
strip.empty?
|
58
|
+
end unless method_defined?(:blank?)
|
59
|
+
|
60
|
+
def snake_case
|
61
|
+
return self.downcase if self =~ /^[A-Z]+$/
|
62
|
+
self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
|
63
|
+
return $+.downcase
|
64
|
+
end unless method_defined?(:snake_case)
|
65
|
+
end # class String
|
66
|
+
|
67
|
+
class Hash #:nodoc:
|
68
|
+
# @return <String> This hash as a query string
|
69
|
+
#
|
70
|
+
# @example
|
71
|
+
# { :name => "Bob",
|
72
|
+
# :address => {
|
73
|
+
# :street => '111 Ruby Ave.',
|
74
|
+
# :city => 'Ruby Central',
|
75
|
+
# :phones => ['111-111-1111', '222-222-2222']
|
76
|
+
# }
|
77
|
+
# }.to_params
|
78
|
+
# #=> "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."
|
79
|
+
def to_params
|
80
|
+
params = self.map { |k,v| normalize_param(k,v) }.join
|
81
|
+
params.chop! # trailing &
|
82
|
+
params
|
83
|
+
end
|
84
|
+
|
85
|
+
# @param key<Object> The key for the param.
|
86
|
+
# @param value<Object> The value for the param.
|
87
|
+
#
|
88
|
+
# @return <String> This key value pair as a param
|
89
|
+
#
|
90
|
+
# @example normalize_param(:name, "Bob Jones") #=> "name=Bob%20Jones&"
|
91
|
+
def normalize_param(key, value)
|
92
|
+
param = ''
|
93
|
+
stack = []
|
94
|
+
|
95
|
+
if value.is_a?(Array)
|
96
|
+
param << value.map { |element| normalize_param("#{key}[]", element) }.join
|
97
|
+
elsif value.is_a?(Hash)
|
98
|
+
stack << [key,value]
|
99
|
+
else
|
100
|
+
param << "#{key}=#{URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&"
|
101
|
+
end
|
102
|
+
|
103
|
+
stack.each do |parent, hash|
|
104
|
+
hash.each do |key, value|
|
105
|
+
if value.is_a?(Hash)
|
106
|
+
stack << ["#{parent}[#{key}]", value]
|
107
|
+
else
|
108
|
+
param << normalize_param("#{parent}[#{key}]", value)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
param
|
114
|
+
end
|
115
|
+
|
116
|
+
# @return <String> The hash as attributes for an XML tag.
|
117
|
+
#
|
118
|
+
# @example
|
119
|
+
# { :one => 1, "two"=>"TWO" }.to_xml_attributes
|
120
|
+
# #=> 'one="1" two="TWO"'
|
121
|
+
def to_xml_attributes
|
122
|
+
map do |k,v|
|
123
|
+
%{#{k.to_s.snake_case.sub(/^(.{1,1})/) { |m| m.downcase }}="#{v}"}
|
124
|
+
end.join(' ')
|
125
|
+
end
|
126
|
+
end
|
data/lib/crack/json.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# Copyright (c) 2004-2008 David Heinemeier Hansson
|
2
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
3
|
+
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
4
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
5
|
+
|
6
|
+
require 'yaml'
|
7
|
+
require 'strscan'
|
8
|
+
|
9
|
+
module Crack
|
10
|
+
|
11
|
+
# <refactor>
|
12
|
+
#
|
13
|
+
def self.use_standard_json_time_format
|
14
|
+
@@use_standard_json_time_format
|
15
|
+
end
|
16
|
+
def self.use_standard_json_time_format=(val)
|
17
|
+
@@use_standard_json_time_format = val
|
18
|
+
end
|
19
|
+
def self.parse_json_times
|
20
|
+
@@parse_json_times
|
21
|
+
end
|
22
|
+
def self.parse_json_times=(val)
|
23
|
+
@@parse_json_times = val
|
24
|
+
end
|
25
|
+
# If true, use ISO 8601 format for dates and times. Otherwise, fall back to the Active Support legacy format.
|
26
|
+
@@use_standard_json_time_format = true
|
27
|
+
# Look for and parse json strings that look like ISO 8601 times.
|
28
|
+
@@parse_json_times = true
|
29
|
+
#
|
30
|
+
# </refactor>
|
31
|
+
|
32
|
+
module JSON
|
33
|
+
# matches YAML-formatted dates
|
34
|
+
DATE_REGEX = /^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[ \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?))$/
|
35
|
+
|
36
|
+
class << self
|
37
|
+
attr_reader :backend
|
38
|
+
# delegate :decode, :to => :backend
|
39
|
+
|
40
|
+
def decode(json)
|
41
|
+
@backend.decode(json)
|
42
|
+
end
|
43
|
+
alias :parse :decode
|
44
|
+
|
45
|
+
def backend=(name)
|
46
|
+
if name.is_a?(Module)
|
47
|
+
@backend = name
|
48
|
+
else
|
49
|
+
require "crack/json/backends/#{name.to_s.downcase}.rb"
|
50
|
+
@backend = Crack::JSON::Backends::const_get(name)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def with_backend(name)
|
55
|
+
old_backend, self.backend = backend, name
|
56
|
+
yield
|
57
|
+
ensure
|
58
|
+
self.backend = old_backend
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class << self
|
64
|
+
def escape_html_entities_in_json
|
65
|
+
@escape_html_entities_in_json
|
66
|
+
end
|
67
|
+
|
68
|
+
def escape_html_entities_in_json=(value)
|
69
|
+
Crack::JSON::Encoding.escape_regex = \
|
70
|
+
if value
|
71
|
+
/[\010\f\n\r\t"\\><&]/
|
72
|
+
else
|
73
|
+
/[\010\f\n\r\t"\\]/
|
74
|
+
end
|
75
|
+
@escape_html_entities_in_json = value
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
JSON.backend = 'Yaml'
|
80
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'json' unless defined?(::JSON)
|
2
|
+
module Crack
|
3
|
+
module JSON
|
4
|
+
ParseError = ::JSON::ParserError
|
5
|
+
|
6
|
+
module Backends
|
7
|
+
module JSONGem
|
8
|
+
extend self
|
9
|
+
|
10
|
+
# Converts a JSON string into a Ruby object.
|
11
|
+
def decode(json)
|
12
|
+
data = ::JSON.parse(json)
|
13
|
+
if Crack.parse_json_times
|
14
|
+
convert_dates_from(data)
|
15
|
+
else
|
16
|
+
data
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def convert_dates_from(data)
|
22
|
+
case data
|
23
|
+
when DATE_REGEX
|
24
|
+
DateTime.parse(data)
|
25
|
+
when Array
|
26
|
+
data.map! { |d| convert_dates_from(d) }
|
27
|
+
when Hash
|
28
|
+
data.each do |key, value|
|
29
|
+
data[key] = convert_dates_from(value)
|
30
|
+
end
|
31
|
+
else data
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
require 'yajl' unless defined?(Yajl)
|
3
|
+
module Crack
|
4
|
+
ParseError = ::Yajl::ParseError
|
5
|
+
module JSON
|
6
|
+
module Backends
|
7
|
+
module Yajl
|
8
|
+
extend self
|
9
|
+
|
10
|
+
# Converts a JSON string into a Ruby object.
|
11
|
+
def decode(json)
|
12
|
+
if !json.respond_to?(:read)
|
13
|
+
json = StringIO.new(json)
|
14
|
+
end
|
15
|
+
data = ::Yajl::Parser.parse(json)
|
16
|
+
if Crack.parse_json_times
|
17
|
+
convert_dates_from(data)
|
18
|
+
else
|
19
|
+
data
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def convert_dates_from(data)
|
25
|
+
case data
|
26
|
+
when DATE_REGEX
|
27
|
+
DateTime.parse(data)
|
28
|
+
when Array
|
29
|
+
data.map! { |d| convert_dates_from(d) }
|
30
|
+
when Hash
|
31
|
+
data.each do |key, value|
|
32
|
+
data[key] = convert_dates_from(value)
|
33
|
+
end
|
34
|
+
else data
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module Crack
|
2
|
+
module JSON
|
3
|
+
class ParseError < StandardError
|
4
|
+
end
|
5
|
+
|
6
|
+
module Backends
|
7
|
+
module Yaml
|
8
|
+
extend self
|
9
|
+
|
10
|
+
# Converts a JSON string into a Ruby object.
|
11
|
+
def decode(json)
|
12
|
+
YAML.load(convert_json_to_yaml(json))
|
13
|
+
rescue ArgumentError => e
|
14
|
+
raise ParseError, "Invalid JSON string"
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
# Ensure that ":" and "," are always followed by a space
|
19
|
+
def convert_json_to_yaml(json) #:nodoc:
|
20
|
+
require 'strscan' unless defined? ::StringScanner
|
21
|
+
scanner, quoting, marks, pos, times = ::StringScanner.new(json), false, [], nil, []
|
22
|
+
while scanner.scan_until(/(\\['"]|['":,\\]|\\.)/)
|
23
|
+
case char = scanner[1]
|
24
|
+
when '"', "'"
|
25
|
+
if !quoting
|
26
|
+
quoting = char
|
27
|
+
pos = scanner.pos
|
28
|
+
elsif quoting == char
|
29
|
+
if json[pos..scanner.pos-2] =~ DATE_REGEX
|
30
|
+
# found a date, track the exact positions of the quotes so we can remove them later.
|
31
|
+
# oh, and increment them for each current mark, each one is an extra padded space that bumps
|
32
|
+
# the position in the final YAML output
|
33
|
+
total_marks = marks.size
|
34
|
+
times << pos+total_marks << scanner.pos+total_marks
|
35
|
+
end
|
36
|
+
quoting = false
|
37
|
+
end
|
38
|
+
when ":",","
|
39
|
+
marks << scanner.pos - 1 unless quoting
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
if marks.empty?
|
44
|
+
json.gsub(/\\([\\\/]|u[[:xdigit:]]{4})/) do
|
45
|
+
ustr = $1
|
46
|
+
if ustr.start_with?('u')
|
47
|
+
[ustr[1..-1].to_i(16)].pack("U")
|
48
|
+
elsif ustr == '\\'
|
49
|
+
'\\\\'
|
50
|
+
else
|
51
|
+
ustr
|
52
|
+
end
|
53
|
+
end
|
54
|
+
else
|
55
|
+
left_pos = [-1].push(*marks)
|
56
|
+
right_pos = marks << scanner.pos + scanner.rest_size
|
57
|
+
output = []
|
58
|
+
left_pos.each_with_index do |left, i|
|
59
|
+
scanner.pos = left.succ
|
60
|
+
output << scanner.peek(right_pos[i] - scanner.pos + 1).gsub(/\\([\\\/]|u[[:xdigit:]]{4})/) do
|
61
|
+
ustr = $1
|
62
|
+
if ustr.start_with?('u')
|
63
|
+
[ustr[1..-1].to_i(16)].pack("U")
|
64
|
+
elsif ustr == '\\'
|
65
|
+
'\\\\'
|
66
|
+
else
|
67
|
+
ustr
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
output = output * " "
|
72
|
+
|
73
|
+
times.each { |i| output[i-1] = ' ' }
|
74
|
+
output.gsub!(/\\\//, '/')
|
75
|
+
output
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|