json_parser 1.0.0 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa3db49e4e788b1d0842fc1a76177a420dddea7a
4
- data.tar.gz: 2e53eee8b60b49d2c789adc5da2aa415b83528c9
3
+ metadata.gz: e1c0ec98b84cd4dbc53f82baaa96fc9afd618e38
4
+ data.tar.gz: 78537b728d6fa5882fa5c269a275e1b7ab7814ea
5
5
  SHA512:
6
- metadata.gz: c501a429649f8578db081111b1db7d07475f31645764277aed8f02ec834cece16b739fe5f5769ec41dfe46c02cd58db7e147fcf361a82114f616fa60b04c7264
7
- data.tar.gz: bdf68e4a5fb504178908b51bbc992028b0442c4f9009db32c1e85c6f9de29cc13aeb676529e07c9937691708b150364a094dfc9316e959b4c6807cafc5520a56
6
+ metadata.gz: f6d2d5d2de986c62f3756c508b315d2406925f67f77d76e66c40d69304a386dc52392c10928a9e2360ea7db3143929e8bc0c3c327814f1a3f3f3761a60d44b42
7
+ data.tar.gz: a8dd328d05d745eec9a79ba26fdd5978aff4fe9c831164e0cddfeccf961ba3851f629702ea683bb58c0470fe48b45a2b503406e489aac021e4ca9cb1a21426f6
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_parser (1.0.0)
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
- # json_parser
2
- Hash/Json Parser
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
@@ -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"
@@ -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 'concerns/type_cast'
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
@@ -1,5 +1,5 @@
1
1
  class JsonParser::Crawler
2
- include OptionsParser
2
+ include ConcernBuilder::OptionsParser
3
3
 
4
4
  attr_reader :post_process, :path
5
5
 
@@ -1,5 +1,5 @@
1
1
  class JsonParser::Fetcher
2
- include OptionsParser
2
+ include ConcernBuilder::OptionsParser
3
3
 
4
4
  attr_reader :path, :json, :instance
5
5
 
@@ -1,4 +1,4 @@
1
- module TypeCast
1
+ module JsonParser::TypeCast
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  def to_integer(value)
@@ -1,3 +1,3 @@
1
1
  module JsonParser
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -1,6 +1,6 @@
1
1
  class JsonParser::Wrapper
2
- include OptionsParser
3
- include TypeCast
2
+ include ConcernBuilder::OptionsParser
3
+ include JsonParser::TypeCast
4
4
 
5
5
  delegate :clazz, :type, to: :options_object
6
6
 
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.0.0
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-09 00:00:00.000000000 Z
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
@@ -1,9 +0,0 @@
1
- module OptionsParser
2
- extend ActiveSupport::Concern
3
-
4
- attr_reader :options
5
-
6
- def options_object
7
- @options_object ||= OpenStruct.new options
8
- end
9
- end