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 b08df54d..812d5c24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/lib/nylas/api.rb b/lib/nylas/api.rb index f8c2b09d..2c260933 100644 --- a/lib/nylas/api.rb +++ b/lib/nylas/api.rb @@ -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 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/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") 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