puppet_forge 2.3.2 → 3.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.
@@ -1,72 +0,0 @@
1
- module PuppetForge
2
- module Middleware
3
-
4
- # SymbolifyJson is a Faraday Middleware that will process any response formatted as a hash
5
- # and change all the keys into symbols (as long as they respond to the method #to_sym.
6
- #
7
- # This middleware makes no changes to the values of the hash.
8
- # If the response is not a hash, no changes will be made.
9
- class SymbolifyJson < Faraday::Middleware
10
-
11
- # Processes an array
12
- #
13
- # @return an array with any hash's keys turned into symbols if possible
14
- def process_array(array)
15
- array.map do |arg|
16
- # Search any arrays and hashes for hash keys
17
- if arg.is_a? Hash
18
- process_hash(arg)
19
- elsif arg.is_a? Array
20
- process_array(arg)
21
- else
22
- arg
23
- end
24
- end
25
- end
26
-
27
- # Processes a hash
28
- #
29
- # @return a hash with all keys turned into symbols if possible
30
- def process_hash(hash)
31
-
32
- # hash.map returns an array in the format
33
- # [ [key, value], [key2, value2], ... ]
34
- # Hash[] converts that into a hash in the format
35
- # { key => value, key2 => value2, ... }
36
- Hash[hash.map do |key, val|
37
- # Convert to a symbol if possible
38
- if key.respond_to? :to_sym
39
- new_key = key.to_sym
40
- else
41
- new_key = key
42
- end
43
-
44
- # If value is a hash or array look for more hash keys inside.
45
- if val.is_a?(Hash)
46
- [new_key, process_hash(val)]
47
- elsif val.is_a?(Array)
48
- [new_key, process_array(val)]
49
- else
50
- [new_key, val]
51
- end
52
- end]
53
- end
54
-
55
- def process_response(env)
56
- if !env["body"].nil? && env["body"].is_a?(Hash)
57
- process_hash(env.body)
58
- else
59
- env.body
60
- end
61
- end
62
-
63
- def call(environment)
64
- @app.call(environment).on_complete do |env|
65
- env.body = process_response(env)
66
- end
67
- end
68
-
69
- end
70
- end
71
- end
72
-
@@ -1,63 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe PuppetForge::Middleware::SymbolifyJson do
4
- let(:basic_array) { [1, "two", 3] }
5
- let(:basic_hash) { { "id" => 1, "data" => "x" } }
6
- let(:symbolified_hash) { { :id => 1, :data => "x" } }
7
- let(:internal_hash) { { :id => 2, :data => basic_hash } }
8
-
9
- let(:hash_with_array) { { "id" => 3, "data" => basic_array } }
10
- let(:array_with_hash) { [1, "two", basic_hash] }
11
-
12
- let(:complex_array) { [array_with_hash, hash_with_array] }
13
- let(:complex_hash) { { "id" => 4, "data" => [complex_array, basic_array], "more_data" => hash_with_array } }
14
- let(:complex_request) { { "id" => 5, "data" => complex_hash } }
15
-
16
- let(:middleware) { described_class.new() }
17
-
18
- context "#process_array" do
19
- it "doesn't change an array with no array or hash inside" do
20
- processed_array = middleware.process_array(basic_array)
21
- expect(processed_array).to eql( [1, "two", 3] )
22
- end
23
-
24
- it "changes all keys of a hash inside the array" do
25
- processed_array = middleware.process_array(array_with_hash)
26
- expect(processed_array).to eql( [ 1, "two", { :id => 1, :data => "x" } ] )
27
- end
28
- end
29
-
30
- context "#process_hash" do
31
- it "changes all keys that respond to :to_sym into Symbols and doesn't change values." do
32
- processed_hash = middleware.process_hash(basic_hash)
33
- expect(processed_hash).to eql( { :id => 1, :data => "x" } )
34
- end
35
-
36
- it "doesn't change keys that don't respond to :to_sym" do
37
- processed_hash = middleware.process_hash(basic_hash.merge({ 1 => 2 }))
38
- expect(processed_hash).to eql( { :id => 1, :data => "x", 1 => 2 } )
39
- end
40
-
41
- it "can process a hash that is already symbolified" do
42
- processed_hash = middleware.process_hash(symbolified_hash)
43
- expect(processed_hash).to eql( { :id => 1, :data => "x" })
44
- end
45
-
46
- it "can process a hash with a hash inside of it" do
47
- processed_hash = middleware.process_hash(internal_hash)
48
- expect(processed_hash).to eql( {:id => 2, :data => { :id => 1, :data => "x" } })
49
- end
50
-
51
- it "can process a hash with an array inside of it" do
52
- processed_hash = middleware.process_hash(hash_with_array)
53
- expect(processed_hash).to eql( { :id => 3, :data => [1, "two", 3] } )
54
- end
55
-
56
- it "can handle extensively nested arrays and hashes" do
57
- processed_hash = middleware.process_hash(complex_request)
58
- expect(processed_hash).to eql( { :id => 5, :data => { :id => 4 , :data=>[ [ [1, "two", { :id => 1, :data => "x" } ], { :id=>3, :data => [1, "two", 3] } ], [1, "two", 3] ], :more_data => { :id => 3, :data => [1, "two", 3] } } } )
59
- end
60
- end
61
-
62
- end
63
-