Skip to content

Commit

Permalink
Add support for read, update, and delete for messages (#429)
Browse files Browse the repository at this point in the history
* Update README for Hacktoberfest (#427)

add blurb on hacktoberfest guidelines

* Fix Rubocop complains

None of them related to my code, but everything is fixed and tested

* Remove hackathon blurb

---------

Co-authored-by: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com>
  • Loading branch information
atejada and mrashed-dev authored Nov 1, 2023
1 parent caec0fc commit 3512749
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 15 deletions.
8 changes: 6 additions & 2 deletions gem_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion lib/nylas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/nylas/client.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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.
Expand Down
28 changes: 18 additions & 10 deletions lib/nylas/handler/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_relative "../errors"
require_relative "../version"

# Module for working with HTTP Client
module Nylas
require "yajl"
require "base64"
Expand Down Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 16 additions & 2 deletions lib/nylas/resources/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions lib/nylas/resources/messages.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/nylas/resources/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3512749

Please sign in to comment.