flexi-json 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -10
- data/lib/flexi/json/loader.rb +32 -11
- data/lib/flexi/json/searcher.rb +1 -1
- data/lib/flexi/json/version.rb +1 -1
- data/lib/flexi/json.rb +1 -1
- metadata +73 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f80c4f6ead357e1dfb161b0dc6cca110858e7698b4f8cd875bc40e16d0cade14
|
4
|
+
data.tar.gz: a66e1baae920f9e1e2c0a6ab2d448e35a71a1a8d337519ad5cd625cda6eefa69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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/
|
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
|
-
#
|
29
|
-
Flexi::Json::Run.new("some/path/to/file.json")
|
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
|
-
|
33
|
-
|
41
|
+
flexi_json.search("john", "first_name")
|
42
|
+
flexi_json.search("john", "first_name,email")
|
34
43
|
|
35
44
|
# Find duplicate emails
|
36
|
-
|
37
|
-
|
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
|
52
|
+
- Optimise the search function by implementing indeces
|
46
53
|
- Optimise the loader by chunking the data
|
47
54
|
|
48
55
|
## Contributing
|
data/lib/flexi/json/loader.rb
CHANGED
@@ -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(
|
5
|
-
@
|
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
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
data/lib/flexi/json/searcher.rb
CHANGED
@@ -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
|
data/lib/flexi/json/version.rb
CHANGED
data/lib/flexi/json.rb
CHANGED
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.
|
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:
|