From d0a3ba31deb9dd08fa889c26d1383bc97b9542e1 Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Wed, 29 Nov 2023 15:38:43 -0500 Subject: [PATCH 1/5] Ensure file is using correct encoding --- lib/nylas/contact.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nylas/contact.rb b/lib/nylas/contact.rb index f827f397..ce74d237 100644 --- a/lib/nylas/contact.rb +++ b/lib/nylas/contact.rb @@ -44,7 +44,7 @@ class Contact def picture return @picture_tempfile if @picture_tempfile - @picture_tempfile = Tempfile.new + @picture_tempfile = Tempfile.new(encoding: "ascii-8bit") @picture_tempfile.write(api.get(path: "#{resource_path}/picture")) @picture_tempfile.close @picture_tempfile From 4926f0bbf3864f4fea00f333891b1a29a0ac3dc6 Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Wed, 29 Nov 2023 15:39:44 -0500 Subject: [PATCH 2/5] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b08df54d..55826e8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +### Unreleased +* Fix contact picture encoding error + ### 5.17.0 / 2022-04-04 * Add support for verifying webhook signatures * Add `event.updated_at` From fc153cbbcb94bb144b10b2b2038e52f51df6b521 Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Wed, 29 Nov 2023 16:01:22 -0500 Subject: [PATCH 3/5] add tests for contact pictures --- spec/nylas/contact_spec.rb | 19 +++++++++++++++++++ spec/spec_helper.rb | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/spec/nylas/contact_spec.rb b/spec/nylas/contact_spec.rb index d9a42278..06af51ec 100644 --- a/spec/nylas/contact_spec.rb +++ b/spec/nylas/contact_spec.rb @@ -132,4 +132,23 @@ expect(JSON.parse(contact.to_json)).to eql(JSON.parse(full_json)) end end + + describe "#picture" do + it "returns a Tempfile" do + contact = described_class.from_json(full_json, api: api) + expect(contact.picture).to be_a(Tempfile) + end + + it "caches the picture in @picture_tempfile" do + contact = described_class.from_json(full_json, api: api) + expect(contact.picture).to be_a(Tempfile) + expect(contact.picture).to be_a(Tempfile) + expect(api.requests.length).to be(1) + end + + it "creates the Tempfile with the correct encoding" do + contact = described_class.from_json(full_json, api: api) + expect(contact.picture.external_encoding.name).to eql("ascii-8bit") + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0f6bd4a5..35540265 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,6 +18,10 @@ def execute(method:, path:, payload: nil, query: {}, auth_method: Nylas::HttpCli requests.push(method: method, path: path, payload: payload, query: query, auth_method: auth_method) end + def get(path: nil, headers: {}, query: {}, auth_method: nil) + requests.push(method: :get, path: path, query: query, headers: headers, auth_method: auth_method) + end + def requests @requests ||= [] end From 9ca10356244903e03ee35548ebadcf7519f2d118 Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Thu, 30 Nov 2023 08:40:56 -0500 Subject: [PATCH 4/5] fix ascii encoding --- spec/nylas/contact_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/nylas/contact_spec.rb b/spec/nylas/contact_spec.rb index 06af51ec..c6652aa1 100644 --- a/spec/nylas/contact_spec.rb +++ b/spec/nylas/contact_spec.rb @@ -148,7 +148,7 @@ it "creates the Tempfile with the correct encoding" do contact = described_class.from_json(full_json, api: api) - expect(contact.picture.external_encoding.name).to eql("ascii-8bit") + expect(contact.picture.external_encoding.name).to eql("ASCII-8BIT") end end end From bea086ee0f45c351ed2dc4fcc471ac6aadb2b953 Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Thu, 30 Nov 2023 08:49:38 -0500 Subject: [PATCH 5/5] Bypass testing for 2.x ruby --- spec/nylas/contact_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/nylas/contact_spec.rb b/spec/nylas/contact_spec.rb index c6652aa1..5846db81 100644 --- a/spec/nylas/contact_spec.rb +++ b/spec/nylas/contact_spec.rb @@ -147,6 +147,11 @@ end it "creates the Tempfile with the correct encoding" do + # Skip test if Ruby version is less than 3.0 due to how it handles IO streams + if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.0") + skip "Test requires Ruby version 3.x or higher" + end + contact = described_class.from_json(full_json, api: api) expect(contact.picture.external_encoding.name).to eql("ASCII-8BIT") end