<commit_before>module Services
  module Protips
    module FakeHawtService
      def feature!
        Rails.logger.ap("#{self.class.name}#feature!")
      end

      def unfeature!
        Rails.logger.ap("#{self.class.name}#unfeature!")
      end

      def hawt?
        Rails.logger.ap("#{self.class.name}#hawt?")

        false
      end
    end

    module RealHawtService
      def feature!
        url = URI.parse("#{ENV['PRIVATE_URL']}/api/v1/protips/feature.json").to_s
        protip_json = MultiJson.load(protip_hash.to_json)

        RestClient.post(url, protip_json, accept: :json, content_type: 'application/json')
      end

      def unfeature!
        url = URI.parse("#{ENV['PRIVATE_URL']}/api/v1/protips/unfeature.json").to_s
        protip_json = MultiJson.load(protip_hash.to_json)

        RestClient.post(url, protip_json, accept: :json, content_type: 'application/json')
      end

      def hawt?
        url = URI.parse("#{ENV['PRIVATE_URL']}/api/v1/protips/hawt.json").to_s
        protip_json = MultiJson.load(protip_hash.to_json)

        JSON.parse(RestClient.post(url, protip_json, accept: :json, content_type: 'application/json').to_s)['hawt?']
      end
    end

    class HawtService
      if ENV['ENABLE_PRIVATE_API'] || Rails.env.production?
        include RealHawtService
      else
        include FakeHawtService
      end

      def initialize(protip)<commit_msg>Cache calls to Hawt service endpoints<commit_after>module Services
  module Protips
    module FakeHawtService
      def feature!
        Rails.logger.ap("#{self.class.name}#feature!")
      end

      def unfeature!
        Rails.logger.ap("#{self.class.name}#unfeature!")
      end

      def hawt?
        Rails.logger.ap("#{self.class.name}#hawt?")

        false
      end
    end

    module RealHawtService
      def feature!
        url = URI.parse("#{ENV['PRIVATE_URL']}/api/v1/protips/feature.json").to_s
        protip_json = MultiJson.load(protip_hash.to_json)

        Rails.cache.fetch(['hawt_feature', url, protip_hash[:public_id]], expires: 1.hour) do
          RestClient.post(url, protip_json, accept: :json, content_type: 'application/json')
        end
      end

      def unfeature!
        url = URI.parse("#{ENV['PRIVATE_URL']}/api/v1/protips/unfeature.json").to_s
        protip_json = MultiJson.load(protip_hash.to_json)

        Rails.cache.fetch(['hawt_unfeature', url, protip_hash[:public_id]], expires: 1.hour) do
          RestClient.post(url, protip_json, accept: :json, content_type: 'application/json')
        end
      end

      def hawt?
        url = URI.parse("#{ENV['PRIVATE_URL']}/api/v1/protips/hawt.json").to_s
        protip_json = MultiJson.load(protip_hash.to_json)

        Rails.cache.fetch(['hawt_hawt', url, protip_hash[:public_id]], expires: 1.hour) do
          JSON.parse(RestClient.post(url, protip_json, accept: :json, content_type: 'application/json').to_s)['hawt?']
        end
      end
    end

    class HawtService
      if ENV['ENABLE_PRIVATE_API'] || Rails.env.production?
        include RealHawtService
      else
        include FakeHawtService
      end

      def initialize(protip)
{'fraction_non_alphanumeric': 0.12678288431061807, 'fraction_numerical': 0.0028526148969889066, 'mean_word_length': 2.7216981132075473, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5307464', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1044, 'n_words': 175}