<commit_before>require 'savon'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/hash'

module Ottick
  class Ticket
    attr_reader :options, :client, :errors, :response

    # Ticket.new(options)
    # for possible options see Savon.client()
    # http://savonrb.com/version2/client.html
    #
    def initialize(options = {})
      sanitize_options!(options)
      @otrs_credentials = otrs_credentials!(options)
      @options          = default_options.merge(options)
      @client           = Savon.client(@options)
      @errors           = []
      @response         = nil
    end

    def get(options = {})
      begin
        response = ticket_get(options)
        if response.success?
          @response = response.body[:ticket_get_response]
          if @response.include?(:ticket)
            @response
          else
            @errors << "#{@response[:error][:error_code]} => #{@response[:error][:error_message]}"
            nil
          end
        elsif response.soap_fault?
          @errors << response.body.to_s
          nil
        else
          @errors << response.http.to_s
          nil
        end
      rescue Exception => e
        @errors << "Exception occurred: " + e.to_s
        nil
      end
    end

    def create(subject, text, options = {})
      begin
        response = ticket_create(subject, text, options)
        if response.success?
          @response = response.body[:ticket_create_response]
          if @response.include?(:ticket_id)
            @response
          else
            @errors << "#{@response[:error][:error_code]} => #{@response[:error][:error_message]}"
            nil
          end
        elsif response.soap_fault?
          @errors << response.body.to_s
          nil
        else
          @errors << response.http.to_s
          nil
        end
      rescue Exception => e
        @errors << "Exception occurred: " + e.to_s
        nil
      end
    end

    # Adds an article to an existing ticket. Either `TicketID` or `TicketNumber`
    # has to be given in the options.
    #
    # For other options visit:
    # http://otrs.github.io/doc/manual/admin/stable/en/html/genericinterface.html#genericinterface-connectors
    #
    # Example:
    # add_article(
    #   'Subject of the Article',
    #   'Content of the Article',
    #   {
    #     'TicketID' => 1, # OR 'TicketNumber' => 2010080210123456
    #     #OPTIONAL START#
    #     'Article' => {
    #       'ArticleType' => 'note-internal',
    #       'OtherArticleOption' => 'value'
    #     }
    #     #OPTIONAL END#
    #   }
    # )
    def add_article( subject, text, options = {} )
      begin
        # Call `ticket_add_article` to check the given parameters and create a
        # SOAP request using savon.
        response = ticket_add_article( subject, text, options )
        # Check if the the response is successful.
        if response.success?
          # Strip from unusefull information
          @response = response.body[:ticket_update_response]
          # Check if the response contains an ticked id.
          if @response.include?( :ticket_id )
            # Return the response
            @response

          # Error handling
          # use `.errors` to see if any errors occurred.
          #
          # example:
          # otrs_ticket = Ottick::Ticket.new()
          # otrs_ticket.add_article( #params# )
          # otrs_ticket.errors
          #
          # This will return an array with errors if there are any.
          # Otherwise it will return an empty array.
          else
            @errors << "#{@response[:error][:error_code]} => #{@response[:error][:error_message]}"
            nil
          end
        elsif response.soap_fault?
          @errors << response.body.to_s
          nil
        else
          @errors << response.http.to_s
          nil
        end
      rescue Exception => e
        @errors << "Exception occurred: " + e.to_s
        nil
      end
    end

    # Search an article based on options given in:
    # http://otrs.github.io/doc/manual/admin/stable/en/html/genericinterface.html#genericinterface-connectors
    #
    # Example:
    # search('TicketNumber' => 100100)
    # => {:ticket_id=>"99", :@xmlns=>"http://www.otrs.org/TicketConnector"}
    def search(options = {})
      begin
        # Call `ticket_search` to check the given parameters and create a
        # SOAP request using savon.
        response = ticket_search(options)
        # Check if the the response is successful.
        if response.success?
          # Strip from unusefull information
          @response = response.body[:ticket_search_response]
          # Check if the response contains an ticked id.
          if @response.include?(:ticket_id)
            # Return the response
            @response
          elsif @response.nil?
            @errors << 'No results for given options'

          # Error handling
          # use `.errors` to see if any errors occurred.
          #
          # example:
          # otrs_ticket = Ottick::Ticket.new()<commit_msg>valid error handling for search<commit_after>require 'savon'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/hash'

