<commit_before>module Facelauncher
  class Model
    extend ActiveModel::Naming
    include ActiveModel::Conversion
    include ActiveModel::Serializers::JSON
    include ActiveModel::Validations

    def initialize(attributes = {})
      self.attributes = attributes
    end

    def attributes=(hash = {})
      unless hash.nil?
        hash.each do |key, value|
          instance_variable_set("@#{key}", value)
        end
      end
    end

    def attributes
      instance_values
    end

    def persisted?
      false
    end

    def self.authentication_required_on(*methods)
      @authentication_required_on = methods.flatten
    end

    def self.authentication_required_on?(method)
      !@authentication_required_on.nil? && (@authentication_required_on.include?(method.to_sym) || @authentication_required_on.include?(method.to_s))
    end

    def self.all(limit=nil, offset=nil)
      class_name = self.name.demodulize.underscore.pluralize
      attributes = Rails.cache.fetch("/#{class_name}?limit=#{limit}&offset=#{offset}-#{cache_timestamp}", :expires_in => cache_expiration) do
        attributes = {}
        Faraday.new(:url => self.facelauncher_url) do |conn|
          conn.adapter :net_http
          if authentication_required_on? :all
            conn.basic_auth Facelauncher::Model.facelauncher_program_id, Facelauncher::Model.facelauncher_program_access_key
          end

          request_args = { program_id: self.facelauncher_program_id }
          request_args[:limit] = limit unless limit.nil?
          request_args[:offset] = offset unless offset.nil?

          response = conn.get("/#{class_name}.json", request_args)
          attributes = response.status == 200 ? response.body : nil
        end
        attributes
      end

      if !attributes.nil?
        ActiveSupport.parse_json_times = true

        items = []
        attributes_for_all_items = ActiveSupport::JSON.decode(attributes)
        attributes_for_all_items.each do |attributes_for_item|
          items << self.new(attributes_for_item)
        end
        items
      end
    end

    def self.find(id)
      class_name = self.name.demodulize.underscore.pluralize
      attributes = Rails.cache.fetch("/#{class_name}/#{id}-#{cache_timestamp}", :expires_in => cache_expiration) do
        Faraday.new(:url => self.facelauncher_url) do |conn|
          conn.adapter :net_http
          if authentication_required_on? :find
            conn.basic_auth Facelauncher::Model.facelauncher_program_id, Facelauncher::Model.facelauncher_program_access_key
          end

          response = conn.get("/#{class_name}/#{id}.json")
          attributes = response.status == 200 ? response.body : nil
        end
        attributes
      end

      if !attributes.nil?
        ActiveSupport.parse_json_times = true

        photo = self.new.from_json(attributes, false)
      else
        raise ArgumentError, "Couldn't find #{self.name.demodulize} with id=#{id}"
      end
    end

    def self.facelauncher_url
      if !defined? @@facelauncher_url
        @@facelauncher_url = ENV.key?('FACELAUNCHER_URL') ? ENV['FACELAUNCHER_URL'] : Facelauncher::Engine.config.server_url
      end

      @@facelauncher_url
    end

    def self.facelauncher_program_id
      if !defined? @@facelauncher_program_id
        @@facelauncher_program_id = ENV.key?('FACELAUNCHER_PROGRAM_ID') ? ENV['FACELAUNCHER_PROGRAM_ID'] : Facelauncher::Engine.config.program_id
      end

      @@facelauncher_program_id
    end

    def self.facelauncher_program_access_key
      if !defined? @@facelauncher_program_access_key
        @@facelauncher_program_access_key = ENV.key?('FACELAUNCHER_PROGRAM_ACCESS_KEY') ? ENV['FACELAUNCHER_PROGRAM_ACCESS_KEY'] : Facelauncher::Engine.config.program_access_key
      end

      @@facelauncher_program_access_key
    end

    def self.facelauncher_app_id
      if !defined? @@facelauncher_app_id
        @@facelauncher_app_id = ENV.key?('FACELAUNCHER_APP_ID') ? ENV['FACELAUNCHER_APP_ID'] : Facelauncher::Engine.config.program_app_id
      end

      @@facelauncher_app_id
    end

    #protected

    def self.attributes=(*attributes)
      attributes = attributes.flatten
      attributes.each do |attribute|
        attr_accessor attribute

        self.class.send :define_method, "find_by_#{attribute}" do |value|
          class_name = self.name.demodulize.underscore.pluralize
          attributes = Rails.cache.fetch("/#{class_name}/find_by_#{attribute}/#{value.to_s.underscore}-#{cache_timestamp}", :expires_in => cache_expiration) do
            Faraday.new(:url => self.facelauncher_url) do |conn|
              conn.adapter :net_http
              if authentication_required_on? "find_by_#{attribute}"
                conn.basic_auth Facelauncher::Model.facelauncher_program_id, Facelauncher::Model.facelauncher_program_access_key
              end

              response = conn.get("/#{class_name}.json", { attribute => value })
              attributes = response.status == 200 ? response.body : nil
            end
            attributes
          end

          if !attributes.nil?
            ActiveSupport.parse_json_times = true

            items = []
            attributes_for_all_photos = ActiveSupport::JSON.decode(attributes)<commit_msg>Added some documentation to model.rb<commit_after>module Facelauncher
  class Model
    extend ActiveModel::Naming
    include ActiveModel::Conversion
    include ActiveModel::Serializers::JSON
    include ActiveModel::Validations

    # Initializes a new object
    #
    # @param attributes [Hash] The attributes
    def initialize(attributes = {})
      self.attributes = attributes
    end

    # Sets the attributes of an object
    #
    # @param attributes [Hash] The attributes
    def attributes=(attributes = {})
      unless attributes.nil?
        attributes.each do |key, value|
          instance_variable_set("@#{key}", value)
        end
      end
    end

    # Gets the attributes of an object
    #
    # @return [Hash] The attributes
    def attributes
      instance_values
    end

    def persisted?
      false
    end

    def self.authentication_required_on(*methods)
      @authentication_required_on = methods.flatten
    end

    def self.authentication_required_on?(method)
      !@authentication_required_on.nil? && (@authentication_required_on.include?(method.to_sym) || @authentication_required_on.include?(method.to_s))
    end

    # Select all items from Facelauncher
    #
    # @param limit [FixNum] An optional limit to the number of results
    # @param offset [FixNum] An optional offset to set the item from which to start
    # @return [Array<Facelauncher::Model>] An array of instances of Facelauncher::Model
    def self.all(limit=nil, offset=nil)
      class_name = self.name.demodulize.underscore.pluralize
      attributes = Rails.cache.fetch("/#{class_name}?limit=#{limit}&offset=#{offset}-#{cache_timestamp}", :expires_in => cache_expiration) do
        attributes = {}
        Faraday.new(:url => self.facelauncher_url) do |conn|
          conn.adapter :net_http
          if authentication_required_on? :all
            conn.basic_auth Facelauncher::Model.facelauncher_program_id, Facelauncher::Model.facelauncher_program_access_key
          end

          request_args = { program_id: self.facelauncher_program_id }
          request_args[:limit] = limit unless limit.nil?
          request_args[:offset] = offset unless offset.nil?

          response = conn.get("/#{class_name}.json", request_args)
          attributes = response.status == 200 ? response.body : nil
        end
        attributes
      end

      if !attributes.nil?
        ActiveSupport.parse_json_times = true

        items = []
        attributes_for_all_items = ActiveSupport::JSON.decode(attributes)
        attributes_for_all_items.each do |attributes_for_item|
          items << self.new(attributes_for_item)
        end
        items
      end
    end

    # Retrieve one item from Facelauncher selected by ID.
    #
    # @param id [String, FixNum] The ID of the item to select
    # @return [Facelauncher::Model] An instance of Facelauncher::Model
    def self.find(id)
      class_name = self.name.demodulize.underscore.pluralize
      attributes = Rails.cache.fetch("/#{class_name}/#{id}-#{cache_timestamp}", :expires_in => cache_expiration) do
        Faraday.new(:url => self.facelauncher_url) do |conn|
          conn.adapter :net_http
          if authentication_required_on? :find
            conn.basic_auth Facelauncher::Model.facelauncher_program_id, Facelauncher::Model.facelauncher_program_access_key
          end

          response = conn.get("/#{class_name}/#{id}.json")
          attributes = response.status == 200 ? response.body : nil
        end
        attributes
      end

      if !attributes.nil?
        ActiveSupport.parse_json_times = true

        photo = self.new.from_json(attributes, false)
      else
        raise ArgumentError, "Couldn't find #{self.name.demodulize} with id=#{id}"
      end
    end

    def self.facelauncher_url
      if !defined? @@facelauncher_url
        @@facelauncher_url = ENV.key?('FACELAUNCHER_URL') ? ENV['FACELAUNCHER_URL'] : Facelauncher::Engine.config.server_url
      end

      @@facelauncher_url
    end

    def self.facelauncher_program_id
      if !defined? @@facelauncher_program_id
        @@facelauncher_program_id = ENV.key?('FACELAUNCHER_PROGRAM_ID') ? ENV['FACELAUNCHER_PROGRAM_ID'] : Facelauncher::Engine.config.program_id
      end

      @@facelauncher_program_id
    end

    def self.facelauncher_program_access_key
      if !defined? @@facelauncher_program_access_key
        @@facelauncher_program_access_key = ENV.key?('FACELAUNCHER_PROGRAM_ACCESS_KEY') ? ENV['FACELAUNCHER_PROGRAM_ACCESS_KEY'] : Facelauncher::Engine.config.program_access_key
      end

      @@facelauncher_program_access_key
    end

    def self.facelauncher_app_id
      if !defined? @@facelauncher_app_id
        @@facelauncher_app_id = ENV.key?('FACELAUNCHER_APP_ID') ? ENV['FACELAUNCHER_APP_ID'] : Facelauncher::Engine.config.program_app_id
      end

      @@facelauncher_app_id
    end

    #protected

    # Sets the possible attributes for a subclass of Facelauncher::Model.
    #
    # @param attributes [Array]
    def self.attributes=(*attributes)
      attributes = attributes.flatten
      attributes.each do |attribute|
        attr_accessor attribute

        self.class.send :define_method, "find_by_#{attribute}" do |value|
          class_name = self.name.demodulize.underscore.pluralize
          attributes = Rails.cache.fetch("/#{class_name}/find_by_#{attribute}/#{value.to_s.underscore}-#{cache_timestamp}", :expires_in => cache_expiration) do
            Faraday.new(:url => self.facelauncher_url) do |conn|
              conn.adapter :net_http
              if authentication_required_on? "find_by_#{attribute}"
                conn.basic_auth Facelauncher::Model.facelauncher_program_id, Facelauncher::Model.facelauncher_program_access_key
              end

              response = conn.get("/#{class_name}.json", { attribute => value })
              attributes = response.status == 200 ? response.body : nil
            end
            attributes
          end

          if !attributes.nil?
            ActiveSupport.parse_json_times = true

            items = []
            attributes_for_all_photos = ActiveSupport::JSON.decode(attributes)
{'fraction_non_alphanumeric': 0.09597063193776768, 'fraction_numerical': 0.001573289048160126, 'mean_word_length': 2.9184931506849314, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 18, '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': '7297744', 'n_tokens_mistral': 3557, 'n_tokens_neox': 3376, 'n_words': 632}