skn_utils 3.3.12 → 3.4.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
  SHA1:
3
- metadata.gz: e6048e54b3ef049c2f0cc12d2e5d8c1068464bf9
4
- data.tar.gz: 13d07f94bf6785297b7da9f407447abe7ea0242c
3
+ metadata.gz: d855808b255a82843fa3fe92df17e71473f2f61b
4
+ data.tar.gz: 6cad163a2d5269b36d874c8e524bcf74f517f1c0
5
5
  SHA512:
6
- metadata.gz: 491f607e8959573f8d9a8806d1447802507fb50a8ae51487fa428421a87c3b20569fe62212f0cc6a1b6226182dba2f25a6813515fe78fa60bced473fcb8be1fe
7
- data.tar.gz: 8237f57472729b24e8b25b7346bdda8413f0d0616f1942dbda9d6cda1f05a558c91441ba68d0f40fcbc48a65bb8b8b7678a23790ebef05cfd1a861dbb055da5e
6
+ metadata.gz: 5bae4958839f86632dbdaf840120e755e668719f3d0f49d8a25fa4ed729c25b2296c25d639fbf0f4c81f5403b0cf423a6421d39d448f65b8bd90c0be8da1f3ea
7
+ data.tar.gz: 7ac67cbc92251445e2cacb367d01658395d92e6d5ab057a15a1242140cd1c93bdbdb5ca83f29370dd341433298b6982253b8e70ab11be13a302796cee3afbbef
data/README.md CHANGED
@@ -16,6 +16,9 @@ Ruby Gem containing a Ruby PORO (Plain Old Ruby Object) that can be instantiated
16
16
 
17
17
 
18
18
  ## New Features
19
+ 09/2017 V3.4.0
20
+ Added HashToXml class which converts complex/simple hashes into XML. pre-req's Nokogiri 1.8.0 or higher, unless Rails present, then uses version included with rails.
21
+
19
22
  08/2017 V3.3.0
20
23
  Added Linked List classes which implement Single, Double, and Circular linked lists patterns. LinkedLists are implemented
21
24
  with method results returning node values or the nodes themselves.
@@ -55,12 +58,14 @@ Ruby Gem containing a Ruby PORO (Plain Old Ruby Object) that can be instantiated
55
58
  ## Public Components
56
59
  SknUtils::NestedResult # >= V 3.0.0 Primary Key/Value Container with Dot/Hash notiation support.
57
60
  SknSettings # Multi-level application Configuration class, Key/Value Container with Dot/Hash notiation support.
58
- SknUtils::Configurable # Basic one-level configuration settings module for Applications and or Gems
59
-
60
61
  SknHash # Wrapper for name only, WITHOUT SknUtils namespace, inherits from SknUtils::NestedResult
61
62
  SknUtils::ResultBean # Wrapper for name only, inherits from SknUtils::NestedResult
62
63
  SknUtils::PageControls # Wrapper for name only, inherits from SknUtils::NestedResult
63
64
 
65
+ SknUtils::Configurable # Basic one-level configuration settings module for Applications and or Gems
66
+ SknUtils::Converters::HashToXml # #call(hash) to convert Ruby complex hashes into XML
67
+
68
+
64
69
  SknUtils::List::LinkedList # List with forward (#next) navigation, and tail/open
65
70
  SknUtils::List::DoublyLinkedList # List with forward (#next) and backward (#prev) navigation, and head/tail open
66
71
  SknUtils::List::CircularLinkedList # List with forward (#next) and backward (#prev) navigation, and head/tail wrapping
