Skip to content

Commit

Permalink
Fix contact picture encoding error (#441)
Browse files Browse the repository at this point in the history
This PR ensured that when we are dealing with contact picture, that we are using the correct encoding. Closes #422.
  • Loading branch information
mrashed-dev committed Dec 21, 2023
1 parent 902ad8c commit 125adcc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/nylas/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions spec/nylas/contact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 125adcc

Please sign in to comment.