xtj 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bffd71e881a2d7876585c478e6d4c4808302275eb852a18dbee7dedeb26c798e
4
+ data.tar.gz: b88148ec4ecb2734b4cc0c03238e149ab2b00de7300657d4462f548f5825c570
5
+ SHA512:
6
+ metadata.gz: b239afa2eee385bcaf6315622ffaf3bd9fa19141b5636c24aa949c840d106c2afd521991684e57c0674c2a9768603be0c703019323668864ca762efda1731ca8
7
+ data.tar.gz: 5b895fbda18c6f876be16e89850fbb473a58db673253a35d0e91b7c18089427758d73cea474c749e574099fc9b4429871e2746dc4f39b750d9d4a6665dfcf1f5
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ Exclude:
4
+ - Gemfile
5
+ - '*.gemspec'
6
+ - 'spec/**/*_spec.rb'
7
+ - 'vendor/**/*'
8
+
9
+ Style/Documentation:
10
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5
7
+ - 2.6
8
+ before_install: gem install bundler -v 1.17.2
9
+ script:
10
+ - bundle exec rspec spec
11
+ - bundle exec rubocop
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at grzegorz.jakubiak@outlook.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in xtj.gemspec
6
+ gemspec
7
+
8
+ gem 'sorbet', :group => :development
9
+ gem 'sorbet-runtime'
10
+
11
+ group :lint do
12
+ gem 'rubocop', '~> 0.74.0', require: false
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ XTJ (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ diff-lcs (1.3)
11
+ jaro_winkler (1.5.3)
12
+ parallel (1.17.0)
13
+ parser (2.6.4.0)
14
+ ast (~> 2.4.0)
15
+ rainbow (3.0.0)
16
+ rake (10.5.0)
17
+ rspec (3.8.0)
18
+ rspec-core (~> 3.8.0)
19
+ rspec-expectations (~> 3.8.0)
20
+ rspec-mocks (~> 3.8.0)
21
+ rspec-core (3.8.2)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-expectations (3.8.4)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.8.0)
26
+ rspec-mocks (3.8.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.8.0)
29
+ rspec-support (3.8.2)
30
+ rubocop (0.74.0)
31
+ jaro_winkler (~> 1.5.1)
32
+ parallel (~> 1.10)
33
+ parser (>= 2.6)
34
+ rainbow (>= 2.2.2, < 4.0)
35
+ ruby-progressbar (~> 1.7)
36
+ unicode-display_width (>= 1.4.0, < 1.7)
37
+ ruby-progressbar (1.10.1)
38
+ sorbet (0.4.4723)
39
+ sorbet-static (= 0.4.4723)
40
+ sorbet-runtime (0.4.4723)
41
+ sorbet-static (0.4.4723-universal-darwin-14)
42
+ unicode-display_width (1.6.0)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ XTJ!
49
+ bundler (~> 1.17)
50
+ rake (~> 10.0)
51
+ rspec (~> 3.0)
52
+ rubocop (~> 0.74.0)
53
+ sorbet
54
+ sorbet-runtime
55
+
56
+ BUNDLED WITH
57
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Grzegorz Jakubiak
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # XTJ
2
+
3
+ [![Build Status](https://travis-ci.org/grzegorz-jakubiak/xtj.svg?branch=master)](https://travis-ci.org/grzegorz-jakubiak/xtj)
4
+
5
+ Allows for conversion of an `XML` file into Ruby `Hash` or `JSON`.
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'xtj'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install xtj
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ require 'xtj'
28
+
29
+ xml_document = XTJ::Document.new(<xml_document_string>)
30
+
31
+ file = File.new(<xml_document_path>)
32
+ xml_document = XTJ::Document.new(file)
33
+
34
+ xml_document.to_hash
35
+ xml_document.to_json
36
+ ```
37
+
38
+ ## Development
39
+
40
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+
42
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/xtj. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
51
+
52
+ ## Code of Conduct
53
+
54
+ Everyone interacting in the XTJ project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/xtj/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'xtj'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,47 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ require 'rexml/sax2listener'
5
+
6
+ module XTJ
7
+ module Handlers
8
+ class XMLHandler
9
+ include REXML::SAX2Listener
10
+ include XTJ::Tags
11
+ attr_reader :xml_hash
12
+
13
+ def initialize
14
+ @xml_hash = {}
15
+ @stack = []
16
+ end
17
+
18
+ def start_element(_uri, _localname, qname, attributes)
19
+ prefixed_attributes = attributes
20
+ .transform_keys { |key| "_#{key}".to_sym }
21
+ element = XMLTag.new(tag: qname.to_sym, attributes: prefixed_attributes)
22
+ @stack.prepend(element)
23
+ end
24
+
25
+ def end_element(_uri, _localname, _qname)
26
+ if @stack.length > 1
27
+ child = @stack.shift
28
+ parent = @stack.shift
29
+ parent.add_to_attributes(child.tag, child.attributes)
30
+ @stack.prepend(parent)
31
+ else
32
+ element = @stack.shift
33
+ @xml_hash[element.tag] = element.attributes
34
+ end
35
+ end
36
+
37
+ def characters(text)
38
+ return if text.strip.empty?
39
+ raise REXML::ParseException, 'Invalid XML' if @stack.empty?
40
+
41
+ element = @stack.shift
42
+ element.attributes[:@text] = text
43
+ @stack.prepend(element)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,29 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ module XTJ
5
+ module Tags
6
+ class XMLTag
7
+ extend T::Sig
8
+ attr_reader :tag, :attributes
9
+
10
+ sig { params(tag: Symbol, attributes: Hash).void }
11
+ def initialize(tag:, attributes:)
12
+ @tag = tag
13
+ @attributes = attributes
14
+ end
15
+
16
+ sig { params(tag: Symbol, attributes: Hash).void }
17
+ def add_to_attributes(tag, attributes)
18
+ if @attributes[tag].nil?
19
+ @attributes[tag] = attributes
20
+ elsif @attributes[tag].is_a?(Array)
21
+ @attributes[tag] << attributes
22
+ else
23
+ same_tags = [@attributes[tag]]
24
+ @attributes[tag] = same_tags << attributes
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,6 @@
1
+ # typed: strong
2
+ # frozen_string_literal: true
3
+
4
+ module XTJ
5
+ VERSION = '0.1.0'
6
+ end
data/lib/xtj.rb ADDED
@@ -0,0 +1,39 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ require_relative 'xtj/version'
5
+ require_relative 'xtj/tags/xml_tag'
6
+ require_relative 'xtj/handlers/xml_handler'
7
+ require 'rexml/parsers/sax2parser'
8
+ require 'json'
9
+
10
+ module XTJ
11
+ class Document
12
+ extend T::Sig
13
+
14
+ def initialize(xml_document)
15
+ @document = xml_document
16
+ end
17
+
18
+ sig { returns(Hash) }
19
+ def to_hash
20
+ @to_hash ||= parse
21
+ end
22
+
23
+ sig { params(_args: T.untyped).returns(String) }
24
+ def to_json(*_args)
25
+ to_hash.to_json
26
+ end
27
+
28
+ private
29
+
30
+ sig { returns(Hash) }
31
+ def parse
32
+ parser = REXML::Parsers::SAX2Parser.new(@document)
33
+ handler = Handlers::XMLHandler.new
34
+ parser.listen(handler)
35
+ parser.parse
36
+ handler.xml_hash
37
+ end
38
+ end
39
+ end
data/sorbet/config ADDED
@@ -0,0 +1,2 @@
1
+ --dir
2
+ .