@@ -0,0 +1,98 @@
1
+ ## lib/skn_utils/converters/hash_to_xml.rb
2
+ #
3
+ # Converts Simple Hash to XML, or a Complex nested hash to XML
4
+ # Ref: https://stackoverflow.com/questions/11933451/converting-nested-hash-into-xml-using-nokogiri
5
+ #
6
+ # Input Format:
7
+ # {
8
+ # someKey: someValue,
9
+ # list: {item: [1,2,3]},
10
+ # listings: {
11
+ # uri: 'topic/content/topic_value',
12
+ # '@attributes' => {secured: true}
13
+ # }
14
+ # }
15
+ #
16
+ # - produces:
17
+ # <?xml version="1.0"?>
18
+ # <root>
19
+ # <one>1</one>
20
+ # <two>2</two>
21
+ # <list>
22
+ # <item>1</item>
23
+ # <item>2</item>
24
+ # <item>3</item>
25
+ # </list>
26
+ # <listings secured="true">
27
+ # <uri>topic/content/topic_value</uri>
28
+ # </listings>
29
+ # </basic>
30
+ ##
31
+ # xml_string = SknUtils::Converters::HashToXml.call(hash)
32
+ ##
33
+ module SknUtils
34
+ module Converters
35
+
36
+ class HashToXml
37
+
38
+ def initialize(*args)
39
+ end
40
+
41
+ def self.call(*vargs_with_at_least_one_hash, &block)
42
+ new().call(*vargs_with_at_least_one_hash, &block)
43
+ end
44
+
45
+ def call(*vargs, &block)
46
+ return nil if vargs.size == 0
47
+ hash = generate_xml(*vargs )
48
+ yield hash if block_given?
49
+ hash
50
+ end
51
+
52
+ protected
53
+
54
+ ##
55
+ # generate xml from a hash of hashes or array of hashes
56
+ def generate_xml(data, parent = false, opt = {})
57
+ return if data.to_s.empty?
58
+ return unless data.is_a?(Hash)
59
+
60
+ unless parent
61
+ # assume that if the hash has a single key that it should be the root
62
+ root, data = (data.length == 1) ? [data.keys.first.to_s , data] : ["root", data]
63
+ builder = ::Nokogiri::XML::Builder.new(opt.merge(:encoding => 'UTF-8')) do |xml|
64
+ xml.send(root) {
65
+ generate_xml(data, xml)
66
+ }
67
+ end
68
+
69
+ return builder.to_xml
70
+ end
71
+
72
+ data.each do |label, value|
73
+ if value.is_a?(Hash)
74
+ attrs = value.fetch('@attributes', {})
75
+ # also passing 'text' as a key makes nokogiri do the same thing
76
+ text = value.fetch('@text', '')
77
+ parent.send(label, attrs, text) {
78
+ value.delete('@attributes')
79
+ value.delete('@text')
80
+ generate_xml(value, parent)
81
+ }
82
+
83
+ elsif value.is_a?(Array)
84
+ value.each do |el|
85
+ # lets trick the above into firing so we do not need to rewrite the checks
86
+ el = {label => el}
87
+ generate_xml(el, parent)
88
+ end
89
+
90
+ else
91
+ parent.send(label, value)
92
+ end
93
+ end
94
+ end # end method
95
+
96
+ end # end class
97
+ end
98
+ end
@@ -1,6 +1,6 @@
1
1
  ## lib/skn_utils/action_service.rb
2
2
  #
3
- # Exploritory Action/Service Class
3
+ # Exploratory Action/Service Class
4
4
  # Ref: https://blog.lelonek.me/what-service-objects-are-not-7abef8aa2f99#.p64vudxq4
5
5
  # http://sporto.github.io/blog/2012/11/15/a-pattern-for-service-objects-in-rails/
6
6
  #
@@ -2,8 +2,8 @@
2
2
  module SknUtils
3
3
  class Version
4
4
  MAJOR = 3
5
- MINOR = 3
6
- PATCH = 12
5
+ MINOR = 4
6
+ PATCH = 0
7
7
 
8
8
  def self.to_s
9
9
  [MAJOR, MINOR, PATCH].join('.')
data/lib/skn_utils.rb CHANGED
@@ -6,6 +6,7 @@ require 'date'
6
6
  require 'time'
7
7
  unless defined?(Rails)
8
8
  require 'deep_merge'
9
+ require 'nokogiri'
9
10
  end
10
11
  require 'skn_utils/nested_result'
11
12
  require 'skn_utils/result_bean'
@@ -23,6 +24,9 @@ require 'skn_utils/lists/circular_linked_list'
23
24
  # require 'skn_utils/exploring/commander'
24
25
  # require 'skn_utils/exploring/action_service'
25
26
  # require 'skn_utils/exploring/configuration'
27
+ if defined?(::Nokogiri)
28
+ require 'skn_utils/converters/hash_to_xml'
29
+ end
26
30
  require 'skn_hash'
27
31
  require 'skn_settings'
28
32
 
data/skn_utils.gemspec CHANGED
@@ -41,4 +41,6 @@ EOF
41
41
  spec.add_development_dependency "pry", ">= 0"
42
42
  spec.add_development_dependency "simplecov", ">= 0"
43
43
  spec.add_development_dependency 'benchmark-ips'
44
+ spec.add_development_dependency 'nokogiri', '~> 1.8.0'
45
+
44
46
  end
