<commit_before>module Landable
  module EventPublisher
    mattr_accessor :page_view, :visit, :event_type, :amqp_configuration

    class << self
      def publish(page_view)
        return unless amqp_service_enabled?

        event_type = amqp_event_mapping[page_view.path]
        if event_type.is_a?(Hash)
          request_type = page_view.http_method
          event_type = event_type[request_type]
        end

        @page_view = page_view
        @visit = page_view.visit
        if event_type
          @event_type = event_type
        end
        amqp_messaging_service.publish(message)
      end

      private

      def amqp_config_hash
        @amqp_config_hash = Landable.configuration.amqp_configuration
      end

      def amqp_enabled?
        @amqp_enabled ||= amqp_config_hash[:enabled]
      end

      def amqp_event_mapping
        @amqp_event_mapping ||= amqp_config_hash[:event_mapping]
      end

      def amqp_site_segment
        @amqp_site_segment ||= amqp_config_hash[:site_segment]
      end

      def amqp_messaging_service
        @amqp_messaging_service ||= amqp_config_hash[:messaging_service]
      end

      def amqp_service_enabled?
        amqp_enabled? && amqp_messaging_service.present?
      end

      def message
        visit = @visit
        attribution = visit.try(:attribution)
        referer = visit.try(:referer)
        visitor = visit.visitor
        page_view = @page_view
        user_agent = visitor.try(:raw_user_agent)
        user_agent_type = user_agent.try(:raw_user_agent_type)
        event_type = @event_type
        {
          site_segment: amqp_site_segment,
          visit_id: visit.id,
          event: event_type.to_s,
          page_view_id: page_view.page_view_id,
          request_type: page_view.http_method,
          created_at: page_view.created_at,
          cookie_id: visit.cookie_id,
          owner_id: visit.try(:owner_id),
          owner: visit.try(:owner).try(:owner),
          referer_id: referer.try(:id),
          domain_id: referer.try(:domain_id),
          domain: referer.try(:domain),
          ip_address_id: visitor.try(:ip_address_id),
          ip_address: visitor.try(:ip_address).try(:to_s),
          user_agent_id: user_agent.try(:id),
          user_agent: user_agent.try(:user_agent),
          user_agent_type_id: user_agent_type.try(:id),
          user_agent_type: user_agent_type.try(:user_agent_type),<commit_msg>amqp_via_bunny: removed amqp prefixes from methods inside the event_publisher class.<commit_after>module Landable
  module EventPublisher
    mattr_accessor :page_view, :visit, :event_type, :amqp_configuration

    class << self
      def publish(page_view)
        return unless service_enabled?

        event_type = event_mapping[page_view.path]
        if event_type.is_a?(Hash)
          request_type = page_view.http_method
          event_type = event_type[request_type]
        end

        @page_view = page_view
        @visit = page_view.visit
        if event_type
          @event_type = event_type
        end
        messaging_service.publish(message)
      end

      private

      def config_hash
        @config_hash = Landable.configuration.amqp_configuration
      end

      def enabled?
        @enabled ||= config_hash[:enabled]
      end

      def event_mapping
        @event_mapping ||= config_hash[:event_mapping]
      end

      def site_segment
        @site_segment ||= config_hash[:site_segment]
      end

      def messaging_service
        @messaging_service ||= config_hash[:messaging_service]
      end

      def service_enabled?
        enabled? && messaging_service.present?
      end

      def message
        visit = @visit
        attribution = visit.try(:attribution)
        referer = visit.try(:referer)
        visitor = visit.visitor
        page_view = @page_view
        user_agent = visitor.try(:raw_user_agent)
        user_agent_type = user_agent.try(:raw_user_agent_type)
        event_type = @event_type
        {
          site_segment: site_segment,
          visit_id: visit.id,
          event: event_type.to_s,
          page_view_id: page_view.page_view_id,
          request_type: page_view.http_method,
          created_at: page_view.created_at,
          cookie_id: visit.cookie_id,
          owner_id: visit.try(:owner_id),
          owner: visit.try(:owner).try(:owner),
          referer_id: referer.try(:id),
          domain_id: referer.try(:domain_id),
          domain: referer.try(:domain),
          ip_address_id: visitor.try(:ip_address_id),
          ip_address: visitor.try(:ip_address).try(:to_s),
          user_agent_id: user_agent.try(:id),
          user_agent: user_agent.try(:user_agent),
          user_agent_type_id: user_agent_type.try(:id),
          user_agent_type: user_agent_type.try(:user_agent_type),
{'fraction_non_alphanumeric': 0.08322981366459627, 'fraction_numerical': 0.0, 'mean_word_length': 2.4581245526127415, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '5700728', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1484, 'n_words': 250}