json_parser 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +4 -1
- data/README.md +52 -2
- data/json_parser.gemspec +1 -0
- data/lib/json_parser.rb +2 -2
- data/lib/json_parser/class_methods/builder.rb +1 -18
- data/lib/json_parser/crawler.rb +1 -1
- data/lib/json_parser/fetcher.rb +1 -1
- data/lib/{concerns → json_parser}/type_cast.rb +1 -1
- data/lib/json_parser/version.rb +1 -1
- data/lib/json_parser/wrapper.rb +2 -2
- metadata +17 -4
- data/lib/concerns/options_parser.rb +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e1c0ec98b84cd4dbc53f82baaa96fc9afd618e38
|
4
|
+
data.tar.gz: 78537b728d6fa5882fa5c269a275e1b7ab7814ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6d2d5d2de986c62f3756c508b315d2406925f67f77d76e66c40d69304a386dc52392c10928a9e2360ea7db3143929e8bc0c3c327814f1a3f3f3761a60d44b42
|
7
|
+
data.tar.gz: a8dd328d05d745eec9a79ba26fdd5978aff4fe9c831164e0cddfeccf961ba3851f629702ea683bb58c0470fe48b45a2b503406e489aac021e4ca9cb1a21426f6
|
data/Gemfile.lock
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
json_parser (1.
|
4
|
+
json_parser (1.1.0)
|
5
5
|
activesupport
|
6
|
+
concern_builder
|
6
7
|
|
7
8
|
GEM
|
8
9
|
remote: https://rubygems.org/
|
@@ -13,6 +14,8 @@ GEM
|
|
13
14
|
minitest (~> 5.1)
|
14
15
|
thread_safe (~> 0.3, >= 0.3.4)
|
15
16
|
tzinfo (~> 1.1)
|
17
|
+
concern_builder (0.0.1)
|
18
|
+
activesupport
|
16
19
|
diff-lcs (1.2.5)
|
17
20
|
docile (1.1.5)
|
18
21
|
i18n (0.7.0)
|
data/README.md
CHANGED
@@ -1,2 +1,52 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
Json Parser
|
2
|
+
========
|
3
|
+
|
4
|
+
This project allows for a quick hash / json data fetching in order to avoid code
|
5
|
+
that tries to crawl through a hash and has to constantly check for nil values or missing keys
|
6
|
+
|
7
|
+
also, this concern, like openstruct, allow the json to be manipulated as an object, but
|
8
|
+
avoids method missing by aways having the declarated methods, even if that means nil return
|
9
|
+
|
10
|
+
Json Parser is also usefull when you need keys case changed or data type cast
|
11
|
+
|
12
|
+
Getting started
|
13
|
+
---------------
|
14
|
+
1. Add JsonParser to your `Gemfile` and `bundle install`:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'json_parser'
|
18
|
+
```
|
19
|
+
|
20
|
+
2. Include in a class that you want to wrap a json/hash
|
21
|
+
```ruby
|
22
|
+
class Parser
|
23
|
+
include JsonParser
|
24
|
+
|
25
|
+
attr_reader :json
|
26
|
+
|
27
|
+
def initialize(json)
|
28
|
+
@json = json
|
29
|
+
end
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
3. Declare the keys you want to crawl
|
34
|
+
```ruby
|
35
|
+
class Parser
|
36
|
+
json_parse :id, :dog_name, cached: true
|
37
|
+
json_parse :age, type: :integer
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
Options
|
42
|
+
-------
|
43
|
+
- path: path where to find the sub hash that contains the key (empty by default)
|
44
|
+
- json: method that contains the hash to be parsed (json by default)
|
45
|
+
- full_path: full path to fetch the value (empty by default)
|
46
|
+
- cached: indicator that once the value has been fetched, it should be cached (false by default)
|
47
|
+
- class: class to be used when wrapping the final value
|
48
|
+
- compact: indicator telling to ignore nil values inside array (false by default)
|
49
|
+
- flatten: indicator telling that to flattern the resulting array (false by default)
|
50
|
+
- after: name of a method to be called after with the resulting value
|
51
|
+
- case: case of the keys from the json (camel by default)
|
52
|
+
- type: Type that the value must be cast into
|
data/json_parser.gemspec
CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
20
|
spec.add_runtime_dependency 'activesupport'
|
21
|
+
spec.add_runtime_dependency 'concern_builder'
|
21
22
|
|
22
23
|
spec.add_development_dependency 'safe_attribute_assignment'
|
23
24
|
spec.add_development_dependency "bundler", "~> 1.6"
|
data/lib/json_parser.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'active_support'
|
2
2
|
require 'active_support/all'
|
3
|
+
require 'concern_builder'
|
3
4
|
|
4
5
|
module JsonParser
|
5
6
|
extend ActiveSupport::Concern
|
6
7
|
|
7
|
-
require '
|
8
|
-
require 'concerns/options_parser'
|
8
|
+
require 'json_parser/type_cast'
|
9
9
|
require 'json_parser/version'
|
10
10
|
require 'json_parser/crawler'
|
11
11
|
require 'json_parser/wrapper'
|
@@ -1,24 +1,7 @@
|
|
1
|
-
class JsonParser::ClassMethods::Builder
|
2
|
-
include OptionsParser
|
3
|
-
|
4
|
-
attr_reader :attr_names, :methods_def
|
1
|
+
class JsonParser::ClassMethods::Builder < ConcernBuilder
|
5
2
|
|
6
3
|
delegate :path, :cached, :compact, :type, :after, to: :options_object
|
7
4
|
|
8
|
-
def initialize(attr_names, instance, options)
|
9
|
-
@attr_names = attr_names
|
10
|
-
@instance = instance
|
11
|
-
@options = options
|
12
|
-
@methods_def = []
|
13
|
-
init
|
14
|
-
end
|
15
|
-
|
16
|
-
def build
|
17
|
-
methods_def.each do |method_def|
|
18
|
-
@instance.module_eval(method_def, __FILE__, __LINE__ + 1)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
5
|
private
|
23
6
|
|
24
7
|
def init
|
data/lib/json_parser/crawler.rb
CHANGED
data/lib/json_parser/fetcher.rb
CHANGED
data/lib/json_parser/version.rb
CHANGED
data/lib/json_parser/wrapper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bidu Dev's Team
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06
|
11
|
+
date: 2015-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: concern_builder
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: safe_attribute_assignment
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -110,13 +124,12 @@ files:
|
|
110
124
|
- README.md
|
111
125
|
- Rakefile
|
112
126
|
- json_parser.gemspec
|
113
|
-
- lib/concerns/options_parser.rb
|
114
|
-
- lib/concerns/type_cast.rb
|
115
127
|
- lib/json_parser.rb
|
116
128
|
- lib/json_parser/class_methods.rb
|
117
129
|
- lib/json_parser/class_methods/builder.rb
|
118
130
|
- lib/json_parser/crawler.rb
|
119
131
|
- lib/json_parser/fetcher.rb
|
132
|
+
- lib/json_parser/type_cast.rb
|
120
133
|
- lib/json_parser/version.rb
|
121
134
|
- lib/json_parser/wrapper.rb
|
122
135
|
- spec/fixtures/json_parser.json
|