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

- old
+ new

@@ -29,13 +29,16 @@ class NoAuthToken < StandardError; end class UnexpectedAccountAction < StandardError; end class UnexpectedResponse < StandardError; end class APIError < StandardError attr_accessor :error_type - def initialize(type, error) + 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 @@ -45,29 +48,39 @@ # 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 + + exc + end + def self.interpret_response(result, result_content, options = {}) # Handle HTTP errors Inbox.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') - if result.code.to_i == 400 - exc = InvalidRequest - elsif result.code.to_i == 402 - exc = MessageRejected - elsif result.code.to_i == 429 - exc = SendingQuotaExceeded - elsif result.code.to_i == 503 - exc = ServiceUnavailable - else - exc = APIError - end + exc = Inbox.http_code_to_exception(result.code.to_i) raise exc.new(json['type'], json['message']) end raise UnexpectedResponse.new(result.msg) if result.is_a?(Net::HTTPClientError) raise UnexpectedResponse.new if options[:expected_class] && !json.is_a?(options[:expected_class]) json @@ -246,10 +259,17 @@ "tag" => Inbox::Tag, "folder" => Inbox::Folder, "label" => Inbox::Label, } + # It's possible to ask the API to expand objects. + # In this case, we do the right thing and return + # an expanded object. + EXPANDED_OBJECTS_TABLE = { + "message" => Inbox::ExpandedMessage, + } + def _build_exclude_types(exclude_types) exclude_string = "&exclude_types=" exclude_types.each do |value| count = 0 @@ -260,21 +280,25 @@ end exclude_string = exclude_string[0..-2] end - def deltas(cursor, exclude_types=[]) + def deltas(cursor, exclude_types=[], expanded_view=false) raise 'Please provide a block for receiving the delta objects' if !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}") + if expanded_view + path += '&view=expanded' + end + json = nil RestClient.get(path) do |response,request,result| json = Inbox.interpret_response(result, response, {:expected_class => Object}) end @@ -286,10 +310,14 @@ if not OBJECTS_TABLE.has_key?(delta['object']) next end cls = OBJECTS_TABLE[delta['object']] + if EXPANDED_OBJECTS_TABLE.has_key?(delta['object']) and expanded_view + cls = EXPANDED_OBJECTS_TABLE[delta['object']] + end + obj = cls.new(self) case delta["event"] when 'create', 'modify' obj.inflate(delta['attributes']) @@ -305,30 +333,37 @@ break if start_cursor == end_cursor cursor = end_cursor end end - def delta_stream(cursor, exclude_types=[], timeout=0) + def delta_stream(cursor, exclude_types=[], timeout=0, expanded_view=false) raise 'Please provide a block for receiving the delta objects' if !block_given? exclude_string = "" 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}") + if expanded_view + path += '&view=expanded' + end parser = Yajl::Parser.new(:symbolize_keys => false) parser.on_parse_complete = proc do |data| delta = Inbox.interpret_response(OpenStruct.new(:code => '200'), data, {:expected_class => Object, :result_parsed => true}) if not OBJECTS_TABLE.has_key?(delta['object']) next end cls = OBJECTS_TABLE[delta['object']] + if EXPANDED_OBJECTS_TABLE.has_key?(delta['object']) and expanded_view + cls = EXPANDED_OBJECTS_TABLE[delta['object']] + end + obj = cls.new(self) case delta["event"] when 'create', 'modify' obj.inflate(delta['attributes'])