Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix contact picture encoding error #441

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading