<commit_before>module EWS
  
  class Parser
    def parse_get_folder(doc)
      folder = doc.xpath('//t:Folder')
      attrs = {
        :folder_id  => folder.xpath('t:FolderId/@Id').to_s,
        :change_key => folder.xpath('t:FolderId/@ChangeKey').to_s,
        :name       => folder.xpath('t:DisplayName/text()').to_s
      }      
      Folder.new attrs
    end
    
    def parse_find_item(doc)
      doc.xpath('//t:Items/child::*').map do |node|
        parse_exchange_item node.xpath('.') # force NodeSelection
      end.compact    
    end
    
    def parse_get_item(doc)      
      parse_exchange_item doc.xpath('//m:Items/child::*[1]')
    end

    def parse_get_attachment(doc)
      parse_attachment doc.xpath('//m:Attachments/child::*[1]')
    end
    
    RESPONSE_MSG_XPATH = ['//m:FindFolderResponseMessage',
                          '//m:GetFolderResponseMessage',
                          '//m:FindItemResponseMessage',
                          '//m:GetItemResponseMessage',
                          '//m:GetAttachmentResponseMessage'].join('|')
    
    # Parses the ResponseMessage looking for errors.
    #
    # @see http://msdn.microsoft.com/en-us/library/aa494164%28EXCHG.80%29.aspx
    # Exhange 2007 Valid Response Messages
    #    
    # CopyFolderResponseMessage
    # CopyItemResponseMessage
    # CreateAttachmentResponseMessage
    # CreateFolderResponseMessage
    # CreateItemResponseMessage
    # CreateManagedFolderResponseMessage
    # DeleteAttachmentResponseMessage
    # DeleteFolderResponseMessage
    # DeleteItemResponseMessage
    # ExpandDLResponseMessage
    # FindFolderResponseMessage
    # FindItemResponseMessage
    # GetAttachmentResponseMessage
    # GetEventsResponseMessage
    # GetFolderResponseMessage
    # GetItemResponseMessage
    # MoveFolderResponseMessage
    # MoveItemResponseMessage
    # ResolveNamesResponseMessage
    # SendItemResponseMessage
    # SendNotificationResponseMessage
    # SubscribeResponseMessage
    # SyncFolderHierarchyResponseMessage
    # SyncFolderItemsResponseMessage
    # UnsubscribeResponseMessage
    # UpdateFolderResponseMessage
    # UpdateItemResponseMessage
    # ConvertIdResponseMessage
    def parse_response_message(doc)      
      response_msg = doc.xpath(RESPONSE_MSG_XPATH)
      if response_msg.xpath('@ResponseClass').to_s == 'Error'
        error_msg = (response_msg / 'm:MessageText/text()').to_s
        response_code = (response_msg / 'm:ResponseCode/text()').to_s
        raise EWS::ResponseError.new(error_msg, response_code)
      end
    end
<commit_msg>simplify the parse_response_message xpath<commit_after>module EWS
  
  class Parser
    def parse_get_folder(doc)
      folder = doc.xpath('//t:Folder')
      attrs = {
        :folder_id  => folder.xpath('t:FolderId/@Id').to_s,
        :change_key => folder.xpath('t:FolderId/@ChangeKey').to_s,
        :name       => folder.xpath('t:DisplayName/text()').to_s
      }      
      Folder.new attrs
    end
    
    def parse_find_item(doc)
      doc.xpath('//t:Items/child::*').map do |node|
        parse_exchange_item node.xpath('.') # force NodeSelection
      end.compact    
    end
    
    def parse_get_item(doc)      
      parse_exchange_item doc.xpath('//m:Items/child::*[1]')
    end

    def parse_get_attachment(doc)
      parse_attachment doc.xpath('//m:Attachments/child::*[1]')
    end
    
    # Checks the ResponseMessage for errors.
    #
    # @see http://msdn.microsoft.com/en-us/library/aa494164%28EXCHG.80%29.aspx
    # Exhange 2007 Valid Response Messages
    def parse_response_message(doc)      
      error_node = doc.xpath('//m:ResponseMessages/child::*[@ResponseClass="Error"]')
      unless error_node.empty?
        error_msg = error_node.xpath('m:MessageText/text()').to_s
        response_code = error_node.xpath('m:ResponseCode/text()').to_s
        raise EWS::ResponseError.new(error_msg, response_code)
      end
    end

{'fraction_non_alphanumeric': 0.10891089108910891, 'fraction_numerical': 0.00913937547600914, 'mean_word_length': 2.7921077959576515, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7511727', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1212, 'n_words': 173}