con_sync 0.0.8 → 0.1.0

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: 8d86209bbb3f62c04ce58f9cdfcb59c8bf302683
4
- data.tar.gz: a1516a7c9f0a497ddaec8bce9e6163c9d2dcf6a6
3
+ metadata.gz: 82e367702922374dd78c4d389a9eefd78460e909
4
+ data.tar.gz: 54714bd28231601afeaaeee6fb374a6938baa4a3
5
5
  SHA512:
6
- metadata.gz: 2585d886cbb33adca81ed594a7f0af6183332fe6284e5a74f3e90e14e05b96985e58bfa15706c475f2636636bfea4a73bb2e973aa23b86f9cbe8c4105a9889be
7
- data.tar.gz: f8eec7cf960d303c83a99f8bf4a03ed43843f734ea3a0106851b9d3b50d2fac109d523516cfc2abc437067f6e1f932fb3f3d2f06391ba9ce5b41f25db253e465
6
+ metadata.gz: 3d1bbe854d843f33f5ef44b3a3c95e9c65da9647b24d3ce218f5070a971363b15b6ea4ee94fb4dd4f17a84c30f2af611d6cd993e28dbe5723e747c3e07a6306f
7
+ data.tar.gz: 51aa12a4ff92dad07cd5624ce2708a90c2672f38725f2c13cac870bb31344077699a9e4cb591f33ee13ee32d41a2c8f4038371d96dd08065c9200664b0526fdd
data/app/models/user.rb CHANGED
@@ -1,108 +1,3 @@
1
1
  class User < ActiveRecord::Base
2
2
  has_many :contacts
3
-
4
-
5
- def sync_contacts(contact_hash = {})
6
- result = {:new => {success:[], failed:[]}, modified: {success:[], failed:[]}, deleted: {success:[], failed:[]}}
7
- raise ArgumentError, "You need to provide contacts hash." if contact_hash.blank?
8
-
9
- ##########################################################################
10
- #########################Create new Contacts##############################
11
- if !contact_hash[:new].blank?
12
- contacts = contact_hash[:new]
13
- contacts.each do |aContact|
14
- phones = aContact[:phones]
15
- emails = aContact[:emails]
16
-
17
- aContact.delete :phones
18
- aContact.delete :emails
19
-
20
- newContact = Contact.new(contact_params(aContact))
21
- if phones
22
- phones.each do |aPhone|
23
- newContact.phones.build(phone_params(aPhone))
24
- end
25
- end
26
- if emails
27
- emails.each do |anEmail|
28
- newContact.emails.build(email_params(anEmail))
29
- end
30
- end
31
- if newContact.save
32
- # result[:new][:success] << newContact.record_id
33
- self.contacts << newContact
34
- else
35
- result[:new][:failed] << newContact.record_id
36
- end
37
- end
38
- end
39
- ##########################################################################
40
- ########################Modify Contacts###################################
41
- if !contact_hash[:modified].blank?
42
- modified_contacts = contact_hash[:modified]
43
- modified_contacts.each do |con|
44
- theContact = self.contacts.find_by_record_id(con[:record_id])
45
- if ! theContact.blank?
46
- if !theContact.update_attributes(contact_params(con))
47
- result[:modified][:failed] << theContact.record_id
48
- puts "Error Updating Contact."
49
- else
50
- # result[:modified][:success] << theContact.record_id
51
- end
52
- end
53
- end
54
- end
55
- ##########################################################################
56
- ########################Delete Contacts###################################
57
- if !contact_hash[:deleted].blank?
58
- delete_contacts = contact_hash[:deleted]
59
- delete_contacts.each do |con|
60
- if self.contacts.find_by_record_id(con[:record_id].to_i).destroy
61
- # result[:deleted][:success] << con[:record_id]
62
- else
63
- result[:deleted][:failed] << con[:record_id]
64
- end
65
- end
66
- end
67
- after_contact_sync
68
- self.save
69
- return result
70
- end
71
-
72
- def matched_contacts
73
- matched_contacts = []
74
- user_phones = []
75
- user_emails = []
76
- self.contacts.each do |con|
77
- user_phones.concat con.phones
78
- user_emails.concat con.emails
79
- end
80
-
81
- user_phones.each do |phone|
82
- u = User.where(phone_number: phone.number.encrypt(:symmetric)).limit(1).first
83
- matched_contacts << u unless u.blank?
84
- end
85
- user_emails.each do |mail|
86
- u = User.where(email: mail.email).limit(1).first
87
- matched_contacts << u unless u.blank?
88
- end
89
- return matched_contacts
90
- end
91
-
92
- private
93
- def contact_params(aContact)
94
- 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)
95
- end
96
-
97
- def phone_params aPhone
98
- aPhone.permit(:label, :number)
99
- end
100
-
101
- def email_params anEmail
102
- anEmail.permit(:label, :email)
103
- end
104
-
105
- def after_contact_sync
106
- self.last_contact_sync = DateTime.now.utc
107
- end
108
3
  end
@@ -1,3 +1,3 @@
1
1
  module ConSync
2
- VERSION = "0.0.8"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/con_sync.rb CHANGED
@@ -1,4 +1,117 @@
1
1
  require "con_sync/engine"
2
2
  require 'api_constraints'
3
3
  module ConSync
