json_checker 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/.gitignore +42 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.md +22 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/json-config.json +21 -0
- data/json_checker.gemspec +27 -0
- data/lib/json_checker/checkable_file.rb +37 -0
- data/lib/json_checker/html_output.rb +70 -0
- data/lib/json_checker/json_comparator.rb +81 -0
- data/lib/json_checker/json_fetcher.rb +49 -0
- data/lib/json_checker/json_to_check.rb +13 -0
- data/lib/json_checker/json_validator.rb +78 -0
- data/lib/json_checker/version.rb +3 -0
- data/lib/json_checker.rb +12 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NjY3NDJiYTdkZmQ4MTUxMDRkZjVlNDU2OTg4ZWJmNTM2OTZhNDVhYQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NGNhMzQ2OWQxM2ZjMzRhOTJmYzUyODcxY2ZmOWJkYTNkMGRjODQxZg==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MmViYmVjYTIzMWU1NjQ4YTg5NDEyM2FmMDM3MmI0MzBkM2Q3NThkNTNlMzZh
|
10
|
+
Zjg4M2E0ZjlkOGY1OTk4MTg0NzQ2MTk0YjFlYTViNGEwNjQ4MDNhOGQ2NWVl
|
11
|
+
Nzg4Mjg1MTMxYTk0Mjk0OGU4MjA4NGI5YjhkZmY0MDExNWYwNDI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MmM4ZGFjNDEwZjMxYzA4ZGQ5NzRjNDlhZTI3N2UzYWExZDNkM2ZkYWRjN2Zl
|
14
|
+
MWU3ODI4Y2YwOTJkZTU2NTM0ODJmNmE0MzdiZmJlODM2YTM0NTQ5Njg2N2Y2
|
15
|
+
ZDJkMzhhNWVlY2NiNjkxNTdjMzI3ZTAyOTNhZjQ0YmI0Njk1MWQ=
|
data/.gitignore
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
/Gemfile.lock
|
13
|
+
/pkg/
|
14
|
+
output.html
|
15
|
+
|
16
|
+
# rspec failure tracking
|
17
|
+
.rspec_status
|
18
|
+
|
19
|
+
## Specific to RubyMotion:
|
20
|
+
.dat*
|
21
|
+
.repl_history
|
22
|
+
build/
|
23
|
+
|
24
|
+
## Documentation cache and generated files:
|
25
|
+
/.yardoc/
|
26
|
+
/_yardoc/
|
27
|
+
/doc/
|
28
|
+
/rdoc/
|
29
|
+
|
30
|
+
## Environment normalisation:
|
31
|
+
/.bundle/
|
32
|
+
/vendor/bundle
|
33
|
+
/lib/bundler/man/
|
34
|
+
|
35
|
+
# for a library or gem, you might want to ignore these files since the code is
|
36
|
+
# intended to run in multiple environments; otherwise, check them in:
|
37
|
+
Gemfile.lock
|
38
|
+
.ruby-version
|
39
|
+
.ruby-gemset
|
40
|
+
|
41
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
42
|
+
.rvmrc
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 ciceroduarte1@gmail.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
data/LICENSE.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
The MIT License (MIT)
|
3
|
+
|
4
|
+
Copyright (c) 2017 Francielly Moraes
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Cícero Duarte
|
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,41 @@
|
|
1
|
+
# JsonChecker
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/json_checker`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'json_checker'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install json_checker
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ciceroduarte/json_checker. 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.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "json_checker"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/json-config.json
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
{
|
2
|
+
"files": [
|
3
|
+
{
|
4
|
+
"name": "JSON Questions 2",
|
5
|
+
"remote-path": "http://private-1e56c2-rubytest.apiary-mock.com/questions2",
|
6
|
+
"keys" : {
|
7
|
+
"choice": "Swift3",
|
8
|
+
"choice1": "Swift22",
|
9
|
+
"choice2": "Swift22",
|
10
|
+
"choice3": "Swift3",
|
11
|
+
"choiceTroll": "hue"
|
12
|
+
},
|
13
|
+
"compare-to": [
|
14
|
+
{
|
15
|
+
"remote-path": "http://private-1e56c2-rubytest.apiary-mock.com/questions",
|
16
|
+
"name": "Questions"
|
17
|
+
}
|
18
|
+
]
|
19
|
+
}
|
20
|
+
]
|
21
|
+
}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'json_checker/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "json_checker"
|
8
|
+
spec.version = JsonChecker::VERSION
|
9
|
+
spec.authors = ["Cícero Duarte"]
|
10
|
+
spec.email = ["ciceroduarte1@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Validate fields and compare with others json files}
|
13
|
+
spec.homepage = "https://github.com/ciceroduarte/json_checker"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
spec.add_development_dependency "diffy", "3.1.0"
|
27
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
|
2
|
+
require "json_checker/json_fetcher"
|
3
|
+
|
4
|
+
module JsonChecker
|
5
|
+
class CheckableFile
|
6
|
+
attr_reader :name, :path, :remotePath
|
7
|
+
|
8
|
+
def initialize(representation)
|
9
|
+
@name = representation['name']
|
10
|
+
@path = representation['path']
|
11
|
+
@remotePath = representation['remote-path']
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.is_valid_representation?(representation)
|
15
|
+
json = JsonChecker::JSONFetcher.json_from_content(representation)
|
16
|
+
unless json.nil?
|
17
|
+
return (json.keys.include? 'name') && ((json.keys.include? 'path') || (json.keys.include? 'remote-path'))
|
18
|
+
end
|
19
|
+
return false
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_content()
|
23
|
+
if @path.nil? && @remotePath.nil?
|
24
|
+
puts "[ERROR] path or remote-path not found"
|
25
|
+
return nil
|
26
|
+
end
|
27
|
+
|
28
|
+
unless @content.nil?
|
29
|
+
return @content
|
30
|
+
end
|
31
|
+
|
32
|
+
@content = @path.nil? ? JSONFetcher.json_from_url(@remotePath) : JSONFetcher.json_from_path(@path)
|
33
|
+
|
34
|
+
return @content
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module JsonChecker
|
2
|
+
class HTMLOutput
|
3
|
+
|
4
|
+
def self.add_validation_item(title, values)
|
5
|
+
item = "<h2>#{title}</h2>"
|
6
|
+
item << "<div class=\"validation\" style=\"overflow-x:auto;\">
|
7
|
+
<table><tr><th>Status</th><th>Key</th><th>Expected</th><th>Value</th></tr>"
|
8
|
+
|
9
|
+
values.each do |value|
|
10
|
+
item = item + value
|
11
|
+
end
|
12
|
+
|
13
|
+
item << "</table></div>"
|
14
|
+
|
15
|
+
if @reportItems.nil?
|
16
|
+
@reportItems = Array.new()
|
17
|
+
end
|
18
|
+
|
19
|
+
@reportItems << item
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.add_comparation_item(title, json)
|
23
|
+
item = "<h2>#{title}</h2>" + "<div class=\"diff\">" + json + "</div>"
|
24
|
+
if @reportItems.nil?
|
25
|
+
@reportItems = Array.new()
|
26
|
+
end
|
27
|
+
@reportItems << item
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.testandoHTML()
|
31
|
+
puts @reportItems
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.generate_output()
|
35
|
+
htmlOutput = HTMLOutput.new()
|
36
|
+
output = "<html>" + htmlOutput.add_style() + "<body>"
|
37
|
+
@reportItems.each do |item|
|
38
|
+
output << item
|
39
|
+
end
|
40
|
+
output << "</body></html>"
|
41
|
+
|
42
|
+
htmlOutput.save_to_file(output)
|
43
|
+
end
|
44
|
+
|
45
|
+
def add_style()
|
46
|
+
return "<style>
|
47
|
+
.addition { background: #eaffea; }
|
48
|
+
.remotion { background: #ffecec; }
|
49
|
+
.diff { outline: 1px solid #eff0d6; margin: 5px; padding-top: 5px; padding-bottom: 5px; }
|
50
|
+
.validation { margin: 5px; }
|
51
|
+
body { font-family: monospace; }
|
52
|
+
table { border-collapse: collapse; width: 100%; }
|
53
|
+
td { border: 1px solid #eff0d6; }
|
54
|
+
th { background-color: #4CAF50; color: white; border: 1px solid #4CAF50; }
|
55
|
+
th, td { padding: 5px; text-align: left; }
|
56
|
+
tr:hover { background-color: #f5f5f5 }
|
57
|
+
p { margin-top: 0em; margin-bottom: 0em; white-space: pre; padding : 3; color: #3e3333 }
|
58
|
+
</style>"
|
59
|
+
end
|
60
|
+
|
61
|
+
def save_to_file(report)
|
62
|
+
if report.nil?
|
63
|
+
puts "[ERROR] Invalid report"
|
64
|
+
else
|
65
|
+
File.write("output.html", report)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'diffy'
|
2
|
+
require 'tempfile'
|
3
|
+
require 'json_checker/checkable_file'
|
4
|
+
require 'json_checker/json_fetcher'
|
5
|
+
|
6
|
+
module JsonChecker
|
7
|
+
class JSONComparator
|
8
|
+
def self.compare(fileToCheck, compareTo)
|
9
|
+
|
10
|
+
if fileToCheck.nil? || compareTo.nil? || !compareTo.is_a?(Array) || !fileToCheck.is_a?(JSONToCheck)
|
11
|
+
return
|
12
|
+
end
|
13
|
+
|
14
|
+
compareTo.each do |compare|
|
15
|
+
if CheckableFile.is_valid_representation?(compare)
|
16
|
+
checkableFile = CheckableFile.new(compare)
|
17
|
+
fileContent = checkableFile.get_content()
|
18
|
+
|
19
|
+
jsonComparator = JSONComparator.new()
|
20
|
+
jsonComparator.compare_json("Comparing #{fileToCheck.name} with #{checkableFile.name}", fileToCheck.get_content(), fileContent)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def compare_json(title, json, jsonToCompare)
|
26
|
+
temp_json = tempfile_from_json(json)
|
27
|
+
temp_jsonToCompare = tempfile_from_json(jsonToCompare)
|
28
|
+
|
29
|
+
unless temp_json.nil? && temp_jsonToCompare.nil?
|
30
|
+
diff = Diffy::Diff.new(temp_json.path, temp_jsonToCompare.path, :source => 'files', :context => 3)
|
31
|
+
puts diff
|
32
|
+
html_report_from_diff(title, diff)
|
33
|
+
|
34
|
+
temp_jsonToCompare.delete
|
35
|
+
temp_json.delete
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def tempfile_from_json(json)
|
40
|
+
json = JSONFetcher.json_from_content(json)
|
41
|
+
unless json.nil?
|
42
|
+
tempfile = Tempfile.new("temp_json")
|
43
|
+
tempfile.write(JSON.pretty_generate(json) + "\n")
|
44
|
+
tempfile.close
|
45
|
+
return tempfile
|
46
|
+
end
|
47
|
+
puts "[ERROR] File content is not a valid JSON"
|
48
|
+
return nil
|
49
|
+
end
|
50
|
+
|
51
|
+
def html_report_from_diff(title, diff)
|
52
|
+
if diff.nil?
|
53
|
+
return nil
|
54
|
+
end
|
55
|
+
|
56
|
+
result = ""
|
57
|
+
diff.to_s.each_line do |line|
|
58
|
+
result = result + add_line(line)
|
59
|
+
end
|
60
|
+
HTMLOutput.add_comparation_item(title, result)
|
61
|
+
end
|
62
|
+
|
63
|
+
def add_line(line)
|
64
|
+
|
65
|
+
if line.nil?
|
66
|
+
return ""
|
67
|
+
end
|
68
|
+
|
69
|
+
line = line.gsub("\n","")
|
70
|
+
formatter = "<p class=\"%{first}\">%{second}</p>"
|
71
|
+
className = "null"
|
72
|
+
|
73
|
+
if line.chars.first == "+"
|
74
|
+
className = "addition"
|
75
|
+
elsif line.chars.first == "-"
|
76
|
+
className = "remotion"
|
77
|
+
end
|
78
|
+
return formatter % {first: className, second: line}
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require 'json'
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
module JsonChecker
|
6
|
+
|
7
|
+
class JSONFetcher
|
8
|
+
|
9
|
+
def self.json_from_path(path)
|
10
|
+
begin
|
11
|
+
jsonFile = open(path)
|
12
|
+
jsonContent = jsonFile.read
|
13
|
+
return JSONFetcher.json_from_content(jsonContent)
|
14
|
+
rescue
|
15
|
+
puts "[ERROR] #{path} not found"
|
16
|
+
return nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.json_from_url(url)
|
21
|
+
begin
|
22
|
+
uri = URI(url)
|
23
|
+
response = Net::HTTP.get_response(uri)
|
24
|
+
case response
|
25
|
+
when Net::HTTPSuccess then
|
26
|
+
return JSONFetcher.json_from_content(response.body)
|
27
|
+
else
|
28
|
+
puts"[ERROR] Connection error"
|
29
|
+
end
|
30
|
+
rescue
|
31
|
+
end
|
32
|
+
|
33
|
+
return nil
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.json_from_content(content)
|
37
|
+
if content.nil? || (!content.is_a?(String) && (!content.is_a?(Hash)))
|
38
|
+
return nil
|
39
|
+
end
|
40
|
+
|
41
|
+
begin
|
42
|
+
return content.is_a?(String) ? JSON.parse(content) : JSON.parse(content.to_json)
|
43
|
+
rescue JSON::ParserError => e
|
44
|
+
puts "[ERROR] Invalid json"
|
45
|
+
end
|
46
|
+
return nil
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'json_checker/checkable_file'
|
2
|
+
|
3
|
+
module JsonChecker
|
4
|
+
class JSONToCheck < CheckableFile
|
5
|
+
attr_reader :keys, :compareTo
|
6
|
+
|
7
|
+
def initialize(representation)
|
8
|
+
super(representation)
|
9
|
+
@keys = representation['keys']
|
10
|
+
@compareTo = representation['compare-to']
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require 'json'
|
3
|
+
require 'json_checker/json_to_check'
|
4
|
+
require 'json_checker/json_fetcher'
|
5
|
+
require 'json_checker/json_comparator'
|
6
|
+
require 'json_checker/html_output'
|
7
|
+
|
8
|
+
module JsonChecker
|
9
|
+
class JSONValidator
|
10
|
+
def self.validate_with_config(config)
|
11
|
+
|
12
|
+
files = config['files']
|
13
|
+
|
14
|
+
if files.nil? || files.empty?
|
15
|
+
puts "[ERROR] Invalid json"
|
16
|
+
return
|
17
|
+
end
|
18
|
+
|
19
|
+
files.each do |file|
|
20
|
+
fileToCheck = JSONToCheck.new(file)
|
21
|
+
|
22
|
+
fileContent = fileToCheck.get_content()
|
23
|
+
|
24
|
+
unless fileToCheck.keys.nil?
|
25
|
+
title = "Validating #{fileToCheck.name} values"
|
26
|
+
jsonValidator = JSONValidator.new()
|
27
|
+
jsonValidator.validate_JSON_with_keys(title, fileToCheck.keys, fileContent)
|
28
|
+
end
|
29
|
+
|
30
|
+
unless fileToCheck.compareTo.nil?
|
31
|
+
JSONComparator.compare(fileToCheck, fileToCheck.compareTo)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
HTMLOutput.generate_output()
|
35
|
+
end
|
36
|
+
|
37
|
+
def validate_JSON_with_keys(title, jsonKeys, json)
|
38
|
+
items = Array.new()
|
39
|
+
jsonKeys.keys.each do |key|
|
40
|
+
value = value_for_key(key, json)
|
41
|
+
expected = jsonKeys[key]
|
42
|
+
items << verify_value(expected, value, key)
|
43
|
+
end
|
44
|
+
if items.size > 0
|
45
|
+
HTMLOutput.add_validation_item(title, items)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def value_for_key(key, json)
|
50
|
+
return value_for_key_with_split_character(key, json, '.')
|
51
|
+
end
|
52
|
+
|
53
|
+
def is_numeric?(obj)
|
54
|
+
obj.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
|
55
|
+
end
|
56
|
+
|
57
|
+
def value_for_key_with_split_character(key, json, splitCharacter)
|
58
|
+
value = json
|
59
|
+
key.split(splitCharacter).each do |item|
|
60
|
+
if !value.nil? && !item.empty?
|
61
|
+
value = is_numeric?(item) ? value[item.to_i] : value[item]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
return value
|
65
|
+
end
|
66
|
+
|
67
|
+
def verify_value(expected, value, key)
|
68
|
+
formatter = "<tr><td>%{first}</td><td>%{second}</td><td>%{third}</td><td>%{fourth}</td></tr>"
|
69
|
+
result = "Error"
|
70
|
+
if value.nil?
|
71
|
+
result = "Not found"
|
72
|
+
elsif value == expected
|
73
|
+
result = "Success"
|
74
|
+
end
|
75
|
+
return formatter % {first: result, second: key, third:expected, fourth:value}
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/lib/json_checker.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "json_checker/version"
|
2
|
+
require 'json_checker/json_fetcher'
|
3
|
+
require 'json_checker/json_validator'
|
4
|
+
|
5
|
+
module JsonChecker
|
6
|
+
|
7
|
+
jsonConfig = JSONFetcher.json_from_path('json-config.json')
|
8
|
+
|
9
|
+
unless jsonConfig.nil?
|
10
|
+
JSONValidator.validate_with_config(jsonConfig)
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json_checker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cícero Duarte
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: diffy
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.1.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.1.0
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- ciceroduarte1@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- .travis.yml
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.md
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- json-config.json
|
88
|
+
- json_checker.gemspec
|
89
|
+
- lib/json_checker.rb
|
90
|
+
- lib/json_checker/checkable_file.rb
|
91
|
+
- lib/json_checker/html_output.rb
|
92
|
+
- lib/json_checker/json_comparator.rb
|
93
|
+
- lib/json_checker/json_fetcher.rb
|
94
|
+
- lib/json_checker/json_to_check.rb
|
95
|
+
- lib/json_checker/json_validator.rb
|
96
|
+
- lib/json_checker/version.rb
|
97
|
+
homepage: https://github.com/ciceroduarte/json_checker
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.6.10
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: Validate fields and compare with others json files
|
121
|
+
test_files: []
|