crack 0.3.2 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0ead2aedf1d6e60619824c1ee539f4f1251d53210a0fe6ce97fc7599243f67df
4
+ data.tar.gz: 75d22fb0beeb6a2464b60090ed6cd4e1b4e65a7856e0b026dcc0b952aabee940
5
+ SHA512:
6
+ metadata.gz: ba3875595545781c75ae83fc55776f69b76b1caf50f953b94f3c4fcec1cfd30461128799b19acb33a25065bc302269f0ecba40aec4bcd1194e946d132b348096
7
+ data.tar.gz: 93fc50484fc549e9e4d3e2a75c98fc3a0661adbccd87e0b5884613ad491594953bd586d3575255a6a4f5e8d7d8a1652af27c141043044d1523a27a41ad14871c
@@ -1,5 +1,4 @@
1
1
  module Crack
2
- VERSION = "0.3.2"
3
2
  class ParseError < StandardError; end
4
3
  end
5
4
 
@@ -3,15 +3,21 @@
3
3
  # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
4
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
5
 
6
- require 'yaml'
7
6
  require 'strscan'
8
7
 
9
8
  module Crack
10
9
  class JSON
10
+ def self.parser_exceptions
11
+ @parser_exceptions ||= [ArgumentError, Psych::SyntaxError]
12
+ end
13
+
11
14
  def self.parse(json)
12
- YAML.load(unescape(convert_json_to_yaml(json)))
13
- rescue ArgumentError => e
15
+ yaml = unescape(convert_json_to_yaml(json))
16
+ YAML.safe_load(yaml, [Regexp, Date, Time])
17
+ rescue *parser_exceptions
14
18
  raise ParseError, "Invalid JSON string"
19
+ rescue Psych::DisallowedClass
20
+ yaml
15
21
  end
16
22
 
17
23
  protected
@@ -24,12 +30,12 @@ module Crack
24
30
  end
25
31
 
26
32
  # matches YAML-formatted dates
27
- DATE_REGEX = /^\d{4}-\d{2}-\d{2}$|^\d{4}-\d{1,2}-\d{1,2}[T \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?)?$/
33
+ DATE_REGEX = /^\d{4}-\d{2}-\d{2}$|^\d{4}-\d{1,2}-\d{1,2}[T \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?)$/
28
34
 
29
35
  # Ensure that ":" and "," are always followed by a space
30
36
  def self.convert_json_to_yaml(json) #:nodoc:
31
37
  json = String.new(json) #can't modify a frozen string
