con_sync 0.1.1 → 0.1.3

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.
data/lib/con_sync.rb CHANGED
@@ -1,128 +1,12 @@
1
1
  require "con_sync/engine"
2
- require 'api_constraints'
3
- module ConSync
4
- module ActsAsSyncable
5
- def acts_as_syncable
6
- include Syncable
7
- end
8
- end
9
-
10
- module Syncable
11
-
12
- def phone_number
13
- return self.encrypted_number.try(:decrypt,:symmetric) || ""
14
- end
15
-
16
- def phone_number=(string)
17
- self.cc_prefix, num = string.extract_country_code
18
- self.encrypted_number = num.encrypt(:symmetric)
19
- self.encrypted_number
20
- end
21
-
22
- def sync_contacts(contact_hash = {})
23
- result = {:new => {success:[], failed:[]}, modified: {success:[], failed:[]}, deleted: {success:[], failed:[]}}
24
- raise ArgumentError, "You need to provide contacts hash." if contact_hash.blank?
25
-
26
- ##########################################################################
27
- #########################Create new Contacts##############################
28
- if !contact_hash[:new].blank?
29
- contacts = contact_hash[:new]
30
- contacts.each do |aContact|
31
- phones = aContact[:phones]
32
- emails = aContact[:emails]
33
-
34
- aContact.delete :phones
35
- aContact.delete :emails
2
+ require 'con_sync/util/api_constraints'
3
+ require 'con_sync/util/string_phone'
4
+ require 'con_sync/util/global_phone'
5
+ require 'con_sync/acts_as_syncable'
6
+ require 'con_sync/syncable'
7
+ require 'encrypted_strings'
36
8
 
37
- newContact = Contact.new(contact_params(aContact))
38
- if phones
39
- phones.each do |aPhone|
40
- newContact.phones.build(phone_params(aPhone))
41
- end
42
- end
43
- if emails
44
- emails.each do |anEmail|
45
- newContact.emails.build(email_params(anEmail))
46
- end
47
- end
48
- if newContact.save
49
- # result[:new][:success] << newContact.record_id
50
- self.contacts << newContact
51
- else
52
- result[:new][:failed] << newContact.record_id
53
- end
54
- end
55
- end
56
- ##########################################################################
57
- ########################Modify Contacts###################################
58
- if !contact_hash[:modified].blank?
59
- modified_contacts = contact_hash[:modified]
60
- modified_contacts.each do |con|
61
- theContact = self.contacts.find_by_record_id(con[:record_id])
62
- if ! theContact.blank?
63
- if !theContact.update_attributes(contact_params(con))
64
- result[:modified][:failed] << theContact.record_id
65
- puts "Error Updating Contact."
66
- else
67
- # result[:modified][:success] << theContact.record_id
68
- end
69
- end
70
- end
71
- end
72
- ##########################################################################
73
- ########################Delete Contacts###################################
74
- if !contact_hash[:deleted].blank?
75
- delete_contacts = contact_hash[:deleted]
76
- delete_contacts.each do |con|
77
- if self.contacts.find_by_record_id(con[:record_id].to_i).destroy
78
- # result[:deleted][:success] << con[:record_id]
79
- else
80
- result[:deleted][:failed] << con[:record_id]
81
- end
82
- end
83
- end
84
- after_contact_sync
85
- self.save
86
- return result
87
- end
88
-
89
- def matched_contacts
90
- matched_contacts = []
91
- user_phones = []
92
- user_emails = []
93
- self.contacts.each do |con|
94
- user_phones.concat con.phones
95
- user_emails.concat con.emails
96
- end
97
-
98
- user_phones.each do |phone|
99
- u = User.where(phone_number: phone.number.encrypt(:symmetric)).limit(1).first
100
- matched_contacts << u unless u.blank?
101
- end
102
- user_emails.each do |mail|
103
- u = User.where(email: mail.email).limit(1).first
104
- matched_contacts << u unless u.blank?
105
- end
106
- return matched_contacts
107
- end
108
-
109
- private
110
- def contact_params(aContact)
111
- aContact.permit(:composite_name, :first_name, :middle_name, :last_name, :prefix, :suffix, :nickname, :job_title, :department, :organization, :birthdate, :note, :creation_date, :modification_date, :record_id)
112
- end
113
-
114
- def phone_params aPhone
115
- aPhone.permit(:label, :number)
116
- end
117
-
118
- def email_params anEmail
119
- anEmail.permit(:label, :email)
120
- end
121
-
122
- def after_contact_sync
123
- self.last_contact_sync = DateTime.now.utc
124
- end
125
- end
9
+ module ConSync
126
10
  end
127
11
 
128
12
  ActiveRecord::Base.send :extend, ConSync::ActsAsSyncable