@@ -0,0 +1,120 @@
1
+ ##
2
+ # spec/lib/skn_utils/converters/hash_to_xml_spec.rb
3
+ #
4
+
5
+ describe SknUtils::Converters::HashToXml, "Hash to XML Converter Utility " do
6
+
7
+ context "Initializers Feature. " do
8
+
9
+ it "#new can be called without params or block without error. " do
10
+ expect( described_class.new()).to be_a described_class
11
+ end
12
+
13
+ it "#call can be called without params or block without error. " do
14
+ expect( described_class.call()).to be nil
15
+ end
16
+
17
+ it "#call called with params returns xml string as expected. " do
18
+ string = described_class.call({
19
+ one: 1,
20
+ two: 2,
21
+ list: {item: [1,2,3]},
22
+ listings: {item: [1,2,3]}
23
+ })
24
+ puts __method__, string
25
+ expect(string).to be_a String
26
+ end
27
+
28
+ it "#call called with params and block yields xml string as expected. " do
29
+ described_class.call({
30
+ one: 1,
31
+ two: 2,
32
+ list: {item: [1,2,3]},
33
+ listings: {item: [1,2,3]}
34
+ }) do |string|
35
+ puts __method__, string
36
+ expect(string).to be_a String
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ context "Handles Complex Hashes. " do
43
+ let(:array_of_hashes) {
44
+ {'resource' => [
45
+ { 'name' => 'category1',
46
+ 'subCategory' => [
47
+ { 'name' => 'subCategory1',
48
+ 'product' => [
49
+ { 'name' => 'productName1',
50
+ 'desc' => 'desc1' },
51
+ { 'name' => 'productName2',
52
+ 'desc' => 'desc2' } ]
53
+ } ]
54
+ },
55
+ { 'name' => 'category2',
56
+ 'subCategory' => [
57
+ { 'name' => 'subCategory2.1',
58
+ 'product' => [
59
+ { 'name' => 'productName2.1.1',
60
+ 'desc' => 'desc1' },
61
+ { 'name' => 'productName2.1.2',
62
+ 'desc' => 'desc2' } ]
63
+ } ]
64
+ }
65
+ ]
66
+ }
67
+ }
68
+
69
+ let(:xml_aware_hash) {
70
+ {'xmlAwareHash' =>
71
+ {
72
+ 'num' => 99,
73
+ 'title' => 'something witty',
74
+ 'nested' => {
75
+ 'total' => [99, 98],
76
+ '@attributes' => {'foo' => 'bar', 'hello' => 'world'}
77
+ },
78
+ 'anothernest' => {
79
+ '@attributes' => {'foo' => 'bar', 'hello' => 'world'},
80
+ 'date' => [
81
+ 'today',
82
+ {'day' => 23, 'month' => 'Dec',
83
+ 'year' => {'y' => 1999, 'c' => 21},
84
+ '@attributes' => {'foo' => 'blhjkldsaf'}
85
+ }
86
+ ]
87
+ }
88
+ }
89
+ }
90
+ }
91
+
92
+ it "#call(nested_hash) return an xml string as expected." do
93
+ string = described_class.call({
94
+ one: 1,
95
+ two: 2,
96
+ list: {item: [1,2,3]},
97
+ listings: {
98
+ uri: 'topic/content/topic_value',
99
+ '@attributes' => {secured: true}}
100
+ })
101
+ puts __method__, string
102
+ expect( string ).to be_a String
103
+ end
104
+
105
+ it "#call(array_of_hashes) return an xml string as expected." do
106
+ string = described_class.call(array_of_hashes, false)
107
+ puts __method__, string
108
+ expect( string ).to be_a String
109
+ end
110
+
111
+ it "#call(xml_aware_hash) return an xml string as expected." do
112
+ string = described_class.call(xml_aware_hash)
113
+ puts __method__, string
114
+ expect( string ).to be_a String
115
+ end
116
+
117
+ end
118
+
119
+
120
+ end
data/spec/spec_helper.rb CHANGED
@@ -16,7 +16,6 @@ require 'skn_utils/exploring/commander'
16
16
  require 'skn_utils/exploring/action_service'
17
17
  require 'skn_utils/exploring/configuration'
18
18
  require 'skn_utils/business_services/year_month'
19
-
20
19
  require 'rspec'
21
20
 
22
21
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skn_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.12
4
+ version: 3.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Scott Jr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-24 00:00:00.000000000 Z
11
+ date: 2017-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deep_merge
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: nokogiri
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 1.8.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 1.8.0
111
125
  description: "The intent of the NestedResult class is to be a container for data values
112
126
  composed of key/value pairs, \nwith easy access to its contents, and on-demand transformation
113
127
  back to the hash (#to_hash).\n\nReview the RSpec tests, and or review the README
@@ -136,6 +150,7 @@ files:
136
150
  - lib/skn_utils.rb
137
151
  - lib/skn_utils/business_services/year_month.rb
138
152
  - lib/skn_utils/configurable.rb
153
+ - lib/skn_utils/converters/hash_to_xml.rb
139
154
  - lib/skn_utils/exploring/action_service.rb
140
155
  - lib/skn_utils/exploring/commander.rb
141
156
  - lib/skn_utils/exploring/configuration.rb
@@ -158,6 +173,7 @@ files:
158
173
  - spec/lib/skn_settings_spec.rb
159
174
  - spec/lib/skn_utils/business_services/year_month_spec.rb
160
175
  - spec/lib/skn_utils/configurable_spec.rb
176
+ - spec/lib/skn_utils/converters/hash_to_xml_spec.rb
161
177
  - spec/lib/skn_utils/exploring/action_service_spec.rb
162
178
  - spec/lib/skn_utils/exploring/commander_spec.rb
163
179
  - spec/lib/skn_utils/exploring/configuration_spec.rb
@@ -206,6 +222,7 @@ test_files:
206
222
  - spec/lib/skn_settings_spec.rb
207
223
  - spec/lib/skn_utils/business_services/year_month_spec.rb
208
224
  - spec/lib/skn_utils/configurable_spec.rb
225
+ - spec/lib/skn_utils/converters/hash_to_xml_spec.rb
209
226
  - spec/lib/skn_utils/exploring/action_service_spec.rb
210
227
  - spec/lib/skn_utils/exploring/commander_spec.rb
211
228
  - spec/lib/skn_utils/exploring/configuration_spec.rb