module Ottick
  class Ticket
    attr_reader :options, :client, :errors, :response

    # Ticket.new(options)
    # for possible options see Savon.client()
    # http://savonrb.com/version2/client.html
    #
    def initialize(options = {})
      sanitize_options!(options)
      @otrs_credentials = otrs_credentials!(options)
      @options          = default_options.merge(options)
      @client           = Savon.client(@options)
      @errors           = []
      @response         = nil
    end

    def get(options = {})
      begin
        response = ticket_get(options)
        if response.success?
          @response = response.body[:ticket_get_response]
          if @response.include?(:ticket)
            @response
          else
            @errors << "#{@response[:error][:error_code]} => #{@response[:error][:error_message]}"
            nil
          end
        elsif response.soap_fault?
          @errors << response.body.to_s
          nil
        else
          @errors << response.http.to_s
          nil
        end
      rescue Exception => e
        @errors << "Exception occurred: " + e.to_s
        nil
      end
    end

    def create(subject, text, options = {})
      begin
        response = ticket_create(subject, text, options)
        if response.success?
          @response = response.body[:ticket_create_response]
          if @response.include?(:ticket_id)
            @response
          else
            @errors << "#{@response[:error][:error_code]} => #{@response[:error][:error_message]}"
            nil
          end
        elsif response.soap_fault?
          @errors << response.body.to_s
          nil
        else
          @errors << response.http.to_s
          nil
        end
      rescue Exception => e
        @errors << "Exception occurred: " + e.to_s
        nil
      end
    end

    # Adds an article to an existing ticket. Either `TicketID` or `TicketNumber`
    # has to be given in the options.
    #
    # For other options visit:
    # http://otrs.github.io/doc/manual/admin/stable/en/html/genericinterface.html#genericinterface-connectors
    #
    # Example:
    # add_article(
    #   'Subject of the Article',
    #   'Content of the Article',
    #   {
    #     'TicketID' => 1, # OR 'TicketNumber' => 2010080210123456
    #     #OPTIONAL START#
    #     'Article' => {
    #       'ArticleType' => 'note-internal',
    #       'OtherArticleOption' => 'value'
    #     }
    #     #OPTIONAL END#
    #   }
    # )
    def add_article( subject, text, options = {} )
      begin
        # Call `ticket_add_article` to check the given parameters and create a
        # SOAP request using savon.
        response = ticket_add_article( subject, text, options )
        # Check if the the response is successful.
        if response.success?
          # Strip from unusefull information
          @response = response.body[:ticket_update_response]
          # Check if the response contains an ticked id.
          if @response.include?( :ticket_id )
            # Return the response
            @response

          # Error handling
          # use `.errors` to see if any errors occurred.
          #
          # example:
          # otrs_ticket = Ottick::Ticket.new()
          # otrs_ticket.add_article( #params# )
          # otrs_ticket.errors
          #
          # This will return an array with errors if there are any.
          # Otherwise it will return an empty array.
          else
            @errors << "#{@response[:error][:error_code]} => #{@response[:error][:error_message]}"
            nil
          end
        elsif response.soap_fault?
          @errors << response.body.to_s
          nil
        else
          @errors << response.http.to_s
          nil
        end
      rescue Exception => e
        @errors << "Exception occurred: " + e.to_s
        nil
      end
    end

    # Search an article based on options given in:
    # http://otrs.github.io/doc/manual/admin/stable/en/html/genericinterface.html#genericinterface-connectors
    #
    # Example:
    # search('TicketNumber' => 100100)
    # => {:ticket_id=>"99", :@xmlns=>"http://www.otrs.org/TicketConnector"}
    def search(options = {})
      begin
        # Call `ticket_search` to check the given parameters and create a
        # SOAP request using savon.
        response = ticket_search(options)
        # Check if the the response is successful.
        if response.success?
          # Strip from unusefull information
          @response = response.body[:ticket_search_response]
          # Check if the response contains an ticked id.
          if @response.include?(:ticket_id)
            # Return the response
            @response
          elsif !@response.include?(:error)
            @errors << 'No results for given options'
            nil

          # Error handling
          # use `.errors` to see if any errors occurred.
          #
          # example:
          # otrs_ticket = Ottick::Ticket.new()
{'fraction_non_alphanumeric': 0.1037080520497016, 'fraction_numerical': 0.005087564817532531, 'mean_word_length': 1.8713483146067416, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '1736823', 'n_tokens_mistral': 2936, 'n_tokens_neox': 2849, 'n_words': 724}