rails-contact 0.1.5 → 0.1.7

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
  SHA256:
3
- metadata.gz: f08d1e4fe3f704acfbf960d33948277264f3ebb56ae72999d02a281526eba72b
4
- data.tar.gz: 746e6e1bc998fc23ef6407eda47f04483c227c8df8ffa7f96fab4fc24d53f755
3
+ metadata.gz: 48540c1897658b886292f1fafe8157fa2aa7fed817d9b9ebb64213456ac958e3
4
+ data.tar.gz: 3329ba043c33922528e3736e8a77d739a71151c30d471bc300ef832131ab09fd
5
5
  SHA512:
6
- metadata.gz: 34ad0a97ca7e0eeb7b566e278db0d83b2102fb6e6c3fc67fe3880a33c6fc0cea060c6282f4614834980ff7ed04d4ccec00c8863f09f6c01a92e01b82bc96e05a
7
- data.tar.gz: 3a8f05a97f69d001580847192a4eebe98284b997cccb230c21e97556d4da504ac4b682e12a8c616cc43d0734e73ef851f450fc3e9736f9bf148979bc4ed626cd
6
+ metadata.gz: 34f85c62bf91b52739ad8833e884a44ac142c44b507eda5c1c1044320cda3fba787c1d20afa3d4eb196df81cf02dd806b593ef1b8d06d383790741bd80b055e1
7
+ data.tar.gz: 4ddb497c06c1fc0fed8ad0b5a636f5ff640afe425e1d1fe0b40c2acc9ab9500bc0b473eab23be1bde9eacfb29c972b63bf3e10095db7ed2f19cd3bdf169d7bbb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.7
4
+
5
+ - Google People API: `updateContact` sends required `updatePersonFields`; update payloads include `resourceName` and `etag`; sync preloads emails, phones, and addresses.
6
+ - Google: optional `google_contact_family_name_suffix` (and env `RAILS_CONTACT_GOOGLE_CONTACT_FAMILY_NAME_SUFFIX`) for family name in sync payloads only.
7
+ - Google: payload mapper skips blank emails/phones, omits empty association arrays, biography uses `TEXT_PLAIN` when present.
8
+ - Elasticsearch search backend reuses one `Elasticsearch::Client` per process (fewer product-check warnings).
9
+ - Remove CSV import from the gem (keep imports in the host application). Drops `rails_contact:import_csv` and `Csv::ImportService`.
10
+ - Engine registers stylesheet/javascript paths for Propshaft.
11
+
3
12
  ## 0.1.5
4
13
 
5
14
  - Scope engine stylesheet to contacts content only (`.rails-contact-page`) so host app layout/header styling is not overridden.
data/README.md CHANGED
@@ -9,7 +9,7 @@ It provides:
9
9
  - labels/tags
10
10
  - dynamic add/remove nested rows
11
11
  - Elasticsearch-backed search with DB fallback
12
- - CSV import and Google sync scaffolding
12
+ - Google sync scaffolding
13
13
  - merge and bulk-delete operations
14
14
  - Devise-style override generators
15
15
 
@@ -128,6 +128,8 @@ Rails::Contact.configure do |config|
128
128
  config.google_sync_enabled = false
129
129
  config.google_max_contacts = 25_000
130
130
  config.rolling_window_sort = :updated_at
131
+ # Optional: appended to familyName in Google People API payloads only (blank = unchanged).
132
+ # config.google_contact_family_name_suffix = "_by_vendor"
131
133
  config.default_per_page = 25
132
134
  end
133
135
  ```
@@ -139,7 +141,6 @@ end
139
141
  ```bash
140
142
  rake rails_contact:reindex
141
143
  rake rails_contact:sync_google
