flexi-json 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 831ab0ef38dfd154ec606fe74ab9c8600ff434b25a9db6cec199176835292081
4
- data.tar.gz: 6b48b8cba93525b3b905a59baceda79e48df3b0165dcddafc2c27a1d38ba8d22
3
+ metadata.gz: f80c4f6ead357e1dfb161b0dc6cca110858e7698b4f8cd875bc40e16d0cade14
4
+ data.tar.gz: a66e1baae920f9e1e2c0a6ab2d448e35a71a1a8d337519ad5cd625cda6eefa69
5
5
  SHA512:
6
- metadata.gz: d09892eb341686fedd577f716dac6c326ed92df32a04fc8bb98aee39b029a2afecb575bd0943bfb7adfdcb5f4742ff8472484ac53602b25c75aff769b97f3a1d
7
- data.tar.gz: d676046637dd8a87ea6b3ab842e70cfcde82fb784a425b6d0c41b4e7acc980c2a9711c48df06dbc7d01e86df5f9950ae3c936c13d8fdb8b44af2fe69946f8fc5
6
+ metadata.gz: a47e6dfe5515d08453ff79d78b224b0c4037450bc3c808e28d214b65c783fc57a46bd7282a4689c692de80fecb23fda6ff582b65988b432116de415a684a9898
7
+ data.tar.gz: da908945d06dd9a524587ec7665a14e061db30241defca07e882b7a7bac9d3943e2461a283034bba46c099a04bf29ce207218896ddeb940f924965a59fea55cb
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,31 @@ 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
+
34
+ # Load a json data from aurl
35
+ flexi_json = Flexi::Json::Run.new("https://raw.githubusercontent.com/GD-Personal/flexi-json/main/spec/data/dataset.json")
36
+
37
+ # Search for data
38
+ flexi_json.search("john")
30
39
 
31
40
  # 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")
41
+ flexi_json.search("john", "first_name")
42
+ flexi_json.search("john", "first_name,email")
34
43
 
35
44
  # 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")
45
+ flexi_json.find_duplicates("email")
46
+ flexi_json.find_duplicates("email,full_name")
38
47
  ```
39
48
 
40
49
  ## TODOS
41
50
  - 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
51
  - Add CRUD support to the dataset
45
- - Optimise the search function by implimenting indeces
52
+ - Optimise the search function by implementing indeces
46
53
  - Optimise the loader by chunking the data
47
54
 
48
55
  ## Contributing
@@ -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
@@ -22,7 +22,7 @@ module Flexi
22
22
  return [] if filtered_fields.empty?
23
23
 
24
24
  grouped_data = @data.group_by do |d|
25
- filtered_fields.map { |f| d.attributes[f].to_s.downcase }
25
+ filtered_fields.map { |f| d.attributes[f.to_sym].to_s.downcase }
26
26
  end
27
27
  grouped_data.each do |key, value|
28
28
  duplicates[key] = value if value.size > 1
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Flexi
4
4
  module Json
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
data/lib/flexi/json.rb CHANGED
@@ -10,7 +10,7 @@ module Flexi::Json
10
10
 
11
11
  class Run
12
12
  # Your code goes here...
13
- def initialize(data, options = {})
13
+ def initialize(data)
14
14
  @json_data = Flexi::Json::Loader.new(data).load_data
15
15
  end
16
16
 
metadata CHANGED
@@ -1,7 +1,7 @@
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.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerda Decio
@@ -9,7 +9,77 @@ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
11
  date: 2024-09-20 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: standard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: simplecov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.22.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.22.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov_json_formatter
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.1.4
69
+ - !ruby/object:Gem::Dependency
70
+ name: github_changelog_generator
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.16.4
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.16.4
13
83
  description: A versatile Ruby gem designed for manipulating JSON data. With functionalities
14
84
  for searching, generating new JSON, and transforming existing structures.
15
85
  email:
@@ -38,7 +108,7 @@ metadata:
38
108
  allowed_push_host: https://rubygems.org
39
109
  homepage_uri: https://github.com/GD-Personal/flexi-json
40
110
  source_code_uri: https://github.com/GD-Personal/flexi-json
41
- changelog_uri: https://github.com/GD-Personal/flexi/blob/main/CHANGELOG
111
+ changelog_uri: https://github.com/GD-Personal/flexi-json/blob/main/CHANGELOG.md
42
112
  post_install_message:
43
113
  rdoc_options: []
44
114
  require_paths: