diff --git a/gem_config.rb b/gem_config.rb index 7a6bb423..f5003e60 100644 --- a/gem_config.rb +++ b/gem_config.rb @@ -12,13 +12,17 @@ def self.apply(gem) gem.platform = "ruby" gem.required_ruby_version = ">= 3.0" gem.metadata = metadata - gem.email = "support@nylas.com" - gem.authors = ["Nylas, Inc."] + add_author_info(gem) dev_dependencies.each do |dependency| gem.add_development_dependency(*dependency) end end + def self.add_author_info(gem) + gem.email = "support@nylas.com" + gem.authors = ["Nylas, Inc."] + end + def self.metadata { "bug_tracker_uri" => "https://github.com/nylas/nylas-ruby/issues", diff --git a/lib/nylas.rb b/lib/nylas.rb index cf606bd8..1894c4ff 100644 --- a/lib/nylas.rb +++ b/lib/nylas.rb @@ -7,7 +7,7 @@ # See https://github.com/sparklemotion/http-cookie/issues/27 # and https://github.com/sparklemotion/http-cookie/issues/6 # -# CookieJar uses unsafe class caching for dynamically loading cookie jars. +# CookieJar uses unsafe class caching for dynamically loading cookie jars. # If two rest-client instances are instantiated at the same time (in threads), non-deterministic # behaviour can occur whereby the Hash cookie jar isn't properly loaded and cached. # Forcing an instantiation of the jar onload will force the CookieJar to load before the system has diff --git a/lib/nylas/client.rb b/lib/nylas/client.rb index 578ed6a9..b7f098f8 100644 --- a/lib/nylas/client.rb +++ b/lib/nylas/client.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative "resources/calendars" +require_relative "resources/messages" require_relative "resources/events" require_relative "resources/auth" require_relative "resources/webhooks" @@ -45,6 +46,13 @@ def events Events.new(self) end + # The event resources for your Nylas application. + # + # @return [Nylas::Messages] Message resources for your Nylas application + def messages + Messages.new(self) + end + # The auth resources for your Nylas application. # # @return [Nylas::Auth] Auth resources for your Nylas application. diff --git a/lib/nylas/handler/http_client.rb b/lib/nylas/handler/http_client.rb index e4c22d9f..d2f9eea4 100644 --- a/lib/nylas/handler/http_client.rb +++ b/lib/nylas/handler/http_client.rb @@ -5,6 +5,7 @@ require_relative "../errors" require_relative "../version" +# Module for working with HTTP Client module Nylas require "yajl" require "base64" @@ -34,11 +35,7 @@ def execute(method:, path:, timeout:, headers: {}, query: {}, payload: nil, api_ query: query, payload: payload, api_key: api_key, timeout: timeout) begin rest_client_execute(**request) do |response, _request, result| - content_type = nil - - if response.headers && response.headers[:content_type] - content_type = response.headers[:content_type].downcase - end + content_type = get_content_type(response) begin response = parse_response(response) if content_type == "application/json" @@ -55,6 +52,13 @@ def execute(method:, path:, timeout:, headers: {}, query: {}, payload: nil, api_ end end + def get_content_type(response) + if response.headers && response.headers[:content_type] + content_type = response.headers[:content_type].downcase + end + content_type + end + # Builds a request sent to the Nylas API. # # @param method [Symbol] HTTP method for the API call. Either :get, :post, :delete, or :patch. @@ -137,14 +141,18 @@ def error_hash_to_exception(response, status_code, path) NylasOAuthError.new(response[:error], response[:error_description], response[:error_uri], response[:error_code], status_code) else - error_obj = response[:error] - provider_error = error_obj.fetch(:provider_error, nil) - - NylasApiError.new(error_obj[:type], error_obj[:message], status_code, provider_error, - response[:request_id]) + throw_error(response) end end + def throw_error(response) + error_obj = response[:error] + provider_error = error_obj.fetch(:provider_error, nil) + + NylasApiError.new(error_obj[:type], error_obj[:message], status_code, provider_error, + response[:request_id]) + end + # Adds query parameters to a URL. # # @return [String] Processed URL, including query params. diff --git a/lib/nylas/resources/auth.rb b/lib/nylas/resources/auth.rb index 5a38247a..1a305fd1 100644 --- a/lib/nylas/resources/auth.rb +++ b/lib/nylas/resources/auth.rb @@ -156,18 +156,32 @@ def build_query(config) "access_type" => config[:access_type] || "online", "response_type" => "code" } + set_params(config) + URI.encode_www_form(params) + end + + # Set the parameters for the query + def set_params(config) params["provider"] = config[:provider] if config[:provider] + set_config_params(config) + set_more_config(config) + end + + # Set login related configurations + def set_config_params(config) if config[:login_hint] params["login_hint"] = config[:login_hint] params["include_grant_scopes"] = config[:include_grant_scopes].to_s if config[:include_grant_scopes] end params["scope"] = config[:scope].join(" ") if config[:scope] + end + + # More config + def set_more_config(config) params["prompt"] = config[:prompt] if config[:prompt] params["metadata"] = config[:metadata] if config[:metadata] params["state"] = config[:state] if config[:state] - - URI.encode_www_form(params) end # Hashes the secret for PKCE authentication. diff --git a/lib/nylas/resources/messages.rb b/lib/nylas/resources/messages.rb new file mode 100644 index 00000000..f7d8f207 --- /dev/null +++ b/lib/nylas/resources/messages.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require_relative "resource" +require_relative "../handler/grants_api_operations" + +module Nylas + # Calendars + class Messages < Resource + include GrantsApiOperations::Update + include GrantsApiOperations::List + include GrantsApiOperations::Destroy + include GrantsApiOperations::Find + + # Initializes Calendars. + def initialize(sdk_instance) + super("messages", sdk_instance) + end + end +end diff --git a/lib/nylas/resources/resource.rb b/lib/nylas/resources/resource.rb index db4f72f9..b12d6863 100644 --- a/lib/nylas/resources/resource.rb +++ b/lib/nylas/resources/resource.rb @@ -2,6 +2,7 @@ module Nylas # NOTE: BaseResource is the base class for all Nylas API resources. + # Used by all Nylas API resources class Resource # Initializes a resource. def initialize(resource_name, sdk_instance)