flexi-json 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: 831ab0ef38dfd154ec606fe74ab9c8600ff434b25a9db6cec199176835292081
4
+ data.tar.gz: 6b48b8cba93525b3b905a59baceda79e48df3b0165dcddafc2c27a1d38ba8d22
5
+ SHA512:
6
+ metadata.gz: d09892eb341686fedd577f716dac6c326ed92df32a04fc8bb98aee39b029a2afecb575bd0943bfb7adfdcb5f4742ff8472484ac53602b25c75aff769b97f3a1d
7
+ data.tar.gz: d676046637dd8a87ea6b3ab842e70cfcde82fb784a425b6d0c41b4e7acc980c2a9711c48df06dbc7d01e86df5f9950ae3c936c13d8fdb8b44af2fe69946f8fc5
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## CHANGELOG
2
+
3
+ ## [0.1.0] - 2024-09-20
4
+
5
+ - The first release of flexi-json gem
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 iamgeeeee
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,55 @@
1
+ [![Github Actions](https://github.com/GD-Personal/flexi-json/actions/workflows/main.yml/badge.svg)](https://github.com/GD-Personal/flexi-json/actions/workflows/main.yml) [![Maintainability](https://api.codeclimate.com/v1/badges/bd14f8a5a0c7575d2ac2/maintainability)](https://codeclimate.com/github/GD-Personal/flexi-json/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/bd14f8a5a0c7575d2ac2/test_coverage)](https://codeclimate.com/github/GD-Personal/flexi-json/test_coverage)
2
+
3
+ # flexi-json
4
+
5
+ `Flexi::Json` is a versatile Ruby gem designed for manipulating JSON data. With functionalities for searching, generating new JSON, and transforming existing structures.
6
+
7
+ ## Installation
8
+
9
+ You can install the gem using RubyGems:
10
+ ```
11
+ gem install flexi-json
12
+ ```
13
+
14
+ or add it to your Gemfile
15
+ ```
16
+ gem 'flexi-json'
17
+ ```
18
+
19
+ and then run:
20
+ ```
21
+ bundle install
22
+ ```
23
+
24
+ ## Usage
25
+ ```ruby
26
+ require 'flexi/json'
27
+
28
+ # File path to the JSON data
29
+ Flexi::Json::Run.new("some/path/to/file.json").search("john")
30
+
31
+ # Or filter it by your chosen key e.g first_name
32
+ Flexi::Json::Run.new("some/path/to/file.json").search("john", "first_name")
33
+ Flexi::Json::Run.new("some/path/to/file.json").search("john", "first_name,email")
34
+
35
+ # Find duplicate emails
36
+ Flexi::Json::Run.new("some/path/to/file.json").find_duplicates("email")
37
+ Flexi::Json::Run.new("some/path/to/file.json").find_duplicates("email,full_name")
38
+ ```
39
+
40
+ ## TODOS
41
+ - Improve search filter by specifying fields to filter from
42
+ - Add support for accepting a dataset url rather than just a local file path
43
+ - Add support for accepting raw json data
44
+ - Add CRUD support to the dataset
45
+ - Optimise the search function by implimenting indeces
46
+ - Optimise the loader by chunking the data
47
+
48
+ ## Contributing
49
+ Contributions are welcome! If you have suggestions for improvements or new features, feel free to fork the repository and create a pull request. Please ensure your code adheres to the project's coding standards and includes tests for new features.
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/GD-Personal/flexi-json. 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/GD-Personal/flexi-json/blob/main/CODE_OF_CONDUCT.md).
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,20 @@
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 "standard/rake"
9
+
10
+ task default: %i[spec lint]
11
+
12
+ desc "Run StandardRB linter and automatically fix issues"
13
+ task :lint do
14
+ sh "bundle exec standardrb --fix"
15
+ end
16
+
17
+ desc "Run bundle-audit to check for gems' vulnerabilities"
18
+ task :audit do
19
+ sh "bundle exec bundle-audit check --update"
20
+ end
@@ -0,0 +1,60 @@
1
+ module Flexi
2
+ module Json
3
+ class Dataset
4
+ attr_reader :attributes
5
+
6
+ def initialize(attributes = {})
7
+ @attributes = attributes
8
+
9
+ @attributes.each do |key, value|
10
+ validate_key(key)
11
+ set_instance_variable(key, value)
12
+ define_accessor_methods(key)
13
+ end
14
+ end
15
+
16
+ # Returns true if any of the client's fields match the search query
17
+ def matches?(query, fields = searchable_fields)
18
+ valid_fields = fields&.select { |field| searchable_fields.include?(field) } || searchable_fields
19
+
20
+ return false if valid_fields.empty?
21
+
22
+ valid_fields.any? do |field|
23
+ value = send(field)
24
+ value.to_s.downcase.include?(query.to_s.downcase)
25
+ end
26
+ end
27
+
28
+ def searchable_fields
29
+ @searchable_fields ||= attributes.keys.map(&:to_s)
30
+ end
31
+
32
+ private
33
+
34
+ # Method to validate that a key is a valid method name and not dangerous
35
+ def valid_key?(key)
36
+ key.match?(/\A[a-z_][a-zA-Z0-9_]*\z/) && !dangerous_method?(key)
37
+ end
38
+
39
+ # Prevent overriding critical Ruby methods
40
+ def dangerous_method?(key)
41
+ %w[initialize object_id method_missing to_s send class].include?(key.to_s)
42
+ end
43
+
44
+ def validate_key(key)
45
+ raise "Invalid key: #{key}" unless valid_key?(key)
46
+ end
47
+
48
+ def set_instance_variable(key, value)
49
+ instance_variable_set(:"@#{key}", value)
50
+ end
51
+
52
+ def define_accessor_methods(key)
53
+ self.class.class_eval do
54
+ define_method(key) { instance_variable_get(:"@#{key}") } unless method_defined?(key)
55
+ define_method(:"#{key}=") { |val| instance_variable_set(:"@#{key}", val) } unless method_defined?(:"#{key}=")
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,20 @@
1
+ module Flexi
2
+ module Json
3
+ class Loader
4
+ def initialize(data_file_path)
5
+ @data_file_path = data_file_path
6
+ end
7
+
8
+ def load_data(output = $stdout)
9
+ file = File.read(@data_file_path)
10
+ JSON.parse(file).map { |result| Dataset.new(result) }
11
+ rescue Errno::ENOENT
12
+ output.puts("Dataset file not found at #{@data_file_path}!")
13
+ []
14
+ rescue JSON::ParserError
15
+ output.puts("Invalid JSON!")
16
+ []
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,47 @@
1
+ require "json"
2
+
3
+ module Flexi
4
+ module Json
5
+ class Searcher
6
+ attr_reader :data
7
+
8
+ def initialize(data)
9
+ @data = data
10
+ @result = []
11
+ end
12
+
13
+ def search(query, fields = nil)
14
+ @data.select { |data| data.matches?(query, fields) }
15
+ end
16
+
17
+ def find_duplicates(keys)
18
+ duplicates = {}
19
+ fields = keys.gsub(/\s+/, "").strip.split(",")
20
+ filtered_fields = fields.select { |field| @data.first&.searchable_fields&.include?(field) }
21
+
22
+ return [] if filtered_fields.empty?
23
+
24
+ grouped_data = @data.group_by do |d|
25
+ filtered_fields.map { |f| d.attributes[f].to_s.downcase }
26
+ end
27
+ grouped_data.each do |key, value|
28
+ duplicates[key] = value if value.size > 1
29
+ end
30
+
31
+ duplicates.values.flatten
32
+ end
33
+
34
+ # Displays results to the console
35
+ def display_results(results, output = $stdout)
36
+ if results.empty?
37
+ output.puts "No data found."
38
+ else
39
+ results.each do |result|
40
+ output.puts result.attributes.map { |k, v| "#{k}: #{v}" }.join(", ")
41
+ end
42
+ output.puts "Found #{results.size} result(s)!"
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flexi
4
+ module Json
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
data/lib/flexi/json.rb ADDED
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "json/dataset"
4
+ require_relative "json/loader"
5
+ require_relative "json/searcher"
6
+ require_relative "json/version"
7
+
8
+ module Flexi::Json
9
+ class Error < StandardError; end
10
+
11
+ class Run
12
+ # Your code goes here...
13
+ def initialize(data, options = {})
14
+ @json_data = Flexi::Json::Loader.new(data).load_data
15
+ end
16
+
17
+ def search(query = "", fields = nil)
18
+ Flexi::Json::Searcher.new(@json_data).search(query, fields)
19
+ end
20
+
21
+ def find_duplicates(keys)
22
+ Flexi::Json::Searcher.new(@json_data).find_duplicates(keys)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,6 @@
1
+ module Flexi
2
+ module Json
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flexi-json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gerda Decio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-09-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A versatile Ruby gem designed for manipulating JSON data. With functionalities
14
+ for searching, generating new JSON, and transforming existing structures.
15
+ email:
16
+ - contact@gerdadecio.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rspec"
22
+ - ".standard.yml"
23
+ - CHANGELOG.md
24
+ - CODE_OF_CONDUCT.md
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - lib/flexi/json.rb
29
+ - lib/flexi/json/dataset.rb
30
+ - lib/flexi/json/loader.rb
31
+ - lib/flexi/json/searcher.rb
32
+ - lib/flexi/json/version.rb
33
+ - sig/flexi/json.rbs
34
+ homepage: https://github.com/GD-Personal/flexi-json
35
+ licenses:
36
+ - MIT
37
+ metadata:
38
+ allowed_push_host: https://rubygems.org
39
+ homepage_uri: https://github.com/GD-Personal/flexi-json
40
+ source_code_uri: https://github.com/GD-Personal/flexi-json
41
+ changelog_uri: https://github.com/GD-Personal/flexi/blob/main/CHANGELOG
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 3.0.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.5.14
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: A ruby gem designed for manipulating JSON data.
61
+ test_files: []