jibe-hash_mapper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: beefbc50845352e4bf068002e453a0d4fe336628
4
+ data.tar.gz: da0be575cd521354ce34637607d776f70520013d
5
+ SHA512:
6
+ metadata.gz: cbe3da9986604b0b74c8a50992afcbd73247fbb80e2789bdbdc91fbd5fc97e1161333fe69d4460d61822027d8b0c506c0969aaac7cfb029062257c042a1e4209
7
+ data.tar.gz: 99ed55db150ab30e62efa5fc941cd357ce08f6dba86c0000994a64f389d3d7f314bcac1e9b0ce529d0bd0212a02dc328ed94a2a3353d58921647f46beeb30ede
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hash_mapper.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Josh Deeden
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # HashMapper
2
+
3
+ A general purpose Hash mapping library
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'hash_mapper'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install hash_mapper
20
+
21
+ ## Usage
22
+
23
+ See specs.
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/hash_mapper/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jibe/hash_mapper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jibe-hash_mapper"
8
+ spec.version = HashMapper::VERSION
9
+ spec.authors = ["Josh Deeden"]
10
+ spec.email = ["jdeeden@jibe.com"]
11
+ spec.summary = %q{General purpose hash mapping library}
12
+ spec.description = %q{General purpose hash mapping library}
13
+ spec.homepage = ""
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", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "rspec-nc"
26
+ spec.add_development_dependency "guard"
27
+ spec.add_development_dependency "guard-rspec"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "pry-remote"
30
+ spec.add_development_dependency "pry-nav"
31
+
32
+ spec.add_dependency "virtus"
33
+ spec.add_dependency "transproc"
34
+ end
@@ -0,0 +1,48 @@
1
+ require 'jibe/hash_mapper/version'
2
+ require 'transproc/all'
3
+
4
+ module Jibe
5
+ class HashMapper
6
+ include Transproc::Helper
7
+
8
+ attr_reader :key_map, :value_map
9
+
10
+ def initialize(key_map, value_map)
11
+ @key_map = key_map
12
+ @value_map = value_map
13
+ end
14
+
15
+ def map(attributes)
16
+ map_values(map_keys(attributes, key_map), value_map)
17
+ end
18
+
19
+ protected
20
+ def map_keys(attributes, key_map)
21
+ key_map = key_map.dup.symbolize_keys!
22
+ transformation = t(:symbolize_keys) >> t(:accept_keys, key_map.keys) >> t(:rename_keys, key_map)
23
+ transformation.call attributes
24
+ end
25
+
26
+ def map_values(attributes, value_map)
27
+ attributes.each do |key, value|
28
+ value_mapping = value_map[key]
29
+ if value_mapping && value_mapping.kind_of?(String)
30
+ attributes[key] = value_mapping
31
+ elsif value_mapping && value_mapping.kind_of?(Hash)
32
+ if value.kind_of?(Array)
33
+ transformation = t(:map_array, -> v { value_mapping[v] })
34
+ new_value = transformation.call(value)
35
+ attributes[key] = new_value
36
+ elsif value.kind_of?(String) || value.kind_of?(Symbol)
37
+ transformation = t(:map_value, key, -> v { value_mapping[v.to_s] })
38
+ new_value = transformation.call({key => value})
39
+ attributes[key] = new_value[key]
40
+ end
41
+ elsif value_mapping && value_mapping.kind_of?(Proc)
42
+ attributes[key] = value_mapping.call(value)
43
+ end
44
+ end
45
+ attributes
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module HashMapper
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jibe::HashMapper do
4
+ include_context "hashes"
5
+ include_context "mappings"
6
+
7
+ describe "Request" do
8
+ subject do
9
+ Jibe::HashMapper.new(request_key_map, request_value_map).map(request_hash)
10
+ end
11
+
12
+ it "maps request attributes" do
13
+ expect(subject).to include({
14
+ jobRequisitionId: request_hash[:id],
15
+ title: request_hash[:title],
16
+ description: request_hash[:description],
17
+ applyType: request_hash[:apply_type],
18
+ applyUrl: request_hash[:apply_url],
19
+ employmentTypes: ['fulltime', 'parttime'],
20
+ experienceLevel: 'entryLevel',
21
+ yearsExperience: request_hash[:years_experience],
22
+ education: 'highschool',
23
+ compensationType: request_hash[:compensation_type],
24
+ compensationAmount: request_hash[:compensation_amount],
25
+ companyId: 'EieNKiU3EQtPFB7H'
26
+ })
27
+ end
28
+ end
29
+
30
+ describe "Response" do
31
+ subject do
32
+ Jibe::HashMapper.new(response_key_map, response_value_map). map(response_hash)
33
+ end
34
+
35
+ it "maps response attributes" do
36
+ expect(subject).to include({
37
+ destination_post_id: response_hash['id'],
38
+ title: response_hash['title'],
39
+ description: response_hash['description'],
40
+ apply_type: response_hash['applyType'],
41
+ apply_url: response_hash['applyUrl'],
42
+ employment_types: ['full_time', 'part_time'],
43
+ experience_level: 'entry_level',
44
+ years_experience: response_hash['yearsExperience'],
45
+ education: 'high_school',
46
+ compensation_type: response_hash['compensationType'],
47
+ compensation_amount: response_hash['compensationAmount'],
48
+ garage_company_id: 'EieNKiU3EQtPFB7H'
49
+ })
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ require 'pry'
2
+ require 'jibe/hash_mapper'
3
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f }
@@ -0,0 +1,47 @@
1
+ shared_context 'hashes' do
2
+ let!(:request_hash) do
3
+ { id: SecureRandom.hex,
4
+ title: 'Full-time Astronaut',
5
+ description: 'Join NASA and be an Astronaut!',
6
+ city: 'New York',
7
+ region: 'NY',
8
+ country: 'US',
9
+ postal_code: '10014',
10
+ apply_type: :url,
11
+ apply_url: 'http://example.com/apply',
12
+ employment_types: ['full_time', 'part_time'],
13
+ experience_level: :entry_level,
14
+ years_experience: 10,
15
+ education: :high_school,
16
+ compensation_type: :salary,
17
+ compensation_amount: 400_000 }
18
+ end
19
+
20
+ let(:response_hash) do
21
+ {'companyId'=>'EieNKiU3EQtPFB7H',
22
+ 'jobRequisitionId'=>'33328e85dbeb6350aae7f813df49f30b',
23
+ 'title'=>'Full-time Astronaut',
24
+ 'location'=>'New York, NY US',
25
+ 'employmentTypes'=>['fulltime', 'parttime'],
26
+ 'description'=>'Join NASA and be an Astronaut!',
27
+ 'applyType'=>'url',
28
+ 'applyUrl'=>'http://example.com/apply',
29
+ 'education'=>'highschool',
30
+ 'experienceLevel'=>'entryLevel',
31
+ 'compensationType'=>'salary',
32
+ 'compensationAmount'=>'400000.0',
33
+ 'benefitsMedical'=>false,
34
+ 'benefitsDental'=>false,
35
+ 'benefitsVision'=>false,
36
+ 'benefitsParentalLeave'=>false,
37
+ 'benefitsVacationDays'=>false,
38
+ 'benefitsSickDays'=>false,
39
+ 'locale'=>'en_US',
40
+ 'id'=>'7390001',
41
+ 'referenceUrl'=>
42
+ 'https://jobs-tt-1875-appspot.sandbox.jibe.com#!t=jo&jid=/jibe-inc/full-time-astronaut-new-york-ny-usa-7390001',
43
+ 'yearsExperience'=>'10',
44
+ 'published'=>true,
45
+ 'jobpostExpiryDate'=>'1435178894'}
46
+ end
47
+ end
@@ -0,0 +1,112 @@
1
+ shared_context 'mappings' do
2
+ let(:request_key_map) do
3
+ {
4
+ id: :jobRequisitionId,
5
+ title: :title,
6
+ description: :description,
7
+ postal_code: :postalCode,
8
+ apply_type: :applyType,
9
+ apply_url: :applyUrl,
10
+ apply_emails: :applyEmails,
11
+ apply_instructions: :applyInstructions,
12
+ benefits: :benefits,
13
+ compensation_currency: :compensationCurrency,
14
+ compensation_frequency: :compensationFrequency,
15
+ compensation_range_min: :compensationRangeMin,
16
+ compensation_range_max: :compensationRangeMin,
17
+ city: :city,
18
+ region: :region,
19
+ country: :country,
20
+ employment_types: :employmentTypes,
21
+ experience_level: :experienceLevel,
22
+ years_experience: :yearsExperience,
23
+ education: :education,
24
+ compensation_type: :compensationType,
25
+ compensation_amount: :compensationAmount,
26
+ garage_company_id: :companyId,
27
+ start_date: :startDate,
28
+ end_date: :endDate,
29
+ travel_percent: :travelPercent,
30
+ error_email: :errorEmail,
31
+ destination_post_id: :destinationPostId
32
+ }
33
+ end
34
+
35
+ let(:request_value_map) do
36
+ {
37
+ companyId: 'EieNKiU3EQtPFB7H',
38
+ employmentTypes: { 'full_time' => 'fulltime',
39
+ 'part_time' => 'parttime' },
40
+
41
+ experienceLevel: { 'entry_level' => 'entryLevel',
42
+ 'experienced' => 'experienced',
43
+ 'manager' => 'manager',
44
+ 'director' => 'director',
45
+ 'executive' => 'executive' },
46
+
47
+ education: { 'associates' => 'associateDegree',
48
+ 'bachelors' => 'bachelors',
49
+ 'doctorate' => 'doctorate',
50
+ 'high_school' => 'highschool',
51
+ 'masters' => 'masters',
52
+ 'no_degree' => 'noDegreeRequired' }
53
+
54
+ }
55
+ end
56
+
57
+ let(:response_key_map) do
58
+ {
59
+ id: :destination_post_id,
60
+ title: :title,
61
+ description: :description,
62
+ postalCode: :postal_code,
63
+ applyType: :apply_type,
64
+ applyUrl: :apply_url,
65
+ applyEmails: :apply_emails,
66
+ applyInstructions: :apply_instructions,
67
+ benefits: :benefits,
68
+ compensationCurrency: :compensation_currency,
69
+ compensationFrequency: :compensation_frequency,
70
+ compensationRangeMin: :compensation_range_max,
71
+ city: :city,
72
+ region: :region,
73
+ country: :country,
74
+ employmentTypes: :employment_types,
75
+ experienceLevel: :experience_level,
76
+ yearsExperience: :years_experience,
77
+ education: :education,
78
+ compensationType: :compensation_type,
79
+ compensationAmount: :compensation_amount,
80
+ companyId: :garage_company_id,
81
+ startDate: :start_date,
82
+ endDate: :end_date,
83
+ travelPercent: :travel_percent,
84
+ errorEmail: :error_email,
85
+ jobRequisitionId: :job_requisition_id,
86
+ published: :published,
87
+ jobpostExpiryDate: :expires_at
88
+ }
89
+ end
90
+
91
+ let(:response_value_map) do
92
+ {
93
+ employment_types: { 'fulltime' => 'full_time',
94
+ 'parttime' => 'part_time' },
95
+
96
+ experience_level: { 'entryLevel' => 'entry_level',
97
+ 'experienced' => 'experienced',
98
+ 'manager' => 'manager',
99
+ 'director' => 'director',
100
+ 'executive' => 'executive' },
101
+
102
+ education: { 'associatesDegree' => 'associates',
103
+ 'bachelors' => 'bachelors',
104
+ 'doctorate' => 'doctorate',
105
+ 'highschool' => 'high_school',
106
+ 'masters' => 'masters',
107
+ 'noDegreeRequired' => 'no_degree' },
108
+
109
+ expires_at: -> (value) { Time.at(value.to_i) }
110
+ }
111
+ end
112
+ end
metadata ADDED
@@ -0,0 +1,215 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jibe-hash_mapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Josh Deeden
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-14 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: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-nc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-remote
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-nav
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: virtus
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: transproc
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: General purpose hash mapping library
168
+ email:
169
+ - jdeeden@jibe.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - Gemfile
176
+ - LICENSE.txt
177
+ - README.md
178
+ - Rakefile
179
+ - hash_mapper.gemspec
180
+ - lib/jibe/hash_mapper.rb
181
+ - lib/jibe/hash_mapper/version.rb
182
+ - spec/jibe/hash_mapper_spec.rb
183
+ - spec/spec_helper.rb
184
+ - spec/support/shared_contexts/hashes.rb
185
+ - spec/support/shared_contexts/mappings.rb
186
+ homepage: ''
187
+ licenses:
188
+ - MIT
189
+ metadata: {}
190
+ post_install_message:
191
+ rdoc_options: []
192
+ require_paths:
193
+ - lib
194
+ required_ruby_version: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - ">="
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
199
+ required_rubygems_version: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - ">="
202
+ - !ruby/object:Gem::Version
203
+ version: '0'
204
+ requirements: []
205
+ rubyforge_project:
206
+ rubygems_version: 2.4.6
207
+ signing_key:
208
+ specification_version: 4
209
+ summary: General purpose hash mapping library
210
+ test_files:
211
+ - spec/jibe/hash_mapper_spec.rb
212
+ - spec/spec_helper.rb
213
+ - spec/support/shared_contexts/hashes.rb
214
+ - spec/support/shared_contexts/mappings.rb
215
+ has_rdoc: