mishmash 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 718d15742a59c5128506b8bcf435b0f85663cc05
4
+ data.tar.gz: 9b912c09140040ffd628a001d1f8b66e7627ade1
5
+ SHA512:
6
+ metadata.gz: 2bf78b8a079d8282b96bc49ca0fd5eb2c197b127324e4c7b754736bbcee53abcd1c5fcdd437a7bf6514505b3a1803676d805af1999258d6e6e6075e44587dc3e
7
+ data.tar.gz: 2f968ba2e3959822a1882ddbc8848d136c72277abea265ce7e346b80dd65f6f6edcd38653f55e8d61a60a7f98a1e9d3793a5c0f54ee483a1b8e6ef2245b9230c
data/.gitignore ADDED
@@ -0,0 +1,51 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mishmash (0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (12.3.2)
11
+ rspec (3.8.0)
12
+ rspec-core (~> 3.8.0)
13
+ rspec-expectations (~> 3.8.0)
14
+ rspec-mocks (~> 3.8.0)
15
+ rspec-core (3.8.0)
16
+ rspec-support (~> 3.8.0)
17
+ rspec-expectations (3.8.2)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-mocks (3.8.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-support (3.8.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler
30
+ mishmash!
31
+ rake
32
+ rspec (~> 3.2)
33
+
34
+ BUNDLED WITH
35
+ 1.16.1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Ty Rauber
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # Mishmash
2
+
3
+ A ruby class for translating *complex* hashes to an alternate schema.
4
+
5
+ ## Features
6
+
7
+ * rename keys in a nested hash
8
+ * condense an array of hash values to an array
9
+ * return a hash from an array of hashes based on conditional attributes
10
+ * return an array of hashes filtered by keys
11
+
12
+ ## What? Why?
13
+
14
+ How do you translate an API response - or multiple responses from different APIs - to a single database schema?
15
+
16
+ Mishmash.
17
+
18
+ ## Methods
19
+
20
+ ### hash = Mishmash.new({ "a" => 1 })
21
+
22
+ Mishmash new accepts and returns an input hash.
23
+
24
+ ### hash.translate({ "a" = "b" })
25
+
26
+ An instance of Mishmash accepts a schema and returns an translated output.
27
+
28
+ > { "b" => 1 }
29
+
30
+ ## Examples
31
+
32
+ * rename keys in a nested hash
33
+
34
+ ```Ruby
35
+ > Mishmash.new({ "hash"=> {"a" => 1} }).translate({ "hash" =>{ "a" => "b" }})
36
+ => {"hash" => {"b" => 1}}
37
+ ```
38
+
39
+ * condense an array of hash values to an array
40
+
41
+ ```Ruby
42
+ > Mishmash.new({
43
+ "array" => [
44
+ {"key" => "value 1"},
45
+ {"key" => "value 2"}
46
+ ]
47
+ }).translate({
48
+ "array" => {"key" => "array_of_values"}
49
+ })
50
+
51
+ => { "array_of_values" => ["value 1", "value 2"] }
52
+ ```
53
+
54
+ * return a hash from an array of hashes based on a condition
55
+
56
+ ```Ruby
57
+ > Mishmash.new({
58
+ "array" => [
59
+ {"option"=> "1", "value" => "X"},
60
+ {"option"=> "2", "value" => "Y"}
61
+ ]
62
+ }
63
+ }).translate({"array" => {"option" => "1", "value" => "key"}})
64
+
65
+ => {"key" => "X"}
66
+ ```
67
+
68
+
69
+ * return an array of hashes filtered by keys
70
+
71
+ ```Ruby
72
+ > Mishmash.new({
73
+ "array" => [
74
+ {"option"=> "1", "value" => "X"},
75
+ {"option"=> "2", "value" => "Y"}
76
+ ]
77
+ }
78
+ }).translate({"array" => ["new_array", {"option" => "1", "value" => "key"}]})
79
+
80
+ => {"new_array" => [{"key" => "X"}, {"key" => "y"}]}
81
+ ```
82
+
83
+ ## Inspection
84
+
85
+ Mishmash .translate() returns an Mishmash instance, allowing for additional translation. Useful for inspecting and navigating a response to determine and appropriate response.
data/lib/mishmash.rb ADDED
@@ -0,0 +1,35 @@
1
+ class Mishmash < Hash
2
+ def initialize(input, schema={}, output={})
3
+ input.each_pair do |key,value|
4
+ self[key] = value
5
+ end
6
+ self.translate(schema, output) unless schema.empty?
7
+ end
8
+
9
+ def translate(schema, output={})
10
+ return unless self.is_a?(Hash) && schema.is_a?(Hash)
11
+ (self.keys | schema.keys).each_with_object({}) do |k, diff|
12
+ if self[k] && schema[k] && self[k] != schema[k]
13
+ if self[k].is_a?(Hash) && schema[k].is_a?(Hash)
14
+ Mishmash.new(self[k]).translate(schema[k], output)
15
+ elsif self[k].is_a?(Array) && schema[k].is_a?(Array)
16
+ output[schema[k][0]] = self[k].map do |kk,vv|
17
+ Mishmash.new(kk).translate(schema[k][1])
18
+ end
19
+ elsif self[k].is_a?(Array) && schema[k].is_a?(Hash)
20
+ self[k].each do |kk|
21
+ if (self[k].map(&:values).flatten & schema[k].values).empty? || !(kk.values & schema[k].values).empty?
22
+ Mishmash.new(kk).translate(schema[k], output)
23
+ end
24
+ end
25
+ elsif output[schema[k]]
26
+ output[schema[k]] = [output[schema[k]]] unless output[schema[k]].is_a?(Array)
27
+ output[schema[k]].push(self[k])
28
+ elsif self[k]
29
+ output[schema[k]] = self[k]
30
+ end
31
+ end
32
+ end
33
+ return Mishmash.new(output)
34
+ end
35
+ end
data/mishmash.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mishmash'
8
+ spec.version = '0.1'
9
+ spec.authors = ['Ty Rauber']
10
+ spec.email = ['tyrauber@mac.com']
11
+ spec.summary = %q{ A ruby hash translation library }
12
+ spec.description = %q{ Given an input hash, output a translated hash with a given schema. Mishmash.new({a:1}).translate({a:b}) = {b:1} }
13
+ spec.homepage = 'https://github.com/tyrauber/Mishmash'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec', '~> 3.2'
24
+ end
@@ -0,0 +1,167 @@
1
+ require './lib/mishmash'
2
+
3
+ describe "Mishmash" do
4
+
5
+ it "should respond to new" do
6
+ expect(Mishmash.respond_to?('new')).to be(true)
7
+ end
8
+
9
+ describe "output" do
10
+ it "shgould respond to translate" do
11
+ expect(Mishmash.new({"key" => "value"}).respond_to?("translate")).to be(true)
12
+ end
13
+ end
14
+
15
+ context "translate with alternate schema" do
16
+
17
+ it "root keys" do
18
+ hash = Mishmash.new({"key" => "value"})
19
+ expect(hash.translate({"key" => "new_key"})).to eq({"new_key" => "value"})
20
+ end
21
+
22
+ it "nested keys to root keys" do
23
+ hash = Mishmash.new({"hash" => {"key" => "value"}})
24
+ expect(hash.translate({"hash" => {"key" => "new_key"}})).to eq({"new_key" => "value"})
25
+ end
26
+ end
27
+
28
+ describe "given an input, schema and output" do
29
+ let(:input){
30
+ {
31
+ "array" => [
32
+ {"key" => "value 1"},
33
+ {"key" => "value 2"}
34
+ ]
35
+ }
36
+ }
37
+ let(:output){
38
+ {
39
+ "array_of_values" => ["value 1", "value 2"]
40
+ }
41
+ }
42
+
43
+ let(:schema) {
44
+ {"array" => {"key" => "array_of_values"}}
45
+ }
46
+ it "Translate an Array of Hashes to Hash of Arrays with a schema" do
47
+ expect(Mishmash.new(input).translate(schema)).to eq(output)
48
+ end
49
+ end
50
+
51
+ describe "given an input with an array of hashes" do
52
+ let(:input){
53
+ {
54
+ "array" => [
55
+ {"key" => "value 1"},
56
+ {"key" => "value 2"}
57
+ ]
58
+ }
59
+ }
60
+ describe "return a hash with array of values" do
61
+ let(:output){
62
+ {
63
+ "key" => ["value 1", "value 2"]
64
+ }
65
+ }
66
+ let(:schema) {
67
+ {"array" => {"key" => "key"}}
68
+ }
69
+ it "with an alternate key_name" do
70
+ expect(Mishmash.new(input).translate(schema)).to eq(output)
71
+ end
72
+ end
73
+ end
74
+
75
+ describe "given an input with an array of hashes" do
76
+ let(:input){
77
+ {
78
+ "array" => [
79
+ {"key" => "value 1"},
80
+ {"key" => "value 2"}
81
+ ]
82
+ }
83
+ }
84
+ describe "return a hash with array of values" do
85
+ let(:output){
86
+ {
87
+ "key" => ["value 1", "value 2"]
88
+ }
89
+ }
90
+ let(:schema) {
91
+ {"array" => {"key" => "key"}}
92
+ }
93
+ it "with an alternate key_name" do
94
+ expect(Mishmash.new(input).translate(schema)).to eq(output)
95
+ end
96
+ end
97
+ end
98
+
99
+ describe "given an input with an array of hashes" do
100
+ let(:input){
101
+ {
102
+ "array" => [
103
+ {"option"=> "1", "value" => "X"},
104
+ {"option"=> "2", "value" => "Y"}
105
+ ]
106
+ }
107
+ }
108
+ describe "return a hash matching a condition" do
109
+ let(:output){
110
+ {"option" => "X"}
111
+ }
112
+ let(:schema) {
113
+ {"array" => {"option" => "1", "value" => "option"}}
114
+ }
115
+ it "with an alternate key_name" do
116
+ expect(Mishmash.new(input).translate(schema)).to eq(output)
117
+ end
118
+ end
119
+ end
120
+
121
+ describe "given an input with an array of hashes" do
122
+ let(:input){
123
+ {
124
+ "array" => [
125
+ {"option"=> "1", "value" => "X"},
126
+ {"option"=> "2", "value" => "Y"}
127
+ ]
128
+ }
129
+ }
130
+ describe "return a hash matching a condition" do
131
+ let(:output){
132
+ {"option" => "X"}
133
+ }
134
+ let(:schema) {
135
+ {"array" => {"option" => "1", "value" => "option"}}
136
+ }
137
+ it "with an alternate key_name" do
138
+ expect(Mishmash.new(input).translate(schema)).to eq(output)
139
+ end
140
+ end
141
+ end
142
+
143
+ describe "given an input with an array of hashes" do
144
+ let(:input){
145
+ {
146
+ "array" => [
147
+ {"option"=> "1", "value" => "X"},
148
+ {"option"=> "2", "value" => "Y"}
149
+ ]
150
+ }
151
+ }
152
+ describe "return an array of translated and filtered hashes" do
153
+ let(:output){
154
+ {"new array" => [
155
+ {"attribute" => "X"},
156
+ {"attribute" => "Y"}
157
+ ]}
158
+ }
159
+ let(:schema) {
160
+ {"array" => [ "new array", {"value" => "attribute"} ] }
161
+ }
162
+ it "with an alternate key_name" do
163
+ expect(Mishmash.new(input).translate(schema)).to eq(output)
164
+ end
165
+ end
166
+ end
167
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mishmash
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Ty Rauber
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ description: " Given an input hash, output a translated hash with a given schema.
56
+ Mishmash.new({a:1}).translate({a:b}) = {b:1} "
57
+ email:
58
+ - tyrauber@mac.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE
67
+ - README.md
68
+ - lib/mishmash.rb
69
+ - mishmash.gemspec
70
+ - spec/mishmash_spec.rb
71
+ homepage: https://github.com/tyrauber/Mishmash
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.6.14
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: A ruby hash translation library
95
+ test_files:
96
+ - spec/mishmash_spec.rb