<commit_before># frozen-string-literal: true

require 'andpush'
require 'active_support/core_ext/hash/transform_values'

module Pushing
  module Adapters
    class AndpushAdapter
      def initialize(fcm_settings)
        @server_key = fcm_settings.server_key
      end

      def push!(notification)
        FcmResponse.new(self.class.client(@server_key).push(notification.payload))
      rescue => e
        raise Pushing::FcmDeliveryError.new("Error while trying to send push notification: #{e.message}", FcmResponse.new(e.response))
      end

      @@semaphore = Mutex.new

      def self.client(server_key)
        defined?(@client) ? @client : @@semaphore.synchronize { @client = Andpush.build(server_key) }
      end

      class FcmResponse < SimpleDelegator
        def code
          __getobj__.code.to_i
        end

        def headers
          __getobj__.headers.transform_values {|value| value.join(", ") }
        end
      end

      private_constant :FcmResponse
    end
  end
end
<commit_msg>Do not assign a new client on race condition<commit_after># frozen-string-literal: true

require 'andpush'
require 'active_support/core_ext/hash/transform_values'

module Pushing
  module Adapters
    class AndpushAdapter
      def initialize(fcm_settings)
        @server_key = fcm_settings.server_key
      end

      def push!(notification)
        FcmResponse.new(self.class.client(@server_key).push(notification.payload))
      rescue => e
        raise Pushing::FcmDeliveryError.new("Error while trying to send push notification: #{e.message}", FcmResponse.new(e.response))
      end

      @@semaphore = Mutex.new

      def self.client(server_key)
        defined?(@client) ? @client : @@semaphore.synchronize { @client ||= Andpush.build(server_key) }
      end

      class FcmResponse < SimpleDelegator
        def code
          __getobj__.code.to_i
        end

        def headers
          __getobj__.headers.transform_values {|value| value.join(", ") }
        end
      end

      private_constant :FcmResponse
    end
  end
end

{'fraction_non_alphanumeric': 0.09139523578026251, 'fraction_numerical': 0.0, 'mean_word_length': 2.776146788990826, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5097133', 'n_tokens_mistral': 624, 'n_tokens_neox': 609, 'n_words': 134}