4
+ module ActsAsSyncable
5
+ def acts_as_syncable
6
+ include Syncable
7
+ end
8
+ end
9
+
10
+ module Syncable
11
+ def sync_contacts(contact_hash = {})
12
+ result = {:new => {success:[], failed:[]}, modified: {success:[], failed:[]}, deleted: {success:[], failed:[]}}
13
+ raise ArgumentError, "You need to provide contacts hash." if contact_hash.blank?
14
+
15
+ ##########################################################################
16
+ #########################Create new Contacts##############################
17
+ if !contact_hash[:new].blank?
18
+ contacts = contact_hash[:new]
19
+ contacts.each do |aContact|
20
+ phones = aContact[:phones]
21
+ emails = aContact[:emails]
22
+
23
+ aContact.delete :phones
24
+ aContact.delete :emails
25
+
26
+ newContact = Contact.new(contact_params(aContact))
27
+ if phones
28
+ phones.each do |aPhone|
29
+ newContact.phones.build(phone_params(aPhone))
30
+ end
31
+ end
32
+ if emails
33
+ emails.each do |anEmail|
34
+ newContact.emails.build(email_params(anEmail))
35
+ end
36
+ end
37
+ if newContact.save
38
+ # result[:new][:success] << newContact.record_id
39
+ self.contacts << newContact
40
+ else
41
+ result[:new][:failed] << newContact.record_id
42
+ end
43
+ end
44
+ end
45
+ ##########################################################################
46
+ ########################Modify Contacts###################################
47
+ if !contact_hash[:modified].blank?
48
+ modified_contacts = contact_hash[:modified]
49
+ modified_contacts.each do |con|
50
+ theContact = self.contacts.find_by_record_id(con[:record_id])
51
+ if ! theContact.blank?
52
+ if !theContact.update_attributes(contact_params(con))
53
+ result[:modified][:failed] << theContact.record_id
54
+ puts "Error Updating Contact."
55
+ else
56
+ # result[:modified][:success] << theContact.record_id
57
+ end
58
+ end
59
+ end
60
+ end
61
+ ##########################################################################
62
+ ########################Delete Contacts###################################
63
+ if !contact_hash[:deleted].blank?
64
+ delete_contacts = contact_hash[:deleted]
65
+ delete_contacts.each do |con|
66
+ if self.contacts.find_by_record_id(con[:record_id].to_i).destroy
67
+ # result[:deleted][:success] << con[:record_id]
68
+ else
69
+ result[:deleted][:failed] << con[:record_id]
70
+ end
71
+ end
72
+ end
73
+ after_contact_sync
74
+ self.save
75
+ return result
76
+ end
77
+
78
+ def matched_contacts
79
+ matched_contacts = []
80
+ user_phones = []
81
+ user_emails = []
82
+ self.contacts.each do |con|
83
+ user_phones.concat con.phones
84
+ user_emails.concat con.emails
85
+ end
86
+
87
+ user_phones.each do |phone|
88
+ u = User.where(phone_number: phone.number.encrypt(:symmetric)).limit(1).first
89
+ matched_contacts << u unless u.blank?
90
+ end
91
+ user_emails.each do |mail|
92
+ u = User.where(email: mail.email).limit(1).first
93
+ matched_contacts << u unless u.blank?
94
+ end
95
+ return matched_contacts
96
+ end
97
+
98
+ private
99
+ def contact_params(aContact)
100
+ 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)
101
+ end
102
+
103
+ def phone_params aPhone
104
+ aPhone.permit(:label, :number)
105
+ end
106
+
107
+ def email_params anEmail
108
+ anEmail.permit(:label, :email)
109
+ end
110
+
111
+ def after_contact_sync
112
+ self.last_contact_sync = DateTime.now.utc
113
+ end
114
+ end
4
115
  end
116
+
117
+ ActiveRecord::Base.send :extend, ConSync::ActsAsSyncable
@@ -10,7 +10,7 @@ class InstallConSyncGenerator < Rails::Generators::Base
10
10
  source_root File.expand_path('../templates', __FILE__)
11
11
 
12
12
  def generate_migration
13
- migration_template "create_con_sync_schema.rb", "db/migrate/create_con_sync.rb"
13
+ migration_template "create_con_sync_schema.rb", "db/migrate/create_con_sync_schema.rb"
14
14
  end
15
15
 
16
16
  def generate_routes
@@ -1,5 +1,5 @@
1
1
  class CreateConSyncSchema < ActiveRecord::Migration
2
- def change
2
+ def up
3
3
  create_table :contacts do |t|
4
4
  t.string :composite_name
5
5
  t.string :first_name
@@ -39,11 +39,23 @@ class CreateConSyncSchema < ActiveRecord::Migration
39
39
  t.timestamps
40
40
  end
41
41
 
42
-
43
- add_column :users, :last_contact_sync, :datetime
44
- add_column :users, :encrypted_number, :string
45
- add_column :users, :cc_prefix, :string
46
- add_index :users, :encrypted_number
42
+ if table_exists? :users
43
+ add_column :users, :last_contact_sync, :datetime
44
+ add_column :users, :encrypted_number, :string
45
+ add_column :users, :cc_prefix, :string
46
+ add_index :users, :encrypted_number
47
+ else
48
+ create_table :users do |t|
49
+ t.string :first_name
50
+ t.string :middle_name
51
+ t.string :last_name
52
+ t.string :email
53
+ t.string :encrypted_number
54
+ t.string :cc_prefix
55
+ t.datetime :last_contact_sync
56
+ end
57
+ add_index :users, :encrypted_number
58
+ end
47
59
 
48
60
  add_index :contacts, [:record_id, :device_id], name: "unique_device_contact", unique: true
49
61
  add_index :contacts, :user_id
@@ -51,4 +63,8 @@ class CreateConSyncSchema < ActiveRecord::Migration
51
63
  add_index :emails, :encrypted_email
52
64
 
53
65
  end
66
+
67
+ def down
68
+
69
+ end
54
70
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: con_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Saad Masood
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-18 00:00:00.000000000 Z
11
+ date: 2015-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: global_phone
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: encrypted_strings
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.3.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.3.3
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: sqlite3
29
57
  requirement: !ruby/object:Gem::Requirement