go_import 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. data/bin/go-import +96 -0
  2. data/lib/go_import/csv_helper.rb +47 -0
  3. data/lib/go_import/email_helper.rb +10 -0
  4. data/lib/go_import/errors.rb +22 -0
  5. data/lib/go_import/excel_helper.rb +10 -0
  6. data/lib/go_import/global_phone.json +6571 -0
  7. data/lib/go_import/model/address.rb +61 -0
  8. data/lib/go_import/model/class_settings.rb +50 -0
  9. data/lib/go_import/model/coworker.rb +76 -0
  10. data/lib/go_import/model/coworker_reference.rb +33 -0
  11. data/lib/go_import/model/customfield.rb +87 -0
  12. data/lib/go_import/model/deal.rb +172 -0
  13. data/lib/go_import/model/deal_class_settings.rb +73 -0
  14. data/lib/go_import/model/deal_state.rb +15 -0
  15. data/lib/go_import/model/deal_status.rb +23 -0
  16. data/lib/go_import/model/deal_status_reference.rb +47 -0
  17. data/lib/go_import/model/deal_status_setting.rb +49 -0
  18. data/lib/go_import/model/documents.rb +51 -0
  19. data/lib/go_import/model/link.rb +70 -0
  20. data/lib/go_import/model/note.rb +97 -0
  21. data/lib/go_import/model/note_classification.rb +25 -0
  22. data/lib/go_import/model/organization.rb +219 -0
  23. data/lib/go_import/model/person.rb +151 -0
  24. data/lib/go_import/model/referencetosource.rb +46 -0
  25. data/lib/go_import/model/relation.rb +23 -0
  26. data/lib/go_import/model/rootmodel.rb +359 -0
  27. data/lib/go_import/model/settings.rb +61 -0
  28. data/lib/go_import/model/tag.rb +35 -0
  29. data/lib/go_import/model_helpers.rb +54 -0
  30. data/lib/go_import/phone_helper.rb +74 -0
  31. data/lib/go_import/roo_helper.rb +80 -0
  32. data/lib/go_import/serialize_helper.rb +186 -0
  33. data/lib/go_import/source.rb +87 -0
  34. data/lib/go_import/templating.rb +52 -0
  35. data/lib/go_import.rb +19 -0
  36. data/sources/csv/.gitignore +14 -0
  37. data/sources/csv/.go_import/runner.rb +62 -0
  38. data/sources/csv/Gemfile +5 -0
  39. data/sources/csv/Rakefile.rb +7 -0
  40. data/sources/csv/converter.rb +179 -0
  41. data/sources/csv/data/coworkers.csv +2 -0
  42. data/sources/csv/data/deals.csv +2 -0
  43. data/sources/csv/data/organizations.csv +2 -0
  44. data/sources/csv/data/persons.csv +2 -0
  45. data/sources/csv/spec/exporter_spec.rb +17 -0
  46. data/sources/csv/spec/sample_data/coworkers.csv +2 -0
  47. data/sources/csv/spec/sample_data/deals.csv +2 -0
  48. data/sources/csv/spec/sample_data/organizations.csv +2 -0
  49. data/sources/csv/spec/sample_data/persons.csv +2 -0
  50. data/sources/csv/spec/spec_helper.rb +30 -0
  51. data/sources/easy/.gitignore +14 -0
  52. data/sources/easy/.go_import/runner.rb +115 -0
  53. data/sources/easy/Export/readme.txt +6 -0
  54. data/sources/easy/Gemfile +5 -0
  55. data/sources/easy/Rakefile.rb +7 -0
  56. data/sources/easy/converter.rb +435 -0
  57. data/sources/easy/spec/exporter_spec.rb +10 -0
  58. data/sources/easy/spec/sample_data/Company.txt +649 -0
  59. data/sources/easy/spec/spec_helper.rb +30 -0
  60. data/sources/excel/.gitignore +14 -0
  61. data/sources/excel/.go_import/runner.rb +116 -0
  62. data/sources/excel/Gemfile +6 -0
  63. data/sources/excel/Rakefile.rb +7 -0
  64. data/sources/excel/converter.rb +130 -0
  65. data/sources/excel/spec/sample_data/sample.xlsx +0 -0
  66. data/sources/excel/spec/spec_helper.rb +26 -0
  67. data/sources/excel/spec/tomodel_spec.rb +18 -0
  68. data/sources/excel/template.xlsx +0 -0
  69. data/spec/address_spec.rb +49 -0
  70. data/spec/class_settings_spec.rb +37 -0
  71. data/spec/coworker_spec.rb +94 -0
  72. data/spec/custom_field_spec.rb +22 -0
  73. data/spec/deal_class_settings_spec.rb +104 -0
  74. data/spec/deal_spec.rb +182 -0
  75. data/spec/deal_status_reference_spec.rb +17 -0
  76. data/spec/documents_spec.rb +37 -0
  77. data/spec/helpers/csv_helper_spec.rb +29 -0
  78. data/spec/helpers/email_helper_spec.rb +32 -0
  79. data/spec/helpers/phone_helper_spec.rb +97 -0
  80. data/spec/helpers/roo_helper_spec.rb +10 -0
  81. data/spec/helpers/serialize_helper_spec.rb +249 -0
  82. data/spec/helpers/xsd_validate_spec.rb +55 -0
  83. data/spec/link_spec.rb +106 -0
  84. data/spec/note_spec.rb +110 -0
  85. data/spec/organization_spec.rb +151 -0
  86. data/spec/person_spec.rb +132 -0
  87. data/spec/rootmodel_spec.rb +371 -0
  88. data/spec/spec_helper.rb +30 -0
  89. data/spec/templating_spec.rb +12 -0
  90. metadata +306 -0
