json_updater 0.1.0 → 0.4.1
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 +4 -4
- data/.gitignore +1 -0
- data/.reek.yml +5 -0
- data/.rubocop.yml +19 -0
- data/Gemfile +4 -2
- data/Gemfile.lock +43 -0
- data/Rakefile +4 -2
- data/analyzer.sh +26 -0
- data/bin/json_updater +7 -3
- data/json_updater.gemspec +20 -15
- data/lib/json_updater/json_full_builder.rb +45 -0
- data/lib/json_updater/json_structure_updater.rb +28 -0
- data/lib/json_updater/json_type_detector.rb +33 -0
- data/lib/json_updater/json_validator.rb +34 -0
- data/lib/json_updater/one_level_json_array_builder.rb +22 -0
- data/lib/json_updater/one_level_json_builder.rb +9 -0
- data/lib/json_updater/version.rb +3 -1
- data/lib/json_updater.rb +41 -3
- metadata +54 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1369c256030d09acbe23c4e948b7e933bc37edcd8d7af20d27ccd9ded600ad19
|
4
|
+
data.tar.gz: 6dc9e09847b8d7ef18059c9037620ab743d40e4009d0a00522f180b94d70f484
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9cbe6d11c86ef64586cf2693db4bc1f7f486e8b12ad6a8c3a1724b9bb598d94d6736c7d92071d71c2f1a5d389378db63c1ab3dec26087c4b9c1476de912b5429
|
7
|
+
data.tar.gz: fc8b7cc124aa5c789f263f2d1d9ac8a0b8208da2a8cb0b381c6eec2e561983eeb418dcd6e8870ce65c5177bdeb4493daf4ff2e6fcfb9d7e79de9483034ccd433
|
data/.gitignore
CHANGED
data/.reek.yml
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- 'bin/*'
|
4
|
+
- 'pkg/**/*'
|
5
|
+
- 'spec/fixtures/**/*'
|
6
|
+
|
7
|
+
Metrics/LineLength:
|
8
|
+
Max: 120
|
9
|
+
|
10
|
+
Style/Documentation:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
Metrics/ModuleLength:
|
14
|
+
Exclude:
|
15
|
+
- "**/*_spec.rb"
|
16
|
+
|
17
|
+
Metrics/BlockLength:
|
18
|
+
Exclude:
|
19
|
+
- "**/*_spec.rb"
|
data/Gemfile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
4
6
|
|
5
7
|
# Specify your gem's dependencies in json_updater.gemspec
|
6
8
|
gemspec
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
json_updater (0.4.1)
|
5
|
+
json
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
coderay (1.1.2)
|
11
|
+
diff-lcs (1.3)
|
12
|
+
json (2.1.0)
|
13
|
+
method_source (0.9.2)
|
14
|
+
pry (0.12.2)
|
15
|
+
coderay (~> 1.1.0)
|
16
|
+
method_source (~> 0.9.0)
|
17
|
+
rake (10.5.0)
|
18
|
+
rspec (3.8.0)
|
19
|
+
rspec-core (~> 3.8.0)
|
20
|
+
rspec-expectations (~> 3.8.0)
|
21
|
+
rspec-mocks (~> 3.8.0)
|
22
|
+
rspec-core (3.8.0)
|
23
|
+
rspec-support (~> 3.8.0)
|
24
|
+
rspec-expectations (3.8.2)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.8.0)
|
27
|
+
rspec-mocks (3.8.0)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.8.0)
|
30
|
+
rspec-support (3.8.0)
|
31
|
+
|
32
|
+
PLATFORMS
|
33
|
+
ruby
|
34
|
+
|
35
|
+
DEPENDENCIES
|
36
|
+
bundler (~> 1.16)
|
37
|
+
json_updater!
|
38
|
+
pry
|
39
|
+
rake (~> 10.0)
|
40
|
+
rspec
|
41
|
+
|
42
|
+
BUNDLED WITH
|
43
|
+
1.16.4
|
data/Rakefile
CHANGED
data/analyzer.sh
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
HEIGLIGHT='\033[0;37m'
|
2
|
+
NO_COLOR='\033[0m'
|
3
|
+
|
4
|
+
rubocop && \
|
5
|
+
echo -e "${HEIGLIGHT}
|
6
|
+
rubocop OK
|
7
|
+
${NO_COLOR}" && \
|
8
|
+
|
9
|
+
rspec && \
|
10
|
+
echo -e "${HEIGLIGHT}
|
11
|
+
rspec OK
|
12
|
+
${NO_COLOR}" && \
|
13
|
+
|
14
|
+
reek && \
|
15
|
+
echo -e "${HEIGLIGHT}
|
16
|
+
reek OK
|
17
|
+
${NO_COLOR}" && \
|
18
|
+
|
19
|
+
bundle-audit -u && \
|
20
|
+
echo -e "${HEIGLIGHT}
|
21
|
+
bundle-audit -u OK
|
22
|
+
${NO_COLOR}" && \
|
23
|
+
|
24
|
+
echo -e "${HEIGLIGHT}
|
25
|
+
ALL OK
|
26
|
+
${NO_COLOR}"
|
data/bin/json_updater
CHANGED
@@ -2,8 +2,12 @@
|
|
2
2
|
|
3
3
|
require "json_updater"
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
arg1 = JsonUpdater::JsonValidator.new(ARGV[0])
|
6
|
+
arg2 = JsonUpdater::JsonValidator.new(ARGV[1])
|
7
|
+
|
8
|
+
if arg1.valid? && arg2.valid?
|
9
|
+
JsonUpdater::UpdateService.update_json(ARGV[0], ARGV[1])
|
7
10
|
else
|
8
|
-
|
11
|
+
puts arg1.messages
|
12
|
+
puts arg2.messages
|
9
13
|
end
|
data/json_updater.gemspec
CHANGED
@@ -1,36 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
|
-
lib = File.expand_path(
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
5
|
+
require 'json_updater/version'
|
5
6
|
|
6
7
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
8
|
+
spec.name = 'json_updater'
|
8
9
|
spec.version = JsonUpdater::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
10
|
+
spec.authors = ['Vladislav Hilko']
|
11
|
+
spec.email = ['vladislav.spawn@gmail.com']
|
11
12
|
|
12
|
-
spec.summary =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
13
|
+
spec.summary = 'This is a gem for updating json file'
|
14
|
+
spec.homepage = 'https://github.com/juggleross/json_updater'
|
15
|
+
spec.license = 'MIT'
|
15
16
|
|
16
17
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
18
|
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
19
|
if spec.respond_to?(:metadata)
|
19
20
|
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
20
21
|
else
|
21
|
-
raise
|
22
|
-
|
22
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
23
|
+
'public gem pushes.'
|
23
24
|
end
|
24
25
|
|
25
26
|
# Specify which files should be added to the gem when it is released.
|
26
27
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
27
|
-
spec.files
|
28
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
28
29
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
29
30
|
end
|
30
31
|
|
31
|
-
spec.executables = [
|
32
|
-
spec.require_paths = [
|
32
|
+
spec.executables = ['json_updater']
|
33
|
+
spec.require_paths = ['lib']
|
33
34
|
|
34
|
-
spec.
|
35
|
-
|
35
|
+
spec.add_dependency 'json'
|
36
|
+
|
37
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
38
|
+
spec.add_development_dependency 'pry'
|
39
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
40
|
+
spec.add_development_dependency 'rspec'
|
36
41
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JsonUpdater
|
4
|
+
class JsonFullBuilder
|
5
|
+
attr_reader :json_etalon, :json_changeble
|
6
|
+
|
7
|
+
def self.build(json_changeble, json_etalon)
|
8
|
+
new(json_changeble, json_etalon).build
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(json_changeble, json_etalon)
|
12
|
+
@json_changeble = json_changeble
|
13
|
+
@json_etalon = json_etalon
|
14
|
+
end
|
15
|
+
|
16
|
+
def build
|
17
|
+
recursion_updation(json_changeble, json_etalon)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def recursion_updation(mutation_json, inner_json_etalon)
|
23
|
+
inner_json_etalon.each do |etalon_field_key, etalon_field_value|
|
24
|
+
if hash_or_array?(etalon_field_value)
|
25
|
+
mutation_json[etalon_field_key] = dive_inside(mutation_json[etalon_field_key], etalon_field_value)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
JsonTypeDetector.detect_type(mutation_json).build(mutation_json, inner_json_etalon)
|
29
|
+
end
|
30
|
+
|
31
|
+
def dive_inside(mutation_field_value, etalon_field_value)
|
32
|
+
case etalon_field_value
|
33
|
+
when Hash
|
34
|
+
recursion_updation(mutation_field_value, etalon_field_value)
|
35
|
+
when Array
|
36
|
+
mutation_field_value.map { |hash| recursion_updation(hash, etalon_field_value.first) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# this method smells of :reek:UtilityFunction
|
41
|
+
def hash_or_array?(etalon_field_value)
|
42
|
+
[Hash, Array].include?(etalon_field_value.class)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JsonUpdater
|
4
|
+
class JsonStructureUpdater
|
5
|
+
attr_reader :json_etalon, :json_changeable
|
6
|
+
|
7
|
+
def self.update_json(json_changeable, json_etalon)
|
8
|
+
new(json_changeable, json_etalon).update_json
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(json_changeable, json_etalon)
|
12
|
+
@json_changeable = json_changeable
|
13
|
+
@json_etalon = json_etalon.dup
|
14
|
+
end
|
15
|
+
|
16
|
+
def update_json
|
17
|
+
update_json_keys
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def update_json_keys
|
23
|
+
json_etalon.each do |key, _value|
|
24
|
+
json_etalon[key] = json_changeable[key] if json_changeable.key?(key)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JsonUpdater
|
4
|
+
class JsonTypeDetector
|
5
|
+
attr_reader :json_changeble
|
6
|
+
|
7
|
+
def self.detect_type(json_changeble)
|
8
|
+
new(json_changeble).detect_type
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(json_changeble)
|
12
|
+
@json_changeble = json_changeble
|
13
|
+
end
|
14
|
+
|
15
|
+
def detect_type
|
16
|
+
if one_level_json?
|
17
|
+
JsonUpdater::OneLevelJsonBuilder
|
18
|
+
elsif one_level_json_array?
|
19
|
+
JsonUpdater::OneLevelJsonArrayBuilder
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def one_level_json?
|
26
|
+
json_changeble.is_a?(Hash)
|
27
|
+
end
|
28
|
+
|
29
|
+
def one_level_json_array?
|
30
|
+
json_changeble.is_a?(Array)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JsonUpdater
|
4
|
+
class JsonValidator
|
5
|
+
attr_reader :path
|
6
|
+
|
7
|
+
def initialize(path)
|
8
|
+
@path = path
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
file_exist? && json_valid?
|
13
|
+
end
|
14
|
+
|
15
|
+
def messages
|
16
|
+
return "#{path} - file does't exist" unless file_exist?
|
17
|
+
return "#{path} - json file doesn't valid" unless json_valid?
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def file_exist?
|
23
|
+
File.exist? path
|
24
|
+
end
|
25
|
+
|
26
|
+
def json_valid?
|
27
|
+
file = File.open(path).read
|
28
|
+
JSON.parse(file)
|
29
|
+
true
|
30
|
+
rescue JSON::ParserError => _e
|
31
|
+
false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JsonUpdater
|
4
|
+
class OneLevelJsonArrayBuilder
|
5
|
+
attr_reader :json_etalon, :json_changeble
|
6
|
+
|
7
|
+
def self.build(json_changeble, json_etalon)
|
8
|
+
new(json_changeble, json_etalon).build
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(json_changeble, json_etalon)
|
12
|
+
@json_changeble = json_changeble
|
13
|
+
@json_etalon = json_etalon
|
14
|
+
end
|
15
|
+
|
16
|
+
def build
|
17
|
+
json_changeble.map do |json_item|
|
18
|
+
JsonStructureUpdater.update_json(json_item, json_etalon)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/json_updater/version.rb
CHANGED
data/lib/json_updater.rb
CHANGED
@@ -1,9 +1,47 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
require 'json_updater/version'
|
6
|
+
require 'json_updater/one_level_json_builder'
|
7
|
+
require 'json_updater/one_level_json_array_builder'
|
8
|
+
require 'json_updater/json_full_builder'
|
9
|
+
|
10
|
+
require 'json_updater/json_structure_updater'
|
11
|
+
require 'json_updater/json_type_detector'
|
12
|
+
require 'json_updater/json_validator'
|
2
13
|
|
3
14
|
module JsonUpdater
|
4
15
|
class UpdateService
|
5
|
-
|
6
|
-
|
16
|
+
attr_reader :changable_file_path, :json_changeble, :json_etalon
|
17
|
+
|
18
|
+
def self.update_json(changable_file_path, etalon_file_path)
|
19
|
+
new(changable_file_path, etalon_file_path).update_json
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(changable_file_path, etalon_file_path)
|
23
|
+
@changable_file_path = changable_file_path
|
24
|
+
|
25
|
+
@json_changeble = JSON.parse(File.open(changable_file_path).read)
|
26
|
+
@json_etalon = JSON.parse(File.open(etalon_file_path).read)
|
27
|
+
end
|
28
|
+
|
29
|
+
def update_json
|
30
|
+
rewrite_file
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def rewrite_file
|
36
|
+
File.open(changable_file_path, 'w') { |file| file.write(output_json) }
|
37
|
+
end
|
38
|
+
|
39
|
+
def output_json
|
40
|
+
JSON.pretty_generate(updated_json)
|
41
|
+
end
|
42
|
+
|
43
|
+
def updated_json
|
44
|
+
JsonUpdater::JsonFullBuilder.build(json_changeble, json_etalon)
|
7
45
|
end
|
8
46
|
end
|
9
47
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_updater
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladislav Hilko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-02-
|
11
|
+
date: 2019-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -24,6 +38,20 @@ dependencies:
|
|
24
38
|
- - "~>"
|
25
39
|
- !ruby/object:Gem::Version
|
26
40
|
version: '1.16'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
27
55
|
- !ruby/object:Gem::Dependency
|
28
56
|
name: rake
|
29
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,6 +66,20 @@ dependencies:
|
|
38
66
|
- - "~>"
|
39
67
|
- !ruby/object:Gem::Version
|
40
68
|
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
41
83
|
description:
|
42
84
|
email:
|
43
85
|
- vladislav.spawn@gmail.com
|
@@ -47,15 +89,25 @@ extensions: []
|
|
47
89
|
extra_rdoc_files: []
|
48
90
|
files:
|
49
91
|
- ".gitignore"
|
92
|
+
- ".reek.yml"
|
93
|
+
- ".rubocop.yml"
|
50
94
|
- Gemfile
|
95
|
+
- Gemfile.lock
|
51
96
|
- LICENSE.txt
|
52
97
|
- README.md
|
53
98
|
- Rakefile
|
99
|
+
- analyzer.sh
|
54
100
|
- bin/console
|
55
101
|
- bin/json_updater
|
56
102
|
- bin/setup
|
57
103
|
- json_updater.gemspec
|
58
104
|
- lib/json_updater.rb
|
105
|
+
- lib/json_updater/json_full_builder.rb
|
106
|
+
- lib/json_updater/json_structure_updater.rb
|
107
|
+
- lib/json_updater/json_type_detector.rb
|
108
|
+
- lib/json_updater/json_validator.rb
|
109
|
+
- lib/json_updater/one_level_json_array_builder.rb
|
110
|
+
- lib/json_updater/one_level_json_builder.rb
|
59
111
|
- lib/json_updater/version.rb
|
60
112
|
homepage: https://github.com/juggleross/json_updater
|
61
113
|
licenses:
|