From 125adcc94d28158bfa570efc3fa4f6ec6450d752 Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Thu, 21 Dec 2023 09:44:28 -0500 Subject: [PATCH] Fix contact picture encoding error (#441) This PR ensured that when we are dealing with contact picture, that we are using the correct encoding. Closes #422. --- CHANGELOG.md | 2 ++ lib/nylas/contact.rb | 2 +- spec/nylas/contact_spec.rb | 24 ++++++++++++++++++++++++ spec/spec_helper.rb | 4 ++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 812d5c24..1cd50076 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ### Unreleased * Add support for count on message/thread search * Add support for detect provider endpoint +* Add accept header by default +* Fix contact picture encoding error ### 5.17.0 / 2022-04-04 * Add support for verifying webhook signatures 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 diff --git a/spec/nylas/contact_spec.rb b/spec/nylas/contact_spec.rb index d9a42278..5846db81 100644 --- a/spec/nylas/contact_spec.rb +++ b/spec/nylas/contact_spec.rb @@ -132,4 +132,28 @@ 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 + # 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 + 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