142
- rake rails_contact:import_csv CSV_PATH=/absolute/path/to/eq.csv
143
144
  ```
144
145
 
145
146
  ---
@@ -46,7 +46,7 @@ This matrix tracks feature parity for `rails-contact` versus Google Contacts.
46
46
 
47
47
  | Capability | Google Contacts | rails-contact Target | Priority | Acceptance Criteria |
48
48
  |---|---|---|---|---|
49
- | CSV import | Flexible column import | Same | P0 | Mapping profile handles eq.csv fields |
49
+ | CSV import | Flexible column import | Host app | P0 | Engine stays format-agnostic; host maps columns to models |
50
50
  | CSV export | Download contacts | Same | P1 | Export with stable headers and escaping |
51
51
  | Google sync | People API sync | Similar | P0/P1 | Two-way sync for rolling window with conflict policy |
52
52
  | Conflict resolution | Last write + ETag semantics | Same policy | P1 | Deterministic merge outcomes and tests |
@@ -11,6 +11,8 @@ Rails::Contact.configure do |config|
11
11
  config.google_client_secret = ENV["GOOGLE_CLIENT_SECRET"]
12
12
  config.google_redirect_uri = ENV["GOOGLE_REDIRECT_URI"]
13
13
  config.google_token_path = ENV.fetch("RAILS_CONTACT_GOOGLE_TOKEN_PATH", "tmp/rails_contact_google_token.json")
14
+ # Optional: suffix appended to family name only in Google sync payloads (e.g. "_by_vendor"). Leave unset to disable.
15
+ # config.google_contact_family_name_suffix = ENV["RAILS_CONTACT_GOOGLE_CONTACT_FAMILY_NAME_SUFFIX"]&.presence
14
16
 
15
17
  config.default_per_page = 25
16
18
  end
@@ -5,7 +5,8 @@ module Rails
5
5
  :google_sync_enabled, :google_max_contacts, :rolling_window_sort,
6
6
  :google_client_id, :google_client_secret, :google_redirect_uri,
7
7
  :google_token_path, :reset_index_on_boot, :default_per_page,
8
- :inherit_host_layout
8
+ :inherit_host_layout,
9
+ :google_contact_family_name_suffix
9
10
 
10
11
  def initialize
11
12
  @contact_class_name = "Rails::Contact::Contact"
@@ -18,6 +19,8 @@ module Rails
18
19
  @google_client_secret = ENV["GOOGLE_CLIENT_SECRET"]
19
20
  @google_redirect_uri = ENV["GOOGLE_REDIRECT_URI"]
20
21
  @google_token_path = ENV.fetch("RAILS_CONTACT_GOOGLE_TOKEN_PATH", "tmp/rails_contact_google_token.json")
22
+ # Optional: appended to familyName in Google People payloads only (not stored on Contact). Blank = disabled.
23
+ @google_contact_family_name_suffix = ENV["RAILS_CONTACT_GOOGLE_CONTACT_FAMILY_NAME_SUFFIX"]&.presence
21
24
  @reset_index_on_boot = false
22
25
  @default_per_page = 25
23
26
  # When true (default), engine pages use the host app +layout+ named +application+ so
@@ -13,6 +13,11 @@ module Rails
13
13
  app.config.filter_parameters += %i[google_access_token google_refresh_token authorization]
14
14
  end
15
15
 
16
+ initializer "rails_contact.assets" do |app|
17
+ app.config.assets.paths << root.join("app/assets/stylesheets")
18
+ app.config.assets.paths << root.join("app/assets/javascripts")
19
+ end
20
+
16
21
  rake_tasks do
17
22
  load File.expand_path("../../tasks/rails/contact_tasks.rake", __dir__)
18
23
  end
@@ -6,6 +6,9 @@ module Rails
6
6
  class Client
7
7
  PEOPLE_API_BASE = "https://people.googleapis.com/v1".freeze
8
8
 
9
+ # Fields allowed in updatePersonFields (People API field mask); excludes Person metadata keys.
10
+ UPDATE_MASK_FIELDS = %w[names emailAddresses phoneNumbers addresses biographies].freeze
11
+
9
12
  def initialize(access_token:)
10
13
  @connection = Faraday.new(url: PEOPLE_API_BASE) do |faraday|
11
14
  faraday.request :retry, max: 3, interval: 0.5, backoff_factor: 2
@@ -19,7 +22,10 @@ module Rails
19
22
  end
20
23
 
21
24
  def update_contact(resource_name, payload)
22
- patch("/#{resource_name}:updateContact", payload)
25
+ mask = update_person_fields_mask(payload)
26
+ raise ArgumentError, "updatePersonFields must not be empty" if mask.blank?
27
+
28
+ patch("/#{resource_name}:updateContact", payload, { "updatePersonFields" => mask })
23
29
  end
24
30
 
25
31
  def delete_contact(resource_name)
@@ -35,10 +41,11 @@ module Rails
35
41
  end)
36
42
  end
37
43
 
38
- def patch(path, payload)
44
+ def patch(path, payload, params = nil)
39
45
  parse(@connection.patch(path) do |request|
40
46
  request.headers = headers
41
47
  request.body = payload.to_json
48
+ request.params.update(params) if params.present?
42
49
  end)
43
50
  end
44
51
 
@@ -49,6 +56,11 @@ module Rails
49
56
  def headers
50
57
  { "Authorization" => "Bearer #{@access_token}", "Content-Type" => "application/json" }
51
58
  end
59
+
60
+ def update_person_fields_mask(payload)
61
+ h = payload.stringify_keys
62
+ UPDATE_MASK_FIELDS.select { |field| h[field].present? }.join(",")
63
+ end
52
64
  end
53
65
  end
54
66
  end
@@ -7,18 +7,44 @@ module Rails
7
7
  end
8
8
 
9
9
  def to_people_payload
10
+ bio_text = [ @contact.biography, @contact.region_name ].compact.join("\n")
10
11
  {
11
- names: [ { givenName: @contact.given_name, familyName: @contact.family_name } ],
12
- emailAddresses: @contact.emails.map { |email| { value: email.value, type: (email.label || "other") } },
13
- phoneNumbers: @contact.phones.map { |phone| { value: phone.e164.presence || phone.value, type: (phone.label || "mobile") } },
12
+ names: [ { givenName: @contact.given_name, familyName: family_name_for_google } ],
13
+ emailAddresses: @contact.emails.filter_map do |email|
14
+ next if email.value.blank?
15
+
16
+ { value: email.value, type: (email.label || "other") }
17
+ end,
18
+ phoneNumbers: @contact.phones.filter_map do |phone|
19
+ value = phone.e164.presence || phone.value
20
+ next if value.blank?
21
+
22
+ { value: value, type: (phone.label || "mobile") }
23
+ end,
14
24
  addresses: @contact.addresses.map do |address|
15
25
  {
16
26
  city: address.city,
17
27
  formattedValue: address.formatted_value.presence || "Current city: #{@contact.current_city}\nDeparture city: #{@contact.departure_city}"
18
28
  }
19
29
  end,
20
- biographies: [ { value: [ @contact.biography, @contact.region_name ].compact.join("\n") } ]
21
- }.compact
30
+ biographies: ([ { value: bio_text, contentType: "TEXT_PLAIN" } ] if bio_text.present?)
31
+ }.compact.tap do |h|
32
+ h.delete(:emailAddresses) if h[:emailAddresses].blank?
33
+ h.delete(:phoneNumbers) if h[:phoneNumbers].blank?
34
+ h.delete(:addresses) if h[:addresses].blank?
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def family_name_for_google
41
+ suffix = Rails::Contact.configuration.google_contact_family_name_suffix
42
+ return @contact.family_name if suffix.blank?
43
+
44
+ base = @contact.family_name.to_s
45
+ return suffix if base.blank?
46
+
47
+ base.end_with?(suffix) ? base : "#{base}#{suffix}"
22
48
  end
23
49
  end
24
50
  end
@@ -8,7 +8,7 @@ module Rails
8
8
  end
9
9
 
10
10
  def sync!
11
- Rails::Contact::Contact.sync_window.each do |contact|
11
+ Rails::Contact::Contact.sync_window.includes(:emails, :phones, :addresses).each do |contact|
12
12
  sync_contact(contact)
13
13
  end
14
14
  end
@@ -16,7 +16,11 @@ module Rails
16
16
  def sync_contact(contact)
17
17
  payload = PayloadMapper.new(contact).to_people_payload
18
18
  response = if contact.google_resource_name.present?
19
- @client.update_contact(contact.google_resource_name, payload)
19
+ body = payload.merge(
20
+ resourceName: contact.google_resource_name,
21
+ etag: contact.google_etag
22
+ ).compact
23
+ @client.update_contact(contact.google_resource_name, body)
20
24
  else
21
25
  @client.create_contact(payload)
22
26
  end
@@ -5,8 +5,22 @@ module Rails
5
5
  class Elasticsearch
6
6
  INDEX = "rails_contact_contacts".freeze
7
7
 
8
+ class << self
9
+ def default_client
10
+ @default_client ||= ::Elasticsearch::Client.new(url: Rails::Contact.configuration.elasticsearch_url)
11
+ end
12
+
13
+ def default_client=(client)
14
+ @default_client = client
15
+ end
16
+
17
+ def clear_default_client!
18
+ @default_client = nil
19
+ end
20
+ end
21
+
8
22
  def initialize(client: nil)
9
- @client = client || ::Elasticsearch::Client.new(url: Rails::Contact.configuration.elasticsearch_url)
23
+ @client = client || self.class.default_client
10
24
  end
11
25
 
12
26
  def search(query, filters)
@@ -1,5 +1,5 @@
1
1
  module Rails
2
2
  module Contact
3
- VERSION = "0.1.5"
3
+ VERSION = "0.1.7"
4
4
  end
5
5
  end
data/lib/rails/contact.rb CHANGED
@@ -7,7 +7,6 @@ require "rails/contact/routing"
7
7
  require "rails/contact/search/query"
8
8
  require "rails/contact/search/backends/database"
9
9
  require "rails/contact/search/backends/elasticsearch"
10
- require "rails/contact/csv/import_service"
11
10
  require "rails/contact/google/client"
12
11
  require "rails/contact/google/token_store"
13
12
  require "rails/contact/google/payload_mapper"
@@ -20,12 +20,4 @@ namespace :rails_contact do
20
20
  puts "Google sync completed"
21
21
  end
22
22
 
23
- desc "Import contacts from CSV path (CSV_PATH=/path/file.csv)"
24
- task import_csv: :environment do
25
- path = ENV["CSV_PATH"]
26
- raise "Set CSV_PATH env var" if path.blank?
27
-
28
- count = Rails::Contact::Csv::ImportService.new(path: path).import!
29
- puts "Imported #{count} rows"
30
- end
31
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-contact
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kshitiz Sinha
@@ -93,8 +93,8 @@ dependencies:
93
93
  - - ">="
94
94
  - !ruby/object:Gem::Version
95
95
  version: '8.13'
96
- description: Mountable Rails engine offering contact CRUD, CSV import, Elasticsearch-backed
97
- search, and optional capped Google Contacts two-way sync.
96
+ description: Mountable Rails engine offering contact CRUD, Elasticsearch-backed search,
97
+ and optional capped Google Contacts two-way sync.
98
98
  email:
99
99
  - kshtzkr@gmail.com
100
100
  executables: []
@@ -169,7 +169,6 @@ files:
169
169
  - lib/generators/rails_contact/rails_contact_generator.rb
170
170
  - lib/rails/contact.rb
171
171
  - lib/rails/contact/configuration.rb
172
- - lib/rails/contact/csv/import_service.rb
173
172
  - lib/rails/contact/engine.rb
174
173
  - lib/rails/contact/google/client.rb
175
174
  - lib/rails/contact/google/conflict_resolver.rb
@@ -1,87 +0,0 @@
1
- require "csv"
2
-
3
- module Rails
4
- module Contact
5
- module Csv
6
- class ImportService
7
- HEADER_MAP = {
8
- "Enquirer First Name" => :given_name,
9
- "Enquirer Last Name" => :family_name,
10
- "Current City" => :current_city,
11
- "Departure City" => :departure_city,
12
- "Region Name" => :region_name
13
- }.freeze
14
-
15
- def initialize(path:, dedupe_key: :email)
16
- @path = path
17
- @dedupe_key = dedupe_key
18
- end
19
-
20
- def import!
21
- imported = 0
22
- CSV.foreach(@path, headers: true) do |row|
23
- import_row(row.to_h)
24
- imported += 1
25
- end
26
- imported
27
- end
28
-
29
- private
30
-
31
- def import_row(row)
32
- attrs = build_attributes(row)
33
- contact = find_or_initialize(row)
34
- contact.assign_attributes(attrs)
35
- contact.save!
36
- upsert_associations(contact, row)
37
- Rails::Contact::IndexContactJob.perform_later(contact.id)
38
- end
39
-
40
- def build_attributes(row)
41
- HEADER_MAP.each_with_object({}) do |(csv_key, attr), memo|
42
- value = row[csv_key].to_s.strip
43
- memo[attr] = (value == "blank" ? nil : value.presence)
44
- end
45
- end
46
-
47
- def find_or_initialize(row)
48
- email = normalize_email(row["Enquirer Email"])
49
- return Rails::Contact::Contact.new if email.blank?
50
- return Rails::Contact::Contact.new if @dedupe_key != :email
51
-
52
- Rails::Contact::Contact.joins(:emails).where(rails_contact_contact_emails: { value: email }).first || Rails::Contact::Contact.new
53
- end
54
-
55
- def upsert_associations(contact, row)
56
- email = normalize_email(row["Enquirer Email"])
57
- primary_phone = normalize_phone(row["Enquirer Phone Country Code"], row["Enquirer Phone"])
58
- alt_phone = normalize_phone(row["Alternate Wa Phone Country Code"], row["Alternate Wa Phone"])
59
-
60
- contact.emails.find_or_create_by!(value: email, primary: true, label: "work") if email.present?
61
- if primary_phone.present?
62
- contact.phones.find_or_create_by!(value: primary_phone, e164: primary_phone, primary: true, label: "mobile")
63
- end
64
- if alt_phone.present?
65
- contact.phones.find_or_create_by!(value: alt_phone, e164: alt_phone, primary: false, label: "whatsapp")
66
- end
67
-
68
- formatted_value = "Current city: #{contact.current_city}\nDeparture city: #{contact.departure_city}"
69
- contact.addresses.find_or_create_by!(city: contact.current_city, departure_city: contact.departure_city, formatted_value: formatted_value)
70
- end
71
-
72
- def normalize_email(value)
73
- clean = value.to_s.strip.downcase
74
- clean.presence
75
- end
76
-
77
- def normalize_phone(country_code, number)
78
- digits = number.to_s.gsub(/\D+/, "")
79
- return nil if digits.blank?
80
- prefix = country_code.to_s.strip
81
- prefix = "+#{prefix}" unless prefix.start_with?("+")
82
- "#{prefix}#{digits}"
83
- end
84
- end
85
- end
86
- end
87
- end