32
- scanner, quoting, marks, pos, times = StringScanner.new(json), false, [], nil, []
38
+ scanner, quoting, marks, pos, date_starts, date_ends = StringScanner.new(json), false, [], nil, [], []
33
39
  while scanner.scan_until(/(\\['"]|['":,\/\\]|\\.)/)
34
40
  case char = scanner[1]
35
41
  when '"', "'"
@@ -42,7 +48,8 @@ module Crack
42
48
  # oh, and increment them for each current mark, each one is an extra padded space that bumps
43
49
  # the position in the final YAML output
44
50
  total_marks = marks.size
45
- times << pos+total_marks << scanner.pos+total_marks
51
+ date_starts << pos+total_marks
52
+ date_ends << scanner.pos+total_marks
46
53
  end
47
54
  quoting = false
48
55
  end
@@ -62,7 +69,7 @@ module Crack
62
69
  if marks.empty?
63
70
  json.gsub(/\\\//, '/')
64
71
  else
65
- left_pos = [-1].push(*marks)
72
+ left_pos = marks.clone.unshift(-1)
66
73
  right_pos = marks << json.length
67
74
  output = []
68
75
  left_pos.each_with_index do |left, i|
@@ -70,10 +77,22 @@ module Crack
70
77
  end
71
78
  output = output * " "
72
79
 
73
- times.each { |i| output[i-1] = ' ' }
80
+ format_dates(output, date_starts, date_ends)
74
81
  output.gsub!(/\\\//, '/')
75
82
  output
76
83
  end
77
84
  end
85
+
86
+ def self.format_dates(output, date_starts, date_ends)
87
+ if YAML.constants.include?('Syck')
88
+ (date_starts + date_ends).each { |i| output[i-1] = ' ' }
89
+ else
90
+ extra_chars_to_be_added = 0
91
+ date_starts.each do |i|
92
+ output[i-2+extra_chars_to_be_added] = '!!timestamp '
93
+ extra_chars_to_be_added += 10
94
+ end
95
+ end
96
+ end
78
97
  end
79
98
  end
@@ -0,0 +1,3 @@
1
+ module Crack
2
+ VERSION = "0.4.4"
3
+ end
@@ -2,11 +2,19 @@ require 'rexml/parsers/streamparser'
2
2
  require 'rexml/parsers/baseparser'
3
3
  require 'rexml/light/node'
4
4
  require 'rexml/text'
5
+ require "rexml/document"
5
6
  require 'date'
6
7
  require 'time'
7
8
  require 'yaml'
8
9
  require 'bigdecimal'
9
10
 
11
+ # The Reason behind redefining the String Class for this specific plugin is to
12
+ # avoid the dynamic insertion of stuff on it (see version previous to this commit).
13
+ # Doing that disables the possibility of efectuating a dump on the structure. This way it goes.
14
+ class REXMLUtiliyNodeString < String
15
+ attr_accessor :attributes
16
+ end
17
+
10
18
  # This is a slighly modified version of the XMLUtilityNode from
11
19
  # http://merb.devjavu.com/projects/merb/ticket/95 (has.sox@gmail.com)
12
20
  # It's mainly just adding vowels, as I ht cd wth n vwls :)
@@ -68,7 +76,8 @@ class REXMLUtilityNode #:nodoc:
68
76
  end
69
77
 
70
78
  def to_hash
71
- if @type == "file"
79
+ # ACG: Added a check here to prevent an exception a type == "file" tag has nodes within it
80
+ if @type == "file" and (@children.first.nil? or @children.first.is_a?(String))
72
81
  f = StringIO.new((@children.first || '').unpack('m').first)
73
82
  class << f
74
83
  attr_accessor :original_filename, :content_type
@@ -81,9 +90,7 @@ class REXMLUtilityNode #:nodoc:
81
90
  if @text
82
91
  t = typecast_value( unnormalize_xml_entities( inner_html ) )
83
92
  if t.is_a?(String)
84
- class << t
85
- attr_accessor :attributes
86
- end
93
+ t = REXMLUtiliyNodeString.new(t)
87
94
  t.attributes = attributes
88
95
  end
89
96
  return { name => t }
@@ -187,7 +194,7 @@ class REXMLUtilityNode #:nodoc:
187
194
  end
188
195
 
189
196
  module Crack
190
- class REXMLParser
197
+ class REXMLParser
191
198
  def self.parse(xml)
192
199
  stack = []
193
200
  parser = REXML::Parsers::BaseParser.new(xml)
@@ -210,6 +217,7 @@ module Crack
210
217
  stack.last.add_node(event[1]) unless event[1].strip.length == 0 || stack.empty?
211
218
  end
212
219
  end
220
+
213
221
  stack.length > 0 ? stack.pop.to_hash : {}
214
222
  end
215
223
  end
metadata CHANGED
@@ -1,84 +1,48 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: crack
3
- version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease:
6
- segments:
7
- - 0
8
- - 3
9
- - 2
10
- version: 0.3.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.4
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - John Nunemaker
14
- - Wynn Netherland
15
- autorequire:
8
+ autorequire:
16
9
  bindir: bin
17
10
  cert_chain: []
18
-
19
- date: 2013-01-09 00:00:00 Z
11
+ date: 2020-09-17 00:00:00.000000000 Z
20
12
  dependencies: []
21
-
22
- description:
23
- email: nunemaker@gmail.com
13
+ description: Really simple JSON and XML parsing, ripped from Merb and Rails.
14
+ email:
15
+ - nunemaker@gmail.com
24
16
  executables: []
25
-
26
17
  extensions: []
27
-
28
- extra_rdoc_files:
29
- - LICENSE
30
- - README.rdoc
31
- files:
32
- - History
33
- - LICENSE
34
- - README.rdoc
35
- - Rakefile
36
- - crack.gemspec
18
+ extra_rdoc_files: []
19
+ files:
37
20
  - lib/crack.rb
38
21
  - lib/crack/json.rb
39
22
  - lib/crack/util.rb
23
+ - lib/crack/version.rb
40
24
  - lib/crack/xml.rb
41
- - test/crack_test.rb
42
- - test/data/twittersearch-firefox.json
43
- - test/data/twittersearch-ie.json
44
- - test/hash_test.rb
45
- - test/json_test.rb
46
- - test/parser_test.rb
47
- - test/string_test.rb
48
- - test/test_helper.rb
49
- - test/xml_test.rb
50
25
  homepage: http://github.com/jnunemaker/crack
51
- licenses: []
52
-
53
- post_install_message:
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
54
30
  rdoc_options: []
55
-
56
- require_paths:
31
+ require_paths:
57
32
  - lib
58
- required_ruby_version: !ruby/object:Gem::Requirement
59
- none: false
60
- requirements:
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
61
35
  - - ">="
62
- - !ruby/object:Gem::Version
63
- hash: 3
64
- segments:
65
- - 0
66
- version: "0"
67
- required_rubygems_version: !ruby/object:Gem::Requirement
68
- none: false
69
- requirements:
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
70
40
  - - ">="
71
- - !ruby/object:Gem::Version
72
- hash: 3
73
- segments:
74
- - 0
75
- version: "0"
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
76
43
  requirements: []
77
-
78
- rubyforge_project: crack
79
- rubygems_version: 1.8.10
80
- signing_key:
81
- specification_version: 3
44
+ rubygems_version: 3.0.3
45
+ signing_key:
46
+ specification_version: 4
82
47
  summary: Really simple JSON and XML parsing, ripped from Merb and Rails.
83
48
  test_files: []
84
-
data/History DELETED
@@ -1,25 +0,0 @@
1
- == 0.1.7 2010-02-19
2
- * 1 minor patch
3
- * Added patch from @purp for ISO 8601 date/time format
4
- == 0.1.6 2010-01-31
5
- * 1 minor patch
6
- * Added Crack::VERSION constant - http://weblog.rubyonrails.org/2009/9/1/gem-packaging-best-practices
7
- == 0.1.5 2010-01-27
8
- * 1 minor patch
9
- * Strings that begin with dates shouldn't be parsed as such (sandro)
10
-
11
- == 0.1.3 2009-06-22
12
- * 1 minor patch
13
- * Parsing a text node with attributes stores them in the attributes method (tamalw)
14
-
15
- == 0.1.2 2009-04-21
16
- * 2 minor patches
17
- * Correct unnormalization of attribute values (der-flo)
18
- * Fix error in parsing YAML in the case where a hash value ends with backslashes, and there are subsequent values in the hash (deadprogrammer)
19
-
20
- == 0.1.1 2009-03-31
21
- * 1 minor patch
22
- * Parsing empty or blank xml now returns empty hash instead of raising error.
23
-
24
- == 0.1.0 2009-03-28
25
- * Initial release.
data/LICENSE DELETED
@@ -1,20 +0,0 @@
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.
@@ -1,42 +0,0 @@
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
- == note on releases
6
-
7
- Releases are tagged on github and also released as gems on github and rubyforge. Master is pushed to whenever I add a patch or a new feature. To build from master, you can clone the code, generate the updated gemspec, build the gem and install.
8
-
9
- * rake gemspec
10
- * gem build crack.gemspec
11
- * gem install the gem that was built
12
-
13
- == note on patches/pull requests
14
-
15
- * Fork the project.
16
- * Make your feature addition or bug fix.
17
- * Add tests for it. This is important so I don't break it in a future version unintentionally.
18
- * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself in another branch so I can ignore when I pull)
19
- * Send me a pull request. Bonus points for topic branches.
20
-
21
- == usage
22
-
23
- gem 'crack'
24
- require 'crack' # for xml and json
25
- require 'crack/json' # for just json
26
- require 'crack/xml' # for just xml
27
-
28
- == examples
29
-
30
- Crack::XML.parse("<tag>This is the contents</tag>")
31
- # => {'tag' => 'This is the contents'}
32
-
33
- Crack::JSON.parse('{"tag":"This is the contents"}')
34
- # => {'tag' => 'This is the contents'}
35
-
36
- == Copyright
37
-
38
- Copyright (c) 2009 John Nunemaker. See LICENSE for details.
39
-
40
- == Docs
41
-
42
- http://rdoc.info/projects/jnunemaker/crack
data/Rakefile DELETED
@@ -1,32 +0,0 @@
1
- $:.unshift("lib")
2
- require 'rubygems'
3
- require 'rake'
4
-
5
- $:.unshift(File.expand_path('lib', File.dirname(__FILE__)))
6
- require 'crack'
7
-
8
- begin
9
- require 'jeweler'
10
- Jeweler::Tasks.new do |gem|
11
- gem.name = "crack"
12
- gem.summary = %Q{Really simple JSON and XML parsing, ripped from Merb and Rails.}
13
- gem.email = "nunemaker@gmail.com"
14
- gem.homepage = "http://github.com/jnunemaker/crack"
15
- gem.authors = ["John Nunemaker", "Wynn Netherland"]
16
- gem.rubyforge_project = 'crack'
17
- gem.version = Crack::VERSION
18
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
- end
20
- Jeweler::GemcutterTasks.new
21
- rescue LoadError
22
- puts "Jeweler not available. Install it with: sudo gem install jeweler"
23
- end
24
-
25
- require 'rake/testtask'
26
- Rake::TestTask.new(:test) do |test|
27
- test.libs << 'lib' << 'test'
28
- test.pattern = 'test/**/*_test.rb'
29
- test.verbose = false
30
- end
31
-
32
- task :default => :test
@@ -1,53 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
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 = "crack"
8
- s.version = "0.3.2"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["John Nunemaker", "Wynn Netherland"]
12
- s.date = "2013-01-09"
13
- s.email = "nunemaker@gmail.com"
14
- s.extra_rdoc_files = [
15
- "LICENSE",
16
- "README.rdoc"
17
- ]
18
- s.files = [
19
- "History",
20
- "LICENSE",
21
- "README.rdoc",
22
- "Rakefile",
23
- "crack.gemspec",
24
- "lib/crack.rb",
25
- "lib/crack/json.rb",
26
- "lib/crack/util.rb",
27
- "lib/crack/xml.rb",
28
- "test/crack_test.rb",
29
- "test/data/twittersearch-firefox.json",
30
- "test/data/twittersearch-ie.json",
31
- "test/hash_test.rb",
32
- "test/json_test.rb",
33
- "test/parser_test.rb",
34
- "test/string_test.rb",
35
- "test/test_helper.rb",
36
- "test/xml_test.rb"
37
- ]
38
- s.homepage = "http://github.com/jnunemaker/crack"
39
- s.require_paths = ["lib"]
40
- s.rubyforge_project = "crack"
41
- s.rubygems_version = "1.8.10"
42
- s.summary = "Really simple JSON and XML parsing, ripped from Merb and Rails."
43
-
44
- if s.respond_to? :specification_version then
45
- s.specification_version = 3
46
-
47
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
- else
49
- end
50
- else
51
- end
52
- end
53
-