From d2ab005f0bb6bc9bd71a2519b80b2af1d3f54484 Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Mon, 4 Dec 2023 15:26:16 -0500 Subject: [PATCH 1/2] Add support for detect provider endpoint (#439) This PR adds support for the /connect/detect-provider endpoint. Closes #421. --- CHANGELOG.md | 3 +++ lib/nylas/api.rb | 18 ++++++++++++++++++ spec/nylas/api_spec.rb | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b08df54d..cadb196d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +### 5.17.0 / TBD +* Add support for detect provider endpoint + ### 5.17.0 / 2022-04-04 * Add support for verifying webhook signatures * Add `event.updated_at` diff --git a/lib/nylas/api.rb b/lib/nylas/api.rb index f8c2b09d..4d91f544 100644 --- a/lib/nylas/api.rb +++ b/lib/nylas/api.rb @@ -200,6 +200,24 @@ 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) diff --git a/spec/nylas/api_spec.rb b/spec/nylas/api_spec.rb index ee96885f..73b6b9c4 100644 --- a/spec/nylas/api_spec.rb +++ b/spec/nylas/api_spec.rb @@ -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" => "test@gmail.com" + } + response = { + auth_name: "gmail", + detected: true, + email_address: "test@gmail.com", + 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("test@gmail.com") + + 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") From ab663a1239303c3c94e6632e9eab2221ae17f25c Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Thu, 21 Dec 2023 09:36:21 -0500 Subject: [PATCH 2/2] Add support for count on message/thread search (#443) This PR adds count support for SearchCollection type. Closes #416. --- .github/workflows/rubocop.yml | 26 +++++++++++++------------- CHANGELOG.md | 3 ++- lib/nylas/api.rb | 3 ++- lib/nylas/search_collection.rb | 4 ++++ lib/nylas/types.rb | 3 ++- spec/nylas/search_collection_spec.rb | 24 ++++++++++++++++++++++++ 6 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 spec/nylas/search_collection_spec.rb diff --git a/.github/workflows/rubocop.yml b/.github/workflows/rubocop.yml index a9ec3c02..b3b94af0 100644 --- a/.github/workflows/rubocop.yml +++ b/.github/workflows/rubocop.yml @@ -9,9 +9,9 @@ name: Rubocop on: push: - branches: [ main ] + branches: [main] pull_request: - branches: [ main ] + branches: [main] jobs: test: @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index cadb196d..812d5c24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog -### 5.17.0 / TBD +### Unreleased +* Add support for count on message/thread search * Add support for detect provider endpoint ### 5.17.0 / 2022-04-04 diff --git a/lib/nylas/api.rb b/lib/nylas/api.rb index 4d91f544..2c260933 100644 --- a/lib/nylas/api.rb +++ b/lib/nylas/api.rb @@ -223,7 +223,8 @@ def detect_provider(email_address) 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 diff --git a/lib/nylas/search_collection.rb b/lib/nylas/search_collection.rb index fe651a22..1d131cc5 100644 --- a/lib/nylas/search_collection.rb +++ b/lib/nylas/search_collection.rb @@ -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 diff --git a/lib/nylas/types.rb b/lib/nylas/types.rb index df8b6c41..f4806f5e 100644 --- a/lib/nylas/types.rb +++ b/lib/nylas/types.rb @@ -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 diff --git a/spec/nylas/search_collection_spec.rb b/spec/nylas/search_collection_spec.rb new file mode 100644 index 00000000..d6b6e6a1 --- /dev/null +++ b/spec/nylas/search_collection_spec.rb @@ -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