json_statham 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bcc57468bc33bb597b1817adc3f882a28b6f9f06e2377d3cd6abe2e0a16c1bd8
4
+ data.tar.gz: fae57291e39be69d2fe5a84763eb2280c3c0112719e4cc43959c0decf94bb35d
5
+ SHA512:
6
+ metadata.gz: ba7b836e09f469ec095b73b0908bf0ccffa19605fb3e1ccbf6aa855824a1c1b8f55677370595bda2b326445b61b7a8f1d010544edef8e3b30a0c923b8cce5591
7
+ data.tar.gz: 3c5fda63e093ff347e378b75fa507559b23b7b59cbacfaf72ef7151651ecbe43a48d5fd56dca817573a17137cd744d882ea02afa6f3ddfbe86de500fca11cb75
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,43 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+ SuggestExtensions: false
5
+ Exclude:
6
+ - "vendor/**/*"
7
+
8
+ Style/FrozenStringLiteralComment:
9
+ Enabled: false
10
+
11
+ Style/StringLiterals:
12
+ EnforcedStyle: double_quotes
13
+
14
+ Style/Documentation:
15
+ Enabled: false
16
+
17
+ Style/WordArray:
18
+ Enabled: false
19
+
20
+ Style/SymbolArray:
21
+ Enabled: false
22
+
23
+ Metrics/CyclomaticComplexity:
24
+ Enabled: true
25
+
26
+ Metrics/MethodLength:
27
+ Max: 20
28
+
29
+ Metrics/BlockLength:
30
+ Exclude:
31
+ - "spec/**/*_spec.rb"
32
+ - "*.gemspec"
33
+
34
+ Layout/LineLength:
35
+ Exclude:
36
+ - "spec/**/*_spec.rb"
37
+ - "*.gemspec"
38
+
39
+ Naming/MemoizedInstanceVariableName:
40
+ EnforcedStyleForLeadingUnderscores: optional
41
+
42
+ Gemspec/RequireMFA:
43
+ Enabled: false
data/.simplecov ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "simplecov_json_formatter"
4
+
5
+ SimpleCov.start
6
+ SimpleCov.formatters = SimpleCov::Formatter::JSONFormatter
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## Changelog
2
+
3
+ ### [0.1.0] - 2023-02-06
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at authiatv@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in json_statham.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ gem "pry", "~> 0.14.2"
15
+
16
+ gem "simplecov", "~> 0.22.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,72 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ json_statham (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ coderay (1.1.3)
11
+ diff-lcs (1.5.0)
12
+ docile (1.4.0)
13
+ json (2.6.3)
14
+ method_source (1.0.0)
15
+ parallel (1.22.1)
16
+ parser (3.2.0.0)
17
+ ast (~> 2.4.1)
18
+ pry (0.14.2)
19
+ coderay (~> 1.1)
20
+ method_source (~> 1.0)
21
+ rainbow (3.1.1)
22
+ rake (13.0.6)
23
+ regexp_parser (2.6.2)
24
+ rexml (3.2.5)
25
+ rspec (3.12.0)
26
+ rspec-core (~> 3.12.0)
27
+ rspec-expectations (~> 3.12.0)
28
+ rspec-mocks (~> 3.12.0)
29
+ rspec-core (3.12.0)
30
+ rspec-support (~> 3.12.0)
31
+ rspec-expectations (3.12.2)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.12.0)
34
+ rspec-mocks (3.12.3)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.12.0)
37
+ rspec-support (3.12.0)
38
+ rubocop (1.44.1)
39
+ json (~> 2.3)
40
+ parallel (~> 1.10)
41
+ parser (>= 3.2.0.0)
42
+ rainbow (>= 2.2.2, < 4.0)
43
+ regexp_parser (>= 1.8, < 3.0)
44
+ rexml (>= 3.2.5, < 4.0)
45
+ rubocop-ast (>= 1.24.1, < 2.0)
46
+ ruby-progressbar (~> 1.7)
47
+ unicode-display_width (>= 2.4.0, < 3.0)
48
+ rubocop-ast (1.24.1)
49
+ parser (>= 3.1.1.0)
50
+ ruby-progressbar (1.11.0)
51
+ simplecov (0.22.0)
52
+ docile (~> 1.1)
53
+ simplecov-html (~> 0.11)
54
+ simplecov_json_formatter (~> 0.1)
55
+ simplecov-html (0.12.3)
56
+ simplecov_json_formatter (0.1.4)
57
+ unicode-display_width (2.4.2)
58
+
59
+ PLATFORMS
60
+ arm64-darwin-21
61
+ x86_64-linux
62
+
63
+ DEPENDENCIES
64
+ json_statham!
65
+ pry (~> 0.14.2)
66
+ rake (~> 13.0)
67
+ rspec (~> 3.0)
68
+ rubocop (~> 1.21)
69
+ simplecov (~> 0.22.0)
70
+
71
+ BUNDLED WITH
72
+ 2.3.23
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Victor
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,158 @@
1
+ # JsonStatham
2
+
3
+ [![Build Status](https://github.com/VictorAuthiat/json_statham/actions/workflows/ci.yml/badge.svg)](https://github.com/VictorAuthiat/json_statham/actions/workflows/ci.yml)
4
+ [![Code Climate](https://codeclimate.com/github/VictorAuthiat/json_statham/badges/gpa.svg)](https://codeclimate.com/github/VictorAuthiat/json_statham)
5
+ [![Test Coverage](https://codeclimate.com/github/VictorAuthiat/json_statham/badges/coverage.svg)](https://codeclimate.com/github/VictorAuthiat/json_statham/coverage)
6
+ [![Issue Count](https://codeclimate.com/github/VictorAuthiat/json_statham/badges/issue_count.svg)](https://codeclimate.com/github/VictorAuthiat/json_statham)
7
+
8
+ Ruby library to check json schema changes.
9
+
10
+ JsonStatham allows to check the structure changes in a json.
11
+ It allows to check that the keys are always the same and that the value classes are identical. You can also observe the differences in execution time of your block.
12
+
13
+ You can use this library when you want to change your serialization system. For example if you want to migrate from [fast_jsonapi](https://github.com/Netflix/fast_jsonapi) to another library.
14
+
15
+ ## Installation
16
+
17
+ Install the gem and add to the application's Gemfile by executing:
18
+
19
+ $ bundle add json_statham
20
+
21
+ If bundler is not being used to manage dependencies, install the gem by executing:
22
+
23
+ $ gem install json_statham
24
+
25
+ ## Usage
26
+
27
+ Configure the `schemas_path`.
28
+
29
+ ```ruby
30
+ JsonStatham.configure do |config|
31
+ config.schemas_path = "schemas"
32
+ end
33
+ ```
34
+
35
+ Now you can extend `JsonStatham` to use `.stathamnize` method.
36
+
37
+ **Example:**
38
+
39
+ ```ruby
40
+ module Foo
41
+ extend JsonStatham
42
+
43
+ def self.call(hash)
44
+ result = stathamnize("foo") { hash }
45
+
46
+ result.success?
47
+ end
48
+ end
49
+ ```
50
+
51
+ Running `Foo.call({ foo: :bar })` will create a new file **foo.json** in a **schemas** directory. This file contain the json schema and execution duration.
52
+ ⚠️ File will be created only if **store_schema** config attribute is true.
53
+
54
+ **Example:**
55
+
56
+ ```json
57
+ {"schema":{"bar":"symbol"},"duration":2.9999937396496534e-06}
58
+ ```
59
+
60
+ It returns a `JsonStatham::Result` object.
61
+ You can execute `success?` or `failure?` on JsonStatham::Result.
62
+ Running `Foo.call` a second time with a new hash schema and `store_schema = false` will not create a new file and result a failure.
63
+
64
+ ## Configuration:
65
+
66
+ Available configuration attributes:
67
+
68
+ ```ruby
69
+ JsonStatham.configure do |config|
70
+ config.schemas_path = "schemas"
71
+ config.store_schema = true
72
+ config.logger = true
73
+ end
74
+ ```
75
+
76
+ *Required attributes:*
77
+
78
+ - `schemas_path` The path where the json files will be read and created
79
+
80
+ *Optional attributes:*
81
+
82
+ - `store_schema` Default to `false`. It allows to create or not a new file
83
+
84
+ - `logger` Default to `false`. It allows to create or not a new file
85
+
86
+ ## Example using RSpec
87
+
88
+ extend `JsonStatham` in your spec_helper.
89
+
90
+ ```ruby
91
+ RSpec.configure do |config|
92
+ config.include JsonStatham
93
+ end
94
+ ```
95
+
96
+ Given a UserSerializer:
97
+
98
+ ```ruby
99
+ class UserSerializer
100
+ attr_reader :user
101
+
102
+ def initialize(user)
103
+ @user = user
104
+ end
105
+
106
+ def to_h
107
+ { id: user.id, full_name: user.full_name, email: user.email }
108
+ end
109
+ end
110
+ ```
111
+
112
+ You can thenuse stathamnize with different traits in your spec file.
113
+
114
+ ```ruby
115
+ RSpec.describe UserSerializer do
116
+ describe "Schema" do
117
+ subject { stathamnize(trait) { serializer }.success? }
118
+
119
+ context "Given a valid user" do
120
+ let(:serializer) { UserSerializer.new(user).to_h }
121
+ let(:trait) { "user_serializer/valid-user" }
122
+ let(:user) { create(:user, :valid) }
123
+
124
+ it "has a valid schema" do
125
+ expect(subject).to eq(true)
126
+ end
127
+ end
128
+
129
+ context "Given an invalid user" do
130
+ let(:serializer) { UserSerializer.new(user).to_h }
131
+ let(:trait) { "user_serializer/invalid-user" }
132
+ let(:user) { create(:user, :invalid) }
133
+
134
+ it "has a valid schema" do
135
+ expect(subject).to eq(true)
136
+ end
137
+ end
138
+ end
139
+ end
140
+ ```
141
+
142
+ ## Development
143
+
144
+ 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.
145
+
146
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
147
+
148
+ ## Contributing
149
+
150
+ Bug reports and pull requests are welcome on GitHub at https://github.com/victorauthiat/json_statham. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/victorauthiat/json_statham/blob/master/CODE_OF_CONDUCT.md).
151
+
152
+ ## License
153
+
154
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
155
+
156
+ ## Code of Conduct
157
+
158
+ Everyone interacting in the JsonStatham project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/victorauthiat/json_statham/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonStatham
4
+ class Config
5
+ attr_reader :schemas_path, :store_schema, :logger
6
+
7
+ def initialize
8
+ @schemas_path = nil
9
+ @store_schema = nil
10
+ @logger = nil
11
+ end
12
+
13
+ def store_schema?
14
+ !!store_schema
15
+ end
16
+
17
+ def logger?
18
+ !!logger
19
+ end
20
+
21
+ def schemas_path_present?
22
+ !!schemas_path
23
+ end
24
+
25
+ def schemas_path=(value)
26
+ Validation.check_object_class(value, [String])
27
+
28
+ @schemas_path = value
29
+ end
30
+
31
+ def store_schema=(value)
32
+ Validation.check_object_class(value, [TrueClass, FalseClass, NilClass])
33
+
34
+ @store_schema = value
35
+ end
36
+
37
+ def logger=(value)
38
+ Validation.check_object_class(value, [TrueClass, FalseClass, NilClass])
39
+
40
+ @logger = value
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonStatham
4
+ module Helper
5
+ class << self
6
+ ["included", "extended"].each do |expand|
7
+ define_method(expand) do |base|
8
+ base.extend HelperMethod
9
+ base.include HelperMethod
10
+ end
11
+ end
12
+ end
13
+
14
+ module HelperMethod
15
+ def stathamnize(name, &block)
16
+ ensure_valid_config
17
+
18
+ JsonStatham::Parser.call(name, &block)
19
+ end
20
+
21
+ private
22
+
23
+ def ensure_valid_config
24
+ return if JsonStatham.config.schemas_path_present?
25
+
26
+ raise ArgumentError, "JsonStatham::Config#chemas_path can't be blank."
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonStatham
4
+ class Parser
5
+ attr_reader :name, :block, :reader, :observer
6
+
7
+ def self.call(name, &block)
8
+ new(name, &block).call
9
+ end
10
+
11
+ def initialize(name, &block)
12
+ Validation.check_object_class(name, [String])
13
+
14
+ @name = name
15
+ @block = block
16
+ end
17
+
18
+ def call
19
+ @reader = JsonStatham::Requests::Reader.call(self)
20
+ @observer = JsonStatham::Requests::Observer.call(self)
21
+
22
+ store_current_schema
23
+
24
+ JsonStatham::Result.call(self)
25
+ end
26
+
27
+ def store_current_schema
28
+ return unless JsonStatham.config.store_schema?
29
+
30
+ JsonStatham::Requests::Writer.call(self)
31
+ end
32
+
33
+ def schema
34
+ observer&.data
35
+ end
36
+
37
+ def stored_schema
38
+ return unless reader?
39
+
40
+ reader["schema"]
41
+ end
42
+
43
+ def previous_duration
44
+ return unless reader?
45
+
46
+ reader["duration"]
47
+ end
48
+
49
+ def reader?
50
+ !!reader
51
+ end
52
+
53
+ def current_schema
54
+ JsonStatham::Schema.call(schema)
55
+ end
56
+
57
+ def stored_schema?
58
+ !!stored_schema
59
+ end
60
+
61
+ def valid?
62
+ !stored_schema? || (stored_schema == current_schema)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ module JsonStatham
6
+ module Requests
7
+ class Base
8
+ extend Forwardable
9
+
10
+ attr_reader :parser
11
+
12
+ def_delegators :@parser, :name, :block, :current_schema, :observer
13
+
14
+ def_delegators :@config, :schemas_path
15
+
16
+ def self.call(parser)
17
+ new(parser).call
18
+ end
19
+
20
+ def initialize(parser)
21
+ Validation.check_object_class(parser, [JsonStatham::Parser])
22
+
23
+ @parser = parser
24
+ @config = JsonStatham.config
25
+ end
26
+
27
+ def base_path
28
+ return "#{schemas_path}/#{folder_path}" if folder?
29
+
30
+ schemas_path
31
+ end
32
+
33
+ def folder?
34
+ !!folder_path
35
+ end
36
+
37
+ def file_path
38
+ "#{base_path}/#{schema_name}.json"
39
+ end
40
+
41
+ def schema_name
42
+ splitted_name.last
43
+ end
44
+
45
+ def folder_path
46
+ return if splitted_name.length == 1
47
+
48
+ splitted_name.reverse.drop(1).reverse.join("/")
49
+ end
50
+
51
+ def splitted_name
52
+ name.split("/")
53
+ end
54
+
55
+ def call
56
+ raise NotImplementedError
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonStatham
4
+ module Requests
5
+ class Observer < Base
6
+ attr_reader :starting, :ending, :data, :duration
7
+
8
+ def self.clock_gettime
9
+ Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_second)
10
+ end
11
+
12
+ def call
13
+ @starting = clock_gettime
14
+ @data = block.call
15
+ @ending = clock_gettime
16
+ @duration = ending - starting
17
+
18
+ self
19
+ end
20
+
21
+ def clock_gettime
22
+ self.class.clock_gettime
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonStatham
4
+ module Requests
5
+ class Reader < Base
6
+ def call
7
+ return {} unless File.exist?(file_path)
8
+
9
+ JSON.parse(File.read(file_path))
10
+ rescue JSON::ParserError
11
+ {}
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonStatham
4
+ module Requests
5
+ class Writer < Base
6
+ def call
7
+ FileUtils.mkdir_p(base_path)
8
+
9
+ File.write(file_path, JSON.dump(dump))
10
+ end
11
+
12
+ def dump
13
+ { schema: current_schema, duration: observer.duration }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module JsonStatham
6
+ module Requests
7
+ autoload :Base, "json_statham/requests/base"
8
+ autoload :Writer, "json_statham/requests/writer"
9
+ autoload :Reader, "json_statham/requests/reader"
10
+ autoload :Observer, "json_statham/requests/observer"
11
+ end
12
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonStatham
4
+ class Result
5
+ attr_reader :parser
6
+
7
+ def self.call(parser)
8
+ new(parser).call
9
+ end
10
+
11
+ def initialize(parser)
12
+ Validation.check_object_class(parser, [JsonStatham::Parser])
13
+
14
+ @parser = parser
15
+ end
16
+
17
+ def call
18
+ if JsonStatham.config.logger?
19
+ puts <<-RESULT_MSG
20
+ Previous duration: #{previous_duration}
21
+ Current duration: #{current_duration}
22
+ RESULT_MSG
23
+ end
24
+
25
+ self
26
+ end
27
+
28
+ def current_duration
29
+ return unless observed?
30
+
31
+ parser.observer.duration
32
+ end
33
+
34
+ def observed?
35
+ !!parser.observer
36
+ end
37
+
38
+ def previous_duration
39
+ parser.previous_duration
40
+ end
41
+
42
+ def success?
43
+ parser.valid?
44
+ end
45
+
46
+ def failure?
47
+ !success?
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonStatham
4
+ class Schema
5
+ attr_reader :object
6
+
7
+ def self.call(object)
8
+ new(object).call
9
+ end
10
+
11
+ def initialize(object)
12
+ @object = object
13
+ end
14
+
15
+ def call
16
+ return array_map if object.is_a?(Array)
17
+ return transformed_hash if object.is_a?(Hash)
18
+
19
+ object.class.name.downcase
20
+ end
21
+
22
+ def transformed_hash
23
+ return unless object.is_a?(Hash)
24
+
25
+ object
26
+ .transform_keys(&:to_s)
27
+ .transform_values { |obj| self.class.call(obj) }
28
+ end
29
+
30
+ def array_map
31
+ return unless object.is_a?(Array)
32
+
33
+ object.map { |obj| self.class.call(obj) }
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonStatham
4
+ module Validation
5
+ def self.check_object_class(object, expected_classes = [])
6
+ return if expected_classes.include?(object.class)
7
+
8
+ raise ArgumentError,
9
+ "Expect #{object} class to be #{expected_classes.join(', ')}"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonStatham
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "json_statham/helper"
4
+ require_relative "json_statham/version"
5
+ require_relative "json_statham/requests"
6
+ require_relative "json_statham/validation"
7
+
8
+ module JsonStatham
9
+ autoload :Config, "json_statham/config"
10
+ autoload :Parser, "json_statham/parser"
11
+ autoload :Schema, "json_statham/schema"
12
+ autoload :Result, "json_statham/result"
13
+
14
+ class << self
15
+ def configure
16
+ yield config
17
+ end
18
+
19
+ def config
20
+ @_config ||= Config.new
21
+ end
22
+
23
+ def extended(base)
24
+ base.include Helper
25
+ end
26
+
27
+ def included(base)
28
+ base.extend Helper
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ module JsonStatham
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_statham
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Victor
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: JsonStatham allows to check the structure changes in a json
14
+ email:
15
+ - authiatv@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - ".simplecov"
23
+ - CHANGELOG.md
24
+ - CODE_OF_CONDUCT.md
25
+ - Gemfile
26
+ - Gemfile.lock
27
+ - LICENSE.txt
28
+ - README.md
29
+ - Rakefile
30
+ - lib/json_statham.rb
31
+ - lib/json_statham/config.rb
32
+ - lib/json_statham/helper.rb
33
+ - lib/json_statham/parser.rb
34
+ - lib/json_statham/requests.rb
35
+ - lib/json_statham/requests/base.rb
36
+ - lib/json_statham/requests/observer.rb
37
+ - lib/json_statham/requests/reader.rb
38
+ - lib/json_statham/requests/writer.rb
39
+ - lib/json_statham/result.rb
40
+ - lib/json_statham/schema.rb
41
+ - lib/json_statham/validation.rb
42
+ - lib/json_statham/version.rb
43
+ - sig/json_statham.rbs
44
+ homepage: https://github.com/victorauthiat/json_statham
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.6.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.3.7
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Ruby library to check json schema changes
67
+ test_files: []