lib/inbox.rb in nylas-1.1.0 vs lib/inbox.rb in nylas-1.2.1

- old
+ new

@@ -19,62 +19,54 @@ require 'folder' require 'restful_model' require 'restful_model_collection' require 'version' - module Inbox - - class AccessDenied < StandardError; end - class ResourceNotFound < StandardError; end - class NoAuthToken < StandardError; end - class UnexpectedAccountAction < StandardError; end - class UnexpectedResponse < StandardError; end - class APIError < StandardError + Error = Class.new(::StandardError) + AccessDenied = Class.new(Error) + ResourceNotFound = Class.new(Error) + NoAuthToken = Class.new(Error) + UnexpectedAccountAction = Class.new(Error) + UnexpectedResponse = Class.new(Error) + class APIError < Error attr_accessor :error_type attr_accessor :server_error def initialize(type, error, server_error = nil) super(error) self.error_type = type self.server_error = server_error end end - class InvalidRequest < APIError; end - class MessageRejected < APIError; end - class SendingQuotaExceeded < APIError; end - class ServiceUnavailable < APIError; end + InvalidRequest = Class.new(APIError) + MessageRejected = Class.new(APIError) + SendingQuotaExceeded = Class.new(APIError) + ServiceUnavailable = Class.new(APIError) def self.interpret_http_status(result) # Handle HTTP errors and RestClient errors raise ResourceNotFound.new if result.code.to_i == 404 raise AccessDenied.new if result.code.to_i == 403 end - def self.http_code_to_exception(http_code) - if http_code == 400 - exc = InvalidRequest - elsif http_code == 402 - exc = MessageRejected - elsif http_code == 403 - exc = AccessDenied - elsif http_code == 404 - exc = ResourceNotFound - elsif http_code == 429 - exc = SendingQuotaExceeded - elsif http_code == 503 - exc = ServiceUnavailable - else - exc = APIError - end + HTTP_CODE_TO_EXCEPTIONS = { + 400 => InvalidRequest, + 402 => MessageRejected, + 403 => AccessDenied, + 404 => ResourceNotFound, + 429 => SendingQuotaExceeded, + 503 => ServiceUnavailable + }.freeze - exc + def self.http_code_to_exception(http_code) + HTTP_CODE_TO_EXCEPTIONS.fetch(http_code, APIError) end def self.interpret_response(result, result_content, options = {}) # Handle HTTP errors - Inbox.interpret_http_status(result) + self.interpret_http_status(result) # Handle content expectation errors raise UnexpectedResponse.new if options[:expected_class] && result_content.empty? json = options[:result_parsed]? result_content : JSON.parse(result_content) if json.is_a?(Hash) && (json['type'] == 'api_error' or json['type'] == 'invalid_request_error') @@ -281,20 +273,21 @@ exclude_string = exclude_string[0..-2] end def deltas(cursor, exclude_types=[], expanded_view=false) - raise 'Please provide a block for receiving the delta objects' if !block_given? + return enum_for(:deltas, cursor, exclude_types, expanded_view) unless block_given? + exclude_string = "" if exclude_types.any? exclude_string = _build_exclude_types(exclude_types) end # loop and yield deltas until we've come to the end. loop do - path = self.url_for_path("/delta?cursor=#{cursor}#{exclude_string}") + path = self.url_for_path("/delta?exclude_folders=false&cursor=#{cursor}#{exclude_string}") if expanded_view path += '&view=expanded' end json = nil @@ -343,10 +336,10 @@ if exclude_types.any? exclude_string = _build_exclude_types(exclude_types) end # loop and yield deltas indefinitely. - path = self.url_for_path("/delta/streaming?cursor=#{cursor}#{exclude_string}") + path = self.url_for_path("/delta/streaming?exclude_folders=false&cursor=#{cursor}#{exclude_string}") if expanded_view path += '&view=expanded' end parser = Yajl::Parser.new(:symbolize_keys => false)