con_sync 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6313be82bd29a65d60a29a4523c75b53e89b06a6
4
- data.tar.gz: b14e7c31eddd7485572b946817fc35a744c687c3
3
+ metadata.gz: 8d86209bbb3f62c04ce58f9cdfcb59c8bf302683
4
+ data.tar.gz: a1516a7c9f0a497ddaec8bce9e6163c9d2dcf6a6
5
5
  SHA512:
6
- metadata.gz: c0fa8d53d305c2a1859d83deac2e2b11719cf6b256ac17b7e05d212907b77af42e1dcbc52d512cfa93455a18007f2cff47d523e09f5c6cb02b6d67485342b9d4
7
- data.tar.gz: 4735d50bd5b2f0e95ab6664b1a8fb2f489c2172c85c0fb7f92b07e737a6b74ea2aeb674d3a6373d7f54e041e9cba1f0f49be639ad7aef399e42273aad2d158c2
6
+ metadata.gz: 2585d886cbb33adca81ed594a7f0af6183332fe6284e5a74f3e90e14e05b96985e58bfa15706c475f2636636bfea4a73bb2e973aa23b86f9cbe8c4105a9889be
7
+ data.tar.gz: f8eec7cf960d303c83a99f8bf4a03ed43843f734ea3a0106851b9d3b50d2fac109d523516cfc2abc437067f6e1f932fb3f3d2f06391ba9ce5b41f25db253e465
data/app/models/user.rb CHANGED
@@ -1,3 +1,108 @@
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
3
108
  end
@@ -0,0 +1,10 @@
1
+ class ApiConstraints
2
+ def initialize(options)
3
+ @version = options[:version]
4
+ @default = options[:default]
5
+ end
6
+
7
+ def matches?(req)
8
+ @default || req.headers['Accept'].include?("application/vnd.kanari.v#{@version}")
9
+ end
10
+ end
data/lib/con_sync.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  require "con_sync/engine"
2
-
2
+ require 'api_constraints'
3
3
  module ConSync
4
4
  end
@@ -1,3 +1,3 @@
1
1
  module ConSync
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -12,4 +12,8 @@ class InstallConSyncGenerator < Rails::Generators::Base
12
12
  def generate_migration
13
13
  migration_template "create_con_sync_schema.rb", "db/migrate/create_con_sync.rb"
14
14
  end
15
+
16
+ def generate_routes
17
+ route(File.read(File.join(File.dirname(__FILE__), 'templates/routes.rb') ))
18
+ end
15
19
  end
@@ -0,0 +1,9 @@
1
+
2
+ namespace :api do
3
+ scope module: :v1, constraints: ApiConstraints.new(version: 1, default: :true) do
4
+ resources :users, only: [] do
5
+ post :sync_contacts
6
+ get :match_contacts
7
+ end
8
+ end
9
+ 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.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Saad Masood
@@ -63,12 +63,14 @@ files:
63
63
  - app/models/user.rb
64
64
  - app/views/layouts/con_sync/application.html.erb
65
65
  - config/routes.rb
66
+ - lib/api_constraints.rb
66
67
  - lib/con_sync.rb
67
68
  - lib/con_sync/engine.rb
68
69
  - lib/con_sync/version.rb
69
70
  - lib/generators/install_con_sync/USAGE
70
71
  - lib/generators/install_con_sync/install_con_sync_generator.rb
71
72
  - lib/generators/install_con_sync/templates/create_con_sync_schema.rb
73
+ - lib/generators/install_con_sync/templates/routes.rb
72
74
  - lib/tasks/con_sync_tasks.rake
73
75
  - test/con_sync_test.rb
74
76
  - test/controllers/con_sync/api/users_sync_controller_test.rb