maestrano-connector-rails 0.3.1 → 0.3.2

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: 7aacc2acd6c8b4ae959bc04f22610a0579eac001
4
- data.tar.gz: d0291c5cdc84f85e25e11a845080792e420b27dc
3
+ metadata.gz: 9444b9cdc7de73b532ce22743daf00eed51f6a88
4
+ data.tar.gz: 68ae18a05cdca6bf985e6927c06d108ff0cc58bf
5
5
  SHA512:
6
- metadata.gz: 237c78f0dbf7fcf046858a580eda157c0d2a0629dfbc03048dc79a3218168c2c196a5f6efb308f1a4e4e719b07294d9b8e0d6bc729735280b2581c81f66f48ab
7
- data.tar.gz: bc8df72fe6d4e8d81f84b4da628c72011e56d04344053f58ad9dc475ccf5c596fa39f78265fad03ab8badc8eef2ac5cebd95cbabd673c72b7a3a14f433e158f0
6
+ metadata.gz: 80667b16e9b8d3436992174980c206b91bf8c95b9e1e5169d8aa6f5e963e9543e08be773633b00a49c14f08f06f1032213f49f2db75b7d162d6293e4e5cf75dd
7
+ data.tar.gz: ad3e5ff87948d0e537ab5d429a021e611f997f47eeca4bac250bdaeb4d33386bfed54d667ff9ceb9518ad0f081cba64e89803588d8b963fdfec393eaed84e133
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
@@ -108,6 +108,13 @@ module Maestrano::Connector::Rails::Concerns::Entity
108
108
  def object_name_from_external_entity_hash(entity)
109
109
  raise "Not implemented"
110
110
  end
111
+
112
+ # [{reference_class: Entities::.., connec_field: 'account_id', external_field: 'account/something/id'}]
113
+ # ledger_account_idmap = Entities::Account.find_idmap({connec_id: entity['account_id'], organization_id: organization.id})
114
+ # ledger_account_id = ledger_account_idmap && ledger_account_idmap.external_id
115
+ def references
116
+ []
117
+ end
111
118
  end
112
119
 
113
120
  # ----------------------------------------------
@@ -115,12 +122,22 @@ module Maestrano::Connector::Rails::Concerns::Entity
115
122
  # ----------------------------------------------
116
123
  # Map a Connec! entity to the external format
117
124
  def map_to_external(entity, organization)
118
- self.class.mapper_class.normalize(entity)
125
+ ref_hash = {}
126
+ self.class.references.each do |ref|
127
+ ref_hash.merge! ref[:external_field].split('/').reverse.inject(self.class.id_from_ref(entity, ref, false, organization)) { |a, n| { n.to_sym => a } }
128
+ end
129
+
130
+ self.class.mapper_class.normalize(entity).merge(ref_hash)
119
131
  end
120
132
 
121
133
  # Map an external entity to Connec! format
122
134
  def map_to_connec(entity, organization)
123
- self.class.mapper_class.denormalize(entity)
135
+ ref_hash = {}
136
+ self.class.references.each do |ref|
137
+ ref_hash.merge! ref[:connec_field].split('/').reverse.inject(self.class.id_from_ref(entity, ref, true, organization)) { |a, n| { n.to_sym => a } }
138
+ end
139
+
140
+ self.class.mapper_class.denormalize(entity).merge(ref_hash)
124
141
  end
125
142
 
126
143
  # ----------------------------------------------
@@ -385,6 +402,24 @@ module Maestrano::Connector::Rails::Concerns::Entity
385
402
  entity_instance.map_external_entity_with_idmap(external_entity, connec_entity_name, idmap, organization)
386
403
  end
387
404
  end
405
+
406
+ def id_from_ref(entity, ref, is_external, organization)
407
+ # field can be address/billing/country_id
408
+ field = is_external ? ref[:external_field] : ref[:connec_field]
409
+ field = field.split('/')
410
+ id = entity
411
+ field.each do |f|
412
+ id &&= id[f]
413
+ end
414
+
415
+ if is_external
416
+ idmap = ref[:reference_class].find_idmap({external_id: id, organization_id: organization.id})
417
+ idmap && idmap.connec_id
418
+ else
419
+ idmap = ref[:reference_class].find_idmap({connec_id: id, organization_id: organization.id})
420
+ idmap && idmap.external_id
421
+ end
422
+ end
388
423
  end
389
424
 
390
425
  def map_external_entity_with_idmap(external_entity, connec_entity_name, idmap, organization)
@@ -61,11 +61,41 @@ module Maestrano::Connector::Rails::Concerns::SubEntityBase
61
61
  Maestrano::Connector::Rails::IdMap.create(h)
62
62
  end
63
63
  end
64
+
65
+ # { 'External Entity' => LalaMapper, 'Other external entity' => LiliMapper }
66
+ # or { 'Connec Entity' => LalaMapper, 'Other connec entity' => LiliMapper }
67
+ def mapper_classes
68
+ {}
69
+ end
70
+
71
+ # {
72
+ # 'External Entity' => [{reference_class: Entities::.., connec_field: '', external_field: ''}],
73
+ # 'Other external entity' => [{reference_class: Entities::.., connec_field: '', external_field: ''}]
74
+ # }
75
+ def references
76
+ {}
77
+ end
64
78
  end
