flexi-json 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 831ab0ef38dfd154ec606fe74ab9c8600ff434b25a9db6cec199176835292081
4
- data.tar.gz: 6b48b8cba93525b3b905a59baceda79e48df3b0165dcddafc2c27a1d38ba8d22
3
+ metadata.gz: b10a91e8b4e687fdd0c69e73b8a53d6f1e66e96361fe08259c7244aec87e638b
4
+ data.tar.gz: 2f7a7dd6d9acc9069c3c1884ff98736041113889c8cd35efa5c94bd7f6acd48e
5
5
  SHA512:
6
- metadata.gz: d09892eb341686fedd577f716dac6c326ed92df32a04fc8bb98aee39b029a2afecb575bd0943bfb7adfdcb5f4742ff8472484ac53602b25c75aff769b97f3a1d
7
- data.tar.gz: d676046637dd8a87ea6b3ab842e70cfcde82fb784a425b6d0c41b4e7acc980c2a9711c48df06dbc7d01e86df5f9950ae3c936c13d8fdb8b44af2fe69946f8fc5
6
+ metadata.gz: 1f793341d32d74087ce18d669f0df367e1503a081a4e227cb8dbda80e8e3a64fdc02b9bd7324b69738bde9b86aeb6f70c287ea75db0bd3accafff23ea08216b5
7
+ data.tar.gz: dbd6b4bf1f222c4fb657ab346057ae975073397b7c5a4d84c32f0589b02e9c905be510ae6ea10c49b19515a312f19ef950bc64e0573268f80172ca185e8fa896
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ # Version: 0.3.0 - 2024-09-22
2
+
3
+ * [#10](https://github.com/GD-Personal/flexi-json/pull/10): Release 0.2.0
4
+ * [#11](https://github.com/GD-Personal/flexi-json/pull/11): v0.3.0 - Advanced search
5
+
6
+
7
+ # Version: 0.2.0 - 2024-09-21
8
+
9
+ * [#1](https://github.com/GD-Personal/flexi-json/pull/1): Add support for different data input to the loader
10
+ * [#2](https://github.com/GD-Personal/flexi-json/pull/2): Add the github_changelog_generator gem
11
+ * [#3](https://github.com/GD-Personal/flexi-json/pull/3): Update changelog workflow
12
+ * [#4](https://github.com/GD-Personal/flexi-json/pull/4): Add token
13
+ * [#5](https://github.com/GD-Personal/flexi-json/pull/5): Add token
14
+ * [#6](https://github.com/GD-Personal/flexi-json/pull/6): Rename json_data to datasets
15
+
16
+
1
17
  ## CHANGELOG
2
18
 
3
19
  ## [0.1.0] - 2024-09-20
data/README.md CHANGED
@@ -1,4 +1,4 @@
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)
1
+ [![Gem Version](https://badge.fury.io/rb/flexi-json.svg)](https://badge.fury.io/rb/flexi-json) [![Github Actions](https://github.com/GD-Personal/flexi-json/actions/workflows/ci.yml/badge.svg)](https://github.com/GD-Personal/flexi-json/actions) [![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
2
 
3
3
  # flexi-json
4
4
 
@@ -25,24 +25,39 @@ bundle install
25
25
  ```ruby
26
26
  require 'flexi/json'
27
27
 
28
- # File path to the JSON data
29
- Flexi::Json::Run.new("some/path/to/file.json").search("john")
28
+ # Load a json data from a local file
29
+ flexi_json = Flexi::Json::Run.new("some/path/to/file.json")
30
+
31
+ # Load a raw json data
32
+ flexi_json = Flexi::Json::Run.new("{\"name\":\"John\",\"address\":\"Sydney Australia\"}")
33
+ # => <Flexi::Json::Run:0x0000ffffa0f5fc58 @datasets=[#<Flexi::Json::Dataset:0x0000ffffa0f5f668 @address="Sydney Australia", @attributes={:name=>"John", :address=>"Sydney Australia"}, @name="John", @searchable_fields=["name", "address"]>]>
34
+
35
+ # Load a json data from aurl
36
+ flexi_json = Flexi::Json::Run.new("https://raw.githubusercontent.com/GD-Personal/flexi-json/main/spec/data/dataset.json")
37
+
38
+ # Search for data
39
+ flexi_json.search("john")
30
40
 
31
41
  # 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")
42
+ flexi_json.search("john", "first_name")
43
+ flexi_json.search("john", "first_name,email")
34
44
 
35
45
  # 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")
46
+ flexi_json.find_duplicates("email")
47
+ flexi_json.find_duplicates("email,full_name")
48
+ ```
49
+
50
+ ## Advanced search
51
+ ```ruby
52
+ options = {matched_all: true, exact_match: false}
53
+ flexi_json.search({first_name: "john", address: "sydney"}, options)
54
+ # => [#<Flexi::Json::Dataset:0x0000ffffa0f5f668 @address="Sydney Australia", @attributes={:name=>"John", :address=>"Sydney Australia"}, @name="John", @searchable_fields=["name", "address"]>]
38
55
  ```
39
56
 
40
57
  ## TODOS
41
58
  - 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
59
  - Add CRUD support to the dataset
45
- - Optimise the search function by implimenting indeces
60
+ - Optimise the search function by implementing indeces
46
61
  - Optimise the loader by chunking the data
47
62
 
48
63
  ## Contributing
@@ -0,0 +1,29 @@
1
+ changelog_type: 'commit_message' # or 'pull_request'
2
+ header_prefix: 'Version:'
3
+ commit_changelog: true
4
+ comment_changelog: true
5
+ include_unlabeled_changes: true
6
+ unlabeled_group_title: 'Unlabeled Changes'
7
+ pull_request_title_regex: '^Release'
8
+ version_regex: 'v?([0-9]{1,2})+[.]+([0-9]{1,2})+[.]+([0-9]{1,2})\s\(\d{1,2}-\d{1,2}-\d{4}\)'
9
+ exclude_labels:
10
+ - bot
11
+ - dependabot
12
+ - ci
13
+ group_config:
14
+ - title: Bug Fixes
15
+ labels:
16
+ - bug
17
+ - bugfix
18
+ - title: Code Improvements
19
+ labels:
20
+ - improvements
21
+ - enhancement
22
+ - title: New Features
23
+ labels:
24
+ - feature
25
+ - title: Documentation Updates
26
+ labels:
27
+ - docs
28
+ - documentation
29
+ - doc
@@ -1,3 +1,4 @@
1
+ require "debug"
1
2
  module Flexi
2
3
  module Json
3
4
  class Dataset
@@ -5,7 +6,6 @@ module Flexi
5
6
 
6
7
  def initialize(attributes = {})
7
8
  @attributes = attributes
8
-
9
9
  @attributes.each do |key, value|
10
10
  validate_key(key)
11
11
  set_instance_variable(key, value)
@@ -13,15 +13,20 @@ module Flexi
13
13
  end
14
14
  end
15
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
16
+ def matches?(query, fields = searchable_fields, options: Searcher::DEFAULT_MATCH_OPTIONS)
17
+ validateable_fields = query.is_a?(Hash) ? query.keys.map(&:to_s) : fields
18
+ valid_fields = validateable_fields&.select { |field| searchable_fields.include?(field) } || searchable_fields
19
19
 
20
20
  return false if valid_fields.empty?
21
21
 
22
- valid_fields.any? do |field|
23
- value = send(field)
24
- value.to_s.downcase.include?(query.to_s.downcase)
22
+ matching_method = options[:matched_all] ? :all? : :any?
23
+ valid_fields.public_send(matching_method) do |field|
24
+ search_query = query.is_a?(Hash) ? query[field.to_sym] : query
25
+ if options[:exact_match]
26
+ attributes[field.to_sym].to_s.downcase == search_query.to_s.downcase
27
+ else
28
+ attributes[field.to_sym].to_s.downcase.include?(search_query.to_s.downcase)
29
+ end
25
30
  end
26
31
  end
27
32
 
@@ -1,19 +1,40 @@
1
+ require "net/http"
2
+ require "uri"
3
+
1
4
  module Flexi
2
5
  module Json
3
6
  class Loader
4
- def initialize(data_file_path)
5
- @data_file_path = data_file_path
7
+ def initialize(data)
8
+ @data = data
9
+ end
10
+
11
+ def load_data
12
+ loaded_data = load_from_local_file || load_from_raw_json || load_from_url || []
13
+ loaded_data.map { |result| Dataset.new(result.transform_keys(&:to_sym)) }
14
+ end
15
+
16
+ private
17
+
18
+ def load_from_raw_json(raw_json = nil)
19
+ data = JSON.parse(raw_json || @data)
20
+ data.is_a?(Array) ? data : [data]
21
+ rescue JSON::ParserError, TypeError
22
+ nil
23
+ end
24
+
25
+ def load_from_local_file
26
+ data = File.read(@data)
27
+ load_from_raw_json(data)
28
+ rescue Errno::ENOENT, TypeError
29
+ nil
6
30
  end
7
31
 
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
- []
32
+ def load_from_url
33
+ uri = URI.parse(@data)
34
+ response = Net::HTTP.get_response(uri)
35
+ response.is_a?(Net::HTTPSuccess) ? load_from_raw_json(response.body) : nil
36
+ rescue URI::InvalidURIError, Errno::ECONNREFUSED
37
+ nil
17
38
  end
18
39
  end
19
40
  end
@@ -3,7 +3,12 @@ require "json"
3
3
  module Flexi
4
4
  module Json
5
5
  class Searcher
6
- attr_reader :data
6
+ attr_reader :data, :result
7
+
8
+ DEFAULT_MATCH_OPTIONS = {
9
+ matched_all: false,
10
+ exact_match: false
11
+ }.freeze
7
12
 
8
13
  def initialize(data)
9
14
  @data = data
@@ -11,7 +16,7 @@ module Flexi
11
16
  end
12
17
 
13
18
  def search(query, fields = nil)
14
- @data.select { |data| data.matches?(query, fields) }
19
+ @result = @data.select { |data| data.matches?(query, fields) }
15
20
  end
16
21
 
17
22
  def find_duplicates(keys)
@@ -22,17 +27,17 @@ module Flexi
22
27
  return [] if filtered_fields.empty?
23
28
 
24
29
  grouped_data = @data.group_by do |d|
25
- filtered_fields.map { |f| d.attributes[f].to_s.downcase }
30
+ filtered_fields.map { |f| d.attributes[f.to_sym].to_s.downcase }
26
31
  end
27
32
  grouped_data.each do |key, value|
28
33
  duplicates[key] = value if value.size > 1
29
34
  end
30
35
 
31
- duplicates.values.flatten
36
+ @result = duplicates.values.flatten
32
37
  end
33
38
 
34
39
  # Displays results to the console
35
- def display_results(results, output = $stdout)
40
+ def display_results(results = @result, output = $stdout)
36
41
  if results.empty?
37
42
  output.puts "No data found."
38
43
  else
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Flexi
4
4
  module Json
5
- VERSION = "0.1.0"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
data/lib/flexi/json.rb CHANGED
@@ -10,16 +10,21 @@ module Flexi::Json
10
10
 
11
11
  class Run
12
12
  # Your code goes here...
13
- def initialize(data, options = {})
14
- @json_data = Flexi::Json::Loader.new(data).load_data
13
+ def initialize(data)
14
+ datasets = Flexi::Json::Loader.new(data).load_data
15
+ @searcher = Flexi::Json::Searcher.new(datasets)
15
16
  end
16
17
 
17
18
  def search(query = "", fields = nil)
18
- Flexi::Json::Searcher.new(@json_data).search(query, fields)
19
+ @searcher.search(query, fields)
19
20
  end
20
21
 
21
22
  def find_duplicates(keys)
22
- Flexi::Json::Searcher.new(@json_data).find_duplicates(keys)
23
+ @searcher.find_duplicates(keys)
24
+ end
25
+
26
+ def display_results
27
+ @searcher.display_results
23
28
  end
24
29
  end
25
30
  end
metadata CHANGED
@@ -1,15 +1,113 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexi-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerda Decio
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-09-20 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-09-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '13.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '13.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundle-audit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.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: standard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.22.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.22.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov_json_formatter
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.1.4
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.1.4
97
+ - !ruby/object:Gem::Dependency
98
+ name: debug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.9.2
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.9.2
13
111
  description: A versatile Ruby gem designed for manipulating JSON data. With functionalities
14
112
  for searching, generating new JSON, and transforming existing structures.
15
113
  email:
@@ -25,6 +123,7 @@ files:
25
123
  - LICENSE.txt
26
124
  - README.md
27
125
  - Rakefile
126
+ - changelog_config.yml
28
127
  - lib/flexi/json.rb
29
128
  - lib/flexi/json/dataset.rb
30
129
  - lib/flexi/json/loader.rb
@@ -36,9 +135,10 @@ licenses:
36
135
  - MIT
37
136
  metadata:
38
137
  allowed_push_host: https://rubygems.org
138
+ bug_tracker_uri: https://github.com/GD-Personal/flexi-json/issues
39
139
  homepage_uri: https://github.com/GD-Personal/flexi-json
40
140
  source_code_uri: https://github.com/GD-Personal/flexi-json
41
- changelog_uri: https://github.com/GD-Personal/flexi/blob/main/CHANGELOG
141
+ changelog_uri: https://github.com/GD-Personal/flexi-json/blob/main/CHANGELOG.md
42
142
  post_install_message:
43
143
  rdoc_options: []
44
144
  require_paths:
@@ -47,7 +147,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
47
147
  requirements:
48
148
  - - ">="
49
149
  - !ruby/object:Gem::Version
50
- version: 3.0.0
150
+ version: 3.2.2
51
151
  required_rubygems_version: !ruby/object:Gem::Requirement
52
152
  requirements:
53
153
  - - ">="