Skip to content

Commit

Permalink
Merge branch 'main' into TW-2386-gh-405-ruby-nylas-api-returns-invali…
Browse files Browse the repository at this point in the history
…d-exception-message-message-in-case-of-errors
  • Loading branch information
mrashed-dev authored Dec 21, 2023
2 parents c3bb7bf + ab663a1 commit 9eb4bf4
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 15 deletions.
26 changes: 13 additions & 13 deletions .github/workflows/rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ name: Rubocop

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

jobs:
test:
Expand All @@ -23,14 +23,14 @@ jobs:
BUNDLE_GEMFILE: gemfiles/Gemfile.${{ matrix.gemfile }}

steps:
- uses: actions/checkout@v2
- name: Set up Ruby
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
# change this to (see https://github.com/ruby/setup-ruby#versioning):
# uses: ruby/setup-ruby@v1
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
with:
ruby-version: 2.6
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- name: Run rubocop
run: bundle exec rubocop --config .rubocop.yml
- uses: actions/checkout@v2
- name: Set up Ruby
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
# change this to (see https://github.com/ruby/setup-ruby#versioning):
# uses: ruby/setup-ruby@v1
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
with:
ruby-version: 3.0
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- name: Run rubocop
run: bundle exec rubocop --config .rubocop.yml
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

### Unreleased
* Add support for count on message/thread search
* Add support for detect provider endpoint

### 5.17.0 / 2022-04-04
* Add support for verifying webhook signatures
* Add `event.updated_at`
Expand Down
21 changes: 20 additions & 1 deletion lib/nylas/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,31 @@ def ip_addresses
client.as(client.app_secret).get(path: path, auth_method: HttpClient::AuthMethod::BASIC)
end

# Returns list of IP addresses
# @param email_address [String] The email address to detect the provider for
# @return [Hash] The provider information
# hash has keys of :updated_at (unix timestamp) and :ip_addresses (array of strings)
def detect_provider(email_address)
payload = {
"client_id" => app_id,
"client_secret" => client.app_secret,
"email_address" => email_address
}

client.as(client.app_secret).execute(
method: :post,
path: "/connect/detect-provider",
payload: JSON.dump(payload)
)
end

# @param message [Hash, String, #send!]
# @return [Message] The resulting message
def send!(message)
return message.send! if message.respond_to?(:send!)
return NewMessage.new(**message.merge(api: self)).send! if message.respond_to?(:key?)
return RawMessage.new(message, api: self).send! if message.is_a? String

RawMessage.new(message, api: self).send! if message.is_a? String
end

# Allows you to get an API that acts as a different user but otherwise has the same settings
Expand Down
4 changes: 4 additions & 0 deletions lib/nylas/search_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ class SearchCollection < Collection
def resources_path
"#{model.resources_path(api: api)}/search"
end

def count
self.class.new(model: model, api: api, constraints: constraints).find_each.map.count
end
end
end
3 changes: 2 additions & 1 deletion lib/nylas/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def serialize(object)

def cast(value)
return JSON.parse(value, symbolize_names: true) if value.is_a?(String)
return value if value.respond_to?(:key)

value if value.respond_to?(:key)
end
end
Types.registry[:hash] = HashType.new
Expand Down
33 changes: 33 additions & 0 deletions spec/nylas/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,39 @@
# This spec is the only one that should have any webmock stuff going on, everything else should use the
# FakeAPI to see what requests were made and what they included.
describe Nylas::API do
describe "#detect_provider" do
# tests the detect_providr method
it "returns the provider" do
url = "https://api.nylas.com/connect/detect-provider"
client = Nylas::HttpClient.new(app_id: "not-real", app_secret: "also-not-real")
data = {
"client_id" => "not-real",
"client_secret" => "also-not-real",
"email_address" => "[email protected]"
}
response = {
auth_name: "gmail",
detected: true,
email_address: "[email protected]",
is_imap: false,
provider_name: "gmail"
}

stub_request(:post, url)
.to_return(
status: 200,
body: response.to_json,
headers: { "content-type" => "application/json" }
)
api = described_class.new(client: client)
res = api.detect_provider("[email protected]")

expect(res).to eq(response)
expect(WebMock).to have_requested(:post, url)
.with(body: data)
end
end

describe "#exchange_code_for_token" do
it "retrieves oauth token with code" do
client = Nylas::HttpClient.new(app_id: "fake-app", app_secret: "fake-secret")
Expand Down
24 changes: 24 additions & 0 deletions spec/nylas/search_collection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require "spec_helper"

describe Nylas::SearchCollection do
let(:api) { FakeAPI.new }

describe "#count" do
it "Returns an enumerable for a single page of results, filtered by `offset` and `limit` and `where`" do
allow(api).to receive(:execute)
.with(
auth_method: Nylas::HttpClient::AuthMethod::BEARER,
method: :get,
path: "/collection/search",
query: { limit: 100, offset: 0 },
headers: {}
).and_return([{ id: "1234" }])

collection = described_class.new(model: FullModel, api: api)

expect(collection.count).to be 1
end
end
end

0 comments on commit 9eb4bf4

Please sign in to comment.