@@ -0,0 +1,7 @@
1
+ module ConSync
2
+ module ActsAsSyncable
3
+ def acts_as_syncable
4
+ include ConSync::Syncable
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,117 @@
1
+ module ConSync
2
+ module Syncable
3
+ def phone_number
4
+ return self.encrypted_number.try(:decrypt,:symmetric) || ""
5
+ end
6
+
7
+ def phone_number=(string)
8
+ self.cc_prefix, num = string.extract_country_code
9
+ self.encrypted_number = num.encrypt(:symmetric)
10
+ self.encrypted_number
11
+ end
12
+
13
+ def sync_contacts(contact_hash = {})
14
+ result = {:new => {success:[], failed:[]}, modified: {success:[], failed:[]}, deleted: {success:[], failed:[]}}
15
+ raise ArgumentError, "You need to provide contacts hash." if contact_hash.blank?
16
+
17
+ ##########################################################################
18
+ #########################Create new Contacts##############################
19
+ if !contact_hash[:new].blank?
20
+ contacts = contact_hash[:new]
21
+ contacts.each do |aContact|
22
+ phones = aContact[:phones]
23
+ emails = aContact[:emails]
24
+
25
+ aContact.delete :phones
26
+ aContact.delete :emails
27
+
28
+ newContact = Contact.new(contact_params(aContact))
29
+ if phones
30
+ phones.each do |aPhone|
31
+ newContact.phones.build(phone_params(aPhone))
32
+ end
33
+ end
34
+ if emails
35
+ emails.each do |anEmail|
36
+ newContact.emails.build(email_params(anEmail))
37
+ end
38
+ end
39
+ if newContact.save
40
+ # result[:new][:success] << newContact.record_id
41
+ self.contacts << newContact
42
+ else
43
+ result[:new][:failed] << newContact.record_id
44
+ end
45
+ end
46
+ end
47
+ ##########################################################################
48
+ ########################Modify Contacts###################################
49
+ if !contact_hash[:modified].blank?
50
+ modified_contacts = contact_hash[:modified]
51
+ modified_contacts.each do |con|
52
+ theContact = self.contacts.find_by_record_id(con[:record_id])
53
+ if ! theContact.blank?
54
+ if !theContact.update_attributes(contact_params(con))
55
+ result[:modified][:failed] << theContact.record_id
56
+ puts "Error Updating Contact."
57
+ else
58
+ # result[:modified][:success] << theContact.record_id
59
+ end
60
+ end
61
+ end
62
+ end
63
+ ##########################################################################
64
+ ########################Delete Contacts###################################
65
+ if !contact_hash[:deleted].blank?
66
+ delete_contacts = contact_hash[:deleted]
67
+ delete_contacts.each do |con|
68
+ if self.contacts.find_by_record_id(con[:record_id].to_i).destroy
69
+ # result[:deleted][:success] << con[:record_id]
70
+ else
71
+ result[:deleted][:failed] << con[:record_id]
72
+ end
73
+ end
74
+ end
75
+ after_contact_sync
76
+ self.save
77
+ return result
78
+ end
79
+
80
+ def matched_contacts
81
+ matched_contacts = []
82
+ user_phones = []
83
+ user_emails = []
84
+ self.contacts.each do |con|
85
+ user_phones.concat con.phones
86
+ user_emails.concat con.emails
87
+ end
88
+
89
+ user_phones.each do |phone|
90
+ u = User.where(phone_number: phone.number.encrypt(:symmetric)).limit(1).first
91
+ matched_contacts << u unless u.blank?
92
+ end
93
+ user_emails.each do |mail|
94
+ u = User.where(email: mail.email).limit(1).first
95
+ matched_contacts << u unless u.blank?
96
+ end
97
+ return matched_contacts
98
+ end
99
+
100
+ private
101
+ def contact_params(aContact)
102
+ aContact.permit(:composite_name, :first_name, :middle_name, :last_name, :prefix, :suffix, :nickname, :job_title, :department, :organization, :birthdate, :note, :creation_date, :modification_date, :record_id)
103
+ end
104
+
105
+ def phone_params aPhone
106
+ aPhone.permit(:label, :number)
107
+ end
108
+
109
+ def email_params anEmail
110
+ anEmail.permit(:label, :email)
111
+ end
112
+
113
+ def after_contact_sync
114
+ self.last_contact_sync = DateTime.now.utc
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,3 @@
1
+ require 'global_phone'
2
+ puts "\n\n\n\n\n\n\n\n\n\n\n#{ConSync::Engine.root}\n\n\n\n\n\n\n\n"
3
+ GlobalPhone.db_path = ConSync::Engine.root.join('db/global_phone.json')
@@ -0,0 +1,28 @@
1
+ require 'active_support/concern'
2
+
3
+ module ConSync
4
+ module StringPhone
5
+ # extend ActiveSupport::Concern
6
+ def extract_country_code
7
+ string = self
8
+ if string[0..1] == "00"
9
+ string[0..1] = "+"
10
+ end
11
+ if string[0] == "+"
12
+ breakdown = GlobalPhone.parse(string)
13
+ prefix = breakdown.country_code
14
+ num = breakdown.national_string
15
+ return [prefix,num]
16
+ elsif string[0] == "0"
17
+ num = string[1..-1]
18
+ prefix = "0"
19
+ return [prefix, num]
20
+ else
21
+ return ["", string]
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ puts "Including StringPhoner Module to: '#{String.name}' class"
28
+ String.send(:include, ConSync::StringPhone)
@@ -1,3 +1,3 @@
1
1
  module ConSync
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -16,4 +16,8 @@ class InstallConSyncGenerator < Rails::Generators::Base
16
16
  def generate_routes
17
17
  route(File.read(File.join(File.dirname(__FILE__), 'templates/routes.rb') ))
18
18
  end
19
+
20
+ # def generate_global_phone_db
21
+ # copy_file "global_phone.json", "db/global_phone.json"
22
+ # end
19
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: con_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Saad Masood
@@ -91,9 +91,14 @@ files:
91
91
  - app/models/user.rb
92
92
  - app/views/layouts/con_sync/application.html.erb
93
93
  - config/routes.rb
94
- - lib/api_constraints.rb
94
+ - db/global_phone.json
95
95
  - lib/con_sync.rb
96
+ - lib/con_sync/acts_as_syncable.rb
96
97
  - lib/con_sync/engine.rb
98
+ - lib/con_sync/syncable.rb
99
+ - lib/con_sync/util/api_constraints.rb
100
+ - lib/con_sync/util/global_phone.rb
101
+ - lib/con_sync/util/string_phone.rb
97
102
  - lib/con_sync/version.rb
98
103
  - lib/generators/install_con_sync/USAGE
99
104
  - lib/generators/install_con_sync/install_con_sync_generator.rb