@@ -0,0 +1,359 @@
1
+ # encoding: utf-8
2
+ module GoImport
3
+ # The root model for Go import. This class is the container for everything else.
4
+ class RootModel
5
+ # the import_coworker is a special coworker that is set as
6
+ # responsible for objects that requires a coworker, eg a note.
7
+ attr_accessor :import_coworker
8
+
9
+ attr_accessor :settings, :organizations, :coworkers, :deals, :notes
10
+
11
+ attr_reader :documents
12
+
13
+ def serialize_variables
14
+ [
15
+ {:id => :settings, :type => :settings},
16
+ {:id => :coworkers, :type => :coworkers},
17
+ {:id => :organizations, :type => :organizations},
18
+ {:id => :deals, :type => :deals},
19
+ {:id => :notes, :type => :notes},
20
+ {:id => :documents, :type => :documents},
21
+ ]
22
+ end
23
+
24
+ def serialize_name
25
+ "GoImport"
26
+ end
27
+
28
+ include SerializeHelper
29
+
30
+ def initialize()
31
+ @settings = Settings.new
32
+ @organizations = []
33
+ @coworkers = []
34
+ @import_coworker = Coworker.new
35
+ @import_coworker.integration_id = "import"
36
+ @import_coworker.first_name = "Import"
37
+ @coworkers.push @import_coworker
38
+ @deals = []
39
+ @notes = []
40
+ @documents = Documents.new
41
+ end
42
+
43
+ # Adds the specifed coworker object to the model.
44
+ # @example Add a coworker from a hash
45
+ # rootmodel.add_coworker({
46
+ # :integration_id=>"123",
47
+ # :first_name=>"Kalle",
48
+ # :last_name=>"Anka",
49
+ # :email=>"kalle.anka@vonanka.com"
50
+ # })
51
+ #
52
+ # @example Add a coworker from a new coworker
53
+ # coworker = GoImport::Coworker.new
54
+ # coworker.integration_id = "123"
55
+ # coworker.first_name="Kalle"
56
+ # coworker.last_name="Anka"
57
+ # coworker.email = "kalle.anka@vonanka.com"
58
+ # rootmodel.add_coworker(coworker)
59
+ #
60
+ # @example If you want to keep adding coworkers and dont care about duplicates not being added
61
+ # begin
62
+ # rootmodel.add_coworker(coworker)
63
+ # rescue GoImport::AlreadyAddedError
64
+ # puts "Warning: already added coworker"
65
+ # end
66
+ # @see Coworker
67
+ def add_coworker(coworker)
68
+ @coworkers = [] if @coworkers == nil
69
+
70
+ if coworker.nil?
71
+ return nil
72
+ end
73
+
74
+ coworker = Coworker.new(coworker) if !coworker.is_a?(Coworker)
75
+
76
+ if find_coworker_by_integration_id(coworker.integration_id) != nil
77
+ raise AlreadyAddedError, "Already added a coworker with integration_id #{coworker.integration_id}"
78
+ end
79
+
80
+ @coworkers.push(coworker)
81
+
82
+ return coworker
83
+ end
84
+
85
+ # Adds the specifed organization object to the model.
86
+ # @example Add an organization from a hash
87
+ # rootmodel.add_organization({
88
+ # :integration_id => "123",
89
+ # :name => "Beagle Boys",
90
+ # })
91
+ #
92
+ # @example Add an organization from a new organization
93
+ # organization = GoImport::Organization.new
94
+ # organization.integration_id = "123"
95
+ # organization.name = "Beagle Boys"
96
+ # rootmodel.add_organization(organization)
97
+ #
98
+ # @example If you want to keep adding organizations and dont
99
+ # care about duplicates not being added. Your model might not
100
+ # be saved due to duplicate integration_ids.
101
+ # begin
102
+ # rootmodel.add_organization(organization)
103
+ # rescue GoImport::AlreadyAddedError
104
+ # puts "Warning: already added organization"
105
+ # end
106
+ # @see Coworker
107
+ def add_organization(organization)
108
+ @organizations = [] if @organizations.nil?
109
+
110
+ if organization.nil?
111
+ return nil
112
+ end
113
+
114
+ organization = Organization.new(organization) if !organization.is_a?(Organization)
115
+
116
+ if find_organization_by_integration_id(organization.integration_id) != nil
117
+ raise AlreadyAddedError, "Already added an organization with integration_id #(organization.integration_id)"
118
+ end
119
+
120
+ @organizations.push(organization)
121
+
122
+ return organization
123
+ end
124
+
125
+ # Adds the specifed deal object to the model.
126
+ # @example Add an deal from a hash
127
+ # rootmodel.add_deal({
128
+ # :integration_id => "123",
129
+ # :name => "Big deal",
130
+ # })
131
+ #
132
+ # @example Add a deal from a new deal
133
+ # deal = GoImport::Deal.new
134
+ # deal.integration_id = "123"
135
+ # deal.name = "Big deal"
136
+ # rootmodel.add_deal(deal)
137
+ #
138
+ # @example If you want to keep adding deals and dont
139
+ # care about duplicates not being added. Your model might not
140
+ # be saved due to duplicate integration_ids.
141
+ # begin
142
+ # rootmodel.add_deal(deal)
143
+ # rescue GoImport::AlreadyAddedError
144
+ # puts "Warning: already added deal"
145
+ # end
146
+ # @see Coworker
147
+ def add_deal(deal)
148
+ @deals = [] if @deals.nil?
149
+
150
+ if deal.nil?
151
+ return nil
152
+ end
153
+
154
+ deal = Deal.new(deal) if !deal.is_a?(Deal)
155
+
156
+ if find_deal_by_integration_id(deal.integration_id) != nil
157
+ raise AlreadyAddedError, "Already added a deal with integration_id #{deal.integration_id}"
158
+ end
159
+
160
+ if deal.responsible_coworker.nil?
161
+ deal.responsible_coworker = @import_coworker
162
+ end
163
+
164
+ @deals.push(deal)
165
+
166
+ return deal
167
+ end
168
+
169
+ # Adds the specifed note object to the model.
170
+ # @example Add an deal from a hash
171
+ # rootmodel.add_note({
172
+ # :integration_id => "123",
173
+ # :text => "This is a note",
174
+ # })
175
+ #
176
+ # @example Add a note from a new note
177
+ # note = GoImport::Note.new
178
+ # note.integration_id = "123"
179
+ # note.text = "Big deal"
180
+ # rootmodel.add_note(note)
181
+ #
182
+ # @example If you want to keep adding deals and dont
183
+ # care about duplicates not being added. Your model might not
184
+ # be saved due to duplicate integration_ids.
185
+ # begin
186
+ # rootmodel.add_deal(deal)
187
+ # rescue GoImport::AlreadyAddedError
188
+ # puts "Warning: already added deal"
189
+ # end
190
+ # @see Coworker
191
+ def add_note(note)
192
+ @notes = [] if @notes == nil
193
+
194
+ if note.nil?
195
+ return nil
196
+ end
197
+
198
+ note = Note.new(note) if !note.is_a?(Note)
199
+
200
+ if (!note.integration_id.nil? && note.integration_id.length > 0) &&
201
+ find_note_by_integration_id(note.integration_id) != nil
202
+ raise AlreadyAddedError, "Already added a note with integration_id #{note.integration_id}"
203
+ end
204
+
205
+ @notes.push(note)
206
+
207
+ return note
208
+ end
209
+
210
+ def add_link(link)
211
+ @documents = Documents.new if @documents == nil
212
+
213
+ return @documents.add_link(link)
214
+ end
215
+
216
+
217
+ def find_coworker_by_integration_id(integration_id)
218
+ return @coworkers.find do |coworker|
219
+ coworker.integration_id == integration_id
220
+ end
221
+ end
222
+
223
+ def find_organization_by_integration_id(integration_id)
224
+ return @organizations.find do |organization|
225
+ organization.integration_id == integration_id
226
+ end
227
+ end
228
+
229
+ def find_person_by_integration_id(integration_id)
230
+ return nil if @organizations.nil?
231
+ @organizations.each do |organization|
232
+ person = organization.find_employee_by_integration_id(integration_id)
233
+ return person if person
234
+ end
235
+ end
236
+
237
+ def find_note_by_integration_id(integration_id)
238
+ return @notes.find do |note|
239
+ note.integration_id == integration_id
240
+ end
241
+ end
242
+
243
+ # find deals for organization using {Organization#integration_id}
244
+ def find_deals_for_organization(organization)
245
+ deals = []
246
+
247
+ deals = @deals.select do |deal|
248
+ !deal.customer.nil? && deal.customer.integration_id == organization.integration_id
249
+ end
250
+
251
+ return deals
252
+ end
253
+
254
+ def find_deal_by_integration_id(integration_id)
255
+ return @deals.find do |deal|
256
+ deal.integration_id == integration_id
257
+ end
258
+ end
259
+
260
+ # Returns a string describing problems with the data. For instance if integration_id for any entity is not unique.
261
+ def sanity_check
262
+ error = String.new
263
+
264
+ dups = get_integration_id_duplicates(with_non_empty_integration_id(@coworkers))
265
+ dups_error_items = (dups.collect{|coworker| coworker.integration_id}).compact
266
+ if dups.length > 0
267
+ error = "#{error}\nDuplicate coworker integration_id: #{dups_error_items.join(", ")}."
268
+ end
269
+
270
+ dups = get_integration_id_duplicates(with_non_empty_integration_id(@organizations))
271
+ dups_error_items = (dups.collect{|org| org.integration_id}).compact
272
+ if dups.length > 0
273
+ error = "#{error}\nDuplicate organization integration_id: #{dups_error_items.join(", ")}."
274
+ end
275
+
276
+ dups = get_integration_id_duplicates(with_non_empty_integration_id(@deals))
277
+ dups_error_items = (dups.collect{|deal| deal.integration_id}).compact
278
+ if dups_error_items.length > 0
279
+ error = "#{error}\nDuplicate deal integration_id: #{dups_error_items.join(", ")}."
280
+ end
281
+
282
+ persons = @organizations.collect{|o| o.employees}.flatten.compact
283
+ dups = get_integration_id_duplicates(with_non_empty_integration_id(persons))
284
+ dups_error_items = (dups.collect{|person| person.integration_id}).compact
285
+ if dups_error_items.length > 0
286
+ error = "#{error}\nDuplicate person integration_id: #{dups_error_items.join(", ")}."
287
+ end
288
+
289
+ dups = get_integration_id_duplicates(with_non_empty_integration_id(@documents.links))
290
+ dups_error_items = (dups.collect{|l| l.integration_id}).compact
291
+ if dups_error_items.length > 0
292
+ error = "#{error}\nDuplicate link integration_id: #{dups_error_items.join(", ")}."
293
+ end
294
+
295
+ return error.strip
296
+ end
297
+
298
+ def validate()
299
+ error = String.new
300
+
301
+ @organizations.each do |o|
302
+ validation_message = o.validate()
303
+
304
+ if !validation_message.empty?
305
+ error = "#{error}\n#{validation_message}"
306
+ end
307
+ end
308
+
309
+ @deals.each do |deal|
310
+ validation_message = deal.validate
311
+
312
+ if !validation_message.empty?
313
+ error = "#{error}\n#{validation_message}"
314
+ end
315
+ end
316
+
317
+ @notes.each do |note|
318
+ validation_message = note.validate
319
+
320
+ if !validation_message.empty?
321
+ error = "#{error}\n#{validation_message}"
322
+ end
323
+ end
324
+
325
+ @documents.links.each do |link|
326
+ validation_message = link.validate
327
+ if !validation_message.empty?
328
+ error = "#{error}\n#{validation_message}"
329
+ end
330
+ end
331
+
332
+ return error.strip
333
+ end
334
+
335
+ # @!visibility private
336
+ def to_rexml(doc)
337
+ element_name = serialize_name
338
+ elem = doc.add_element(element_name,{"Version"=>"v2_0"})
339
+ SerializeHelper::serialize_variables_rexml(elem, self)
340
+ end
341
+
342
+ private
343
+ # returns all items from the object array with duplicate integration ids.
344
+ # To get all organizations with the same integration_id use
345
+ # @example Get all the organization duplicates with the same integration id
346
+ # rm.get_integration_id_duplicates(rm.organizations)
347
+ def get_integration_id_duplicates(objects)
348
+ uniq_items = objects.uniq {|item| item.integration_id}.compact
349
+
350
+ return (objects - uniq_items).compact
351
+ end
352
+
353
+ def with_non_empty_integration_id(objects)
354
+ return objects.select do |obj|
355
+ obj.integration_id != nil && !obj.integration_id.empty?
356
+ end
357
+ end
358
+ end
359
+ end
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+ module GoImport
3
+ class Settings
4
+ include SerializeHelper
5
+ attr_reader :organization, :person, :deal
6
+
7
+ # @example Add custom fields available for organization
8
+ # rootmodel.settings.with_organization do |organization_settings|
9
+ # organization_settings.set_custom_field({:integration_id=>"link_to_bi_system", :title=>"Link to BI system"})
10
+ # organization_settings.set_custom_field({:integration_id=>"yield_quota", :title=>"Yield quota"})
11
+ # end
12
+ # @see ClassSettings
13
+ # @see CustomField
14
+ # @see RootModel
15
+ def with_organization
16
+ @organization = ClassSettings.new if @organization ==nil
17
+ yield @organization
18
+ end
19
+
20
+ # @example Add custom fields available for person
21
+ # rootmodel.settings.with_person do |person_settings|
22
+ # person_settings.set_custom_field({:integration_id=>"link_to_bi_system", :title=>"Link to BI system"})
23
+ # end
24
+ # @see ClassSettings
25
+ # @see CustomField
26
+ # @see RootModel
27
+ def with_person
28
+ @person = ClassSettings.new if @person ==nil
29
+ yield @person
30
+ end
31
+
32
+ # @example Add custom fields available for deal
33
+ # rootmodel.settings.with_deal do |deal_settings|
34
+ # deal_settings.set_custom_field({:integration_id=>"link_to_bi_system", :title=>"Link to BI system"})
35
+ # end
36
+ # @see ClassSettings
37
+ # @see CustomField
38
+ # @see RootModel
39
+ def with_deal
40
+ @deal = DealClassSettings.new if @deal ==nil
41
+ yield @deal
42
+ end
43
+
44
+ def initialize(opt = nil)
45
+ if opt != nil
46
+ serialize_variables.each do |myattr|
47
+ val = opt[myattr[:id]]
48
+ instance_variable_set("@" + myattr[:id].to_s, val) if val != nil
49
+ end
50
+ end
51
+ end
52
+
53
+ def serialize_variables
54
+ [:organization, :person, :deal].map {|p| {:id => p, :type => :class_settings} }
55
+ end
56
+
57
+ def serialize_name
58
+ "Settings"
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ module GoImport
4
+ class Tag
5
+ def serialize_name
6
+ "Tag"
7
+ end
8
+
9
+ attr_accessor :value
10
+
11
+ def initialize(val=nil)
12
+ if val
13
+ @value = val
14
+ end
15
+ end
16
+
17
+ # @!visibility private
18
+ def to_rexml(elem)
19
+ element_name = serialize_name
20
+ elem.add_element(element_name).text = @value.to_s.encode('utf-8')
21
+ end
22
+
23
+ def to_s
24
+ return "tag: '#{@value}'"
25
+ end
26
+
27
+ def ==(other)
28
+ if other.respond_to?(:value)
29
+ return @value == other.value
30
+ else
31
+ return false
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,54 @@
1
+ module GoImport
2
+ module ModelHasCustomFields
3
+ # @example
4
+ # value = row['business_value_partner_info']
5
+ # obj.set_custom_value("partner_info", value)
6
+ def set_custom_value(integration_id, value)
7
+ return set_custom_field({integration_id: integration_id, value: value})
8
+ end
9
+ # @example
10
+ # value = row['business_value_partner_info']
11
+ # obj.set_custom_field({:integration_id=>"partner_info", :value=>value})
12
+ def set_custom_field(obj)
13
+ @custom_values = [] if @custom_values == nil
14
+ value = obj[:value]
15
+ field = CustomFieldReference.new(obj)
16
+ custom_value = CustomValue.new
17
+ custom_value.value = value
18
+ custom_value.field = field
19
+ index = @custom_values.find_index do |custom_value|
20
+ custom_value.field.same_as?(field)
21
+ end
22
+ if index
23
+ @custom_values.delete_at index
24
+ end
25
+
26
+ @custom_values.push custom_value
27
+ return custom_value
28
+ end
29
+ end
30
+
31
+ module ModelWithIntegrationIdSameAs
32
+ # check if other is same as regarding integration_id or id
33
+ def same_as?(other)
34
+ if @integration_id != nil && @integration_id == other.integration_id
35
+ return true
36
+ end
37
+ if @id != nil && @id == other.id
38
+ return true
39
+ end
40
+ return false
41
+ end
42
+ end
43
+
44
+ module ModelHasTags
45
+ # @example
46
+ # obj.set_tag("partner")
47
+ def set_tag(str)
48
+ @tags = [] if @tags == nil
49
+ if ! @tags.any? {|tag| tag.value == str }
50
+ @tags.push(Tag.new(str))
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,74 @@
1
+ require 'global_phone'
2
+
3
+ module GoImport
4
+ # The PhoneHelper helps you parse and format phone number strings
5
+ # into pretty looking numbers.
6
+ class PhoneHelper
7
+ GlobalPhone.db_path = File.join(File.dirname(__FILE__), 'global_phone.json')
8
+ GlobalPhone.default_territory_name = :se
9
+
10
+ # Sets the country code used during parsning. The default is
11
+ # swedish (:se) and if you are parsing swedish numbers you
12
+ # dont need to set the country code.
13
+ def self.set_country_code(country_code)
14
+ GlobalPhone.default_territory_name = country_code
15
+ end
16
+
17
+ # Parses the specifed number_string and returns only valid
18
+ # numbers.
19
+ # @see parse_numbers
20
+ def self.parse_numbers_strict(number_string, delimiters = ',')
21
+ parse_numbers number_string, delimiters, true
22
+ end
23
+
24
+ # Parses the specified number_string into one or more phone
25
+ # numbers using the specified delimiters. If strict_mode is
26
+ # true only valid numbers are returned, otherwise are invalid
27
+ # numbers returned as found in the number_string.
28
+ #
29
+ # @example Parse a number
30
+ # number = GoImport::PhoneHelper.parse_numbers("046 - 270 48 00")
31
+ #
32
+ # @example Parses a string with two numbers and a custom delimiter
33
+ # source = "046 - 270 48 00/ 031-712 44 00"
34
+ # number1, number2 = GoImport::PhoneHelper.parse_numbers(source, '/')
35
+ def self.parse_numbers(number_string, delimiters = ',', strict_mode = false)
36
+ numbers = []
37
+
38
+ if delimiters.is_a?(Array)
39
+ # we have several delimiters, replace all delimiters
40
+ # in the number_string with the first delimiter
41
+ delimiters.each do |del|
42
+ number_string = number_string.sub(del, delimiters[0])
43
+ end
44
+ delimiter = delimiters[0]
45
+ elsif delimiters.is_a?(String)
46
+ delimiter = delimiters
47
+ else
48
+ raise "delimiters should be either a string or and array of strings"
49
+ end
50
+
51
+ number_string.split(delimiter).each do |possible_number|
52
+ if !possible_number.empty?
53
+ number = GlobalPhone.parse([possible_number])
54
+
55
+ if !number.nil? && number.valid?
56
+ numbers.push number.to_s
57
+ else
58
+ if !strict_mode
59
+ numbers.push possible_number
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ if numbers.length == 0
66
+ return ""
67
+ elsif numbers.length == 1
68
+ return numbers[0]
69
+ else
70
+ return numbers
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,80 @@
1
+ require "csv"
2
+ module GoImport
3
+ # @example transform xlsx file into rows
4
+ # organizations_path = File.join(File.dirname(__FILE__), 'organizations.xlsx') # same path as this file
5
+ # rows = GoImport::RooHelper.new(Roo::Excelx.new(organizations_path)).rows
6
+ class RooHelper
7
+
8
+ def initialize(data)
9
+ @data = data
10
+ @default_sheet = data.sheets.first
11
+ end
12
+
13
+ # Get rows for the first sheet.
14
+ # The rows are hashes of the first row of cells as header cells and the rest as content.
15
+ # @example If the header 'Name' and the second column contains 'Johan'.
16
+ # GoImport::RooHelper.new(Roo::Excelx.new(file_path)).rows
17
+ # # returns:
18
+ # [{'Name'=>'Johan'}]
19
+ def rows
20
+ return rows_for_sheet(@default_sheet)
21
+ end
22
+
23
+ # Returns true if the current workbook has a sheet with the
24
+ # specifed name. This is case sensitive.
25
+ def has_sheet?(name)
26
+ sheet = @data.sheets.find { |s| s.to_s == name}
27
+
28
+ return !sheet.nil?
29
+ end
30
+
31
+ # @example transform xlsx file into rows for the second sheet
32
+ # data = Roo::Excelx.new(organizations_path)
33
+ # rows = GoImport::RooHelper.new(data).rows_for_sheet(data.sheets[1])
34
+ def rows_for_sheet(sheet)
35
+ column_headers = {}
36
+ 1.upto(@data.last_column(sheet)) do |col|
37
+ column_headers[col] = @data.cell(1, col, sheet).encode('UTF-8')
38
+ end
39
+
40
+ rs = []
41
+ 2.upto(@data.last_row(sheet)) do |row|
42
+ r = {}
43
+ 1.upto(@data.last_column(sheet)) do |col|
44
+ val = cell_to_csv(row, col, sheet)
45
+ r[column_headers[col]] = val
46
+ end
47
+ rs.push(r)
48
+ end
49
+ return rs
50
+ end
51
+
52
+ def cell_to_csv(row, col, sheet)
53
+ if @data.empty?(row,col,sheet)
54
+ ''
55
+ else
56
+ onecell = @data.cell(row,col,sheet)
57
+ case @data.celltype(row,col,sheet)
58
+ when :string
59
+ unless onecell.empty?
60
+ onecell.encode('UTF-8').strip
61
+ end
62
+ when :float, :percentage
63
+ if onecell == onecell.to_i
64
+ onecell.to_i.to_s
65
+ else
66
+ onecell.to_s
67
+ end
68
+ when :date, :datetime
69
+ onecell.to_s
70
+ when :time
71
+ Roo::Base.integer_to_timestring(onecell)
72
+ when :formula
73
+ onecell.to_s
74
+ else
75
+ raise "unhandled celltype #{@data.celltype(row,col,sheet)} for cell at row: #{row}, col: #{col} in sheet #{sheet}"
76
+ end || ""
77
+ end
78
+ end
79
+ end
80
+ end