65
79
 
66
80
 
67
81
  def map_to(name, entity, organization)
68
- raise "Not implemented"
82
+ mapper = self.class.mapper_classes[name]
83
+ raise "Impossible mapping from #{self.class.entity_name} to #{name}" unless mapper
84
+
85
+ ref_hash = {}
86
+ if self.class.references[name]
87
+ self.class.references[name].each do |ref|
88
+ field = self.class.external? ? ref[:connec_field] : ref[:external_field]
89
+ ref_hash.merge! field.split('/').reverse.inject(Maestrano::Connector::Rails::Entity.id_from_ref(entity, ref, false, organization)) { |a, n| { n.to_sym => a } }
90
+ end
91
+ end
92
+
93
+ if self.class.external?
94
+ mapped_entity = mapper.denormalize(entity)
95
+ else
96
+ mapped_entity = mapper.normalize(entity)
97
+ end
98
+ mapped_entity.merge(ref_hash)
69
99
  end
70
100
 
71
101
 
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: maestrano-connector-rails 0.3.1 ruby lib
5
+ # stub: maestrano-connector-rails 0.3.2 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "maestrano-connector-rails"
9
- s.version = "0.3.1"
9
+ s.version = "0.3.2"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Pierre Berard"]
14
- s.date = "2016-03-18"
14
+ s.date = "2016-03-21"
15
15
  s.description = "Maestrano is the next generation marketplace for SME applications. See https://maestrano.com for details."
16
16
  s.email = "pierre.berard@maestrano.com"
17
17
  s.executables = ["rails"]
@@ -132,16 +132,44 @@ describe Maestrano::Connector::Rails::Entity do
132
132
 
133
133
  describe 'map_to_external' do
134
134
  it 'calls the mapper normalize' do
135
- expect(AMapper).to receive(:normalize).with({})
135
+ expect(AMapper).to receive(:normalize).with({}).and_return({})
136
136
  subject.map_to_external({}, nil)
137
137
  end
138
+
139
+ context 'with references' do
140
+ let!(:organization) { create(:organization) }
141
+ let!(:idmap) { create(:idmap, organization: organization) }
142
+ before {
143
+ clazz = Maestrano::Connector::Rails::Entity
144
+ allow(clazz).to receive(:find_idmap).and_return(idmap)
145
+ allow(clazz).to receive(:references).and_return([{reference_class: clazz, connec_field: 'organization_id', external_field: 'contact_id'}])
146
+ }
147
+
148
+ it 'returns the mapped entity with its references' do
149
+ expect(subject.map_to_external({'organization_id' => 'abcd'}, organization)).to eql({contact_id: idmap.external_id})
150
+ end
151
+ end
138
152
  end
139
153
 
140
154
  describe 'map_to_connec' do
141
155
  it 'calls the mapper denormalize' do
142
- expect(AMapper).to receive(:denormalize).with({})
156
+ expect(AMapper).to receive(:denormalize).with({}).and_return({})
143
157
  subject.map_to_connec({}, nil)
144
158
  end
159
+
160
+ context 'with references' do
161
+ let!(:organization) { create(:organization) }
162
+ let!(:idmap) { create(:idmap, organization: organization) }
163
+ before {
164
+ clazz = Maestrano::Connector::Rails::Entity
165
+ allow(clazz).to receive(:find_idmap).and_return(idmap)
166
+ allow(clazz).to receive(:references).and_return([{reference_class: clazz, connec_field: 'organization_id', external_field: 'contact_id'}])
167
+ }
168
+
169
+ it 'returns the mapped entity with its references' do
170
+ expect(subject.map_to_connec({'contact_id' => 'abcd'}, organization)).to eql({organization_id: idmap.connec_id})
171
+ end
172
+ end
145
173
  end
146
174
  end
147
175
 
@@ -116,7 +116,33 @@ describe Maestrano::Connector::Rails::SubEntityBase do
116
116
  subject { Maestrano::Connector::Rails::SubEntityBase.new }
117
117
 
118
118
  describe 'map_to' do
119
- it { expect{ subject.map_to('name', {}, nil) }.to raise_error('Not implemented') }
119
+ before {
120
+ class AMapper
121
+ extend HashMapper
122
+ end
123
+ allow(subject.class).to receive(:mapper_classes).and_return('Name' => AMapper)
124
+ }
125
+
126
+ context 'when external' do
127
+ before {
128
+ allow(subject.class).to receive(:external?).and_return(true)
129
+ }
130
+
131
+ it 'calls the mapper denormalize' do
132
+ expect(AMapper).to receive(:denormalize).and_return({})
133
+ subject.map_to('Name', {}, nil)
134
+ end
135
+ end
136
+ context 'when not external' do
137
+ before {
138
+ allow(subject.class).to receive(:external?).and_return(false)
139
+ }
140
+
141
+ it 'calls the mapper normalize' do
142
+ expect(AMapper).to receive(:normalize).and_return({})
143
+ subject.map_to('Name', {}, nil)
144
+ end
145
+ end
120
146
  end
121
147
  end
122
148
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maestrano-connector-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre Berard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-18 00:00:00.000000000 Z
11
+ date: 2016-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: maestrano-rails