<commit_before>require File.expand_path('../../utils', __FILE__)
require 'json'

module Newspilot
  module Resource
    attr_accessor :attributes
    attr_accessor :id

    def initialize(attributes = {})
      @attributes = attributes
    end

    def id
      (attributes['id'] ||= nil)
    end

    def self.included(base)
      base.extend ClassMethods
    end

    def method_missing(method, *_args, &_block)
      return @attributes[method.to_s] if @attributes.key?(method.to_s)
    end

    def respond_to_missing?(method_name, include_private = false)
      @attributes.key?(method.to_s) || super
    end

    module ClassMethods
      def construct_from_response(payload)
        resource_body = if payload.size > 2
                          payload
                        else
                          payload[1].first
                        end
        resource_body.delete('link')
        new resource_body
      end

      def find(id)
        response = Newspilot.get(collection_with_id(id))
        construct_from_response JSON.parse(response.body)
      end

      def where(options = {})
        options = preprocess_options(options)
        response = Newspilot.get collection_name, options
        response = JSON.parse(response.body)
        response.first[1].map { |attributes| construct_from_response attributes }
      end

      alias_method :all, :where

      # private

      def resource_name
        Utils.demodulize name
      end

      def collection_name
        Utils.pluralize Utils.camelize_lower(resource_name)
      end

      def collection_with_id(id)
        "#{collection_name}/#{id}"
      end

      def preprocess_options(options)
        options = options.merge(query_depts) if Newspilot.configuration.departments?
        [:photoDate, :prodDate].each do |date_param|
          options[date_param] = date_range_parse(options[date_param]) if options[date_param]
        end
        options
      end

      # add departments from the configuration to the query
      def query_depts
        { 'respDepartment' => Newspilot.configuration.departments }
      end

      def date_range_parse(date_range)
        return unless date_range.is_a?(Range)
        a = date_range.to_a
        return if !a.first.is_a?(Date) && !a.last.is_a?(Date)

        "#{a.first.strftime('%Y-%m-%d')} TO " \
          "#{a.last.strftime('%Y-%m-%d')}"
      end
    end
  end
end<commit_msg>Support :updated_after attribute<commit_after>require File.expand_path('../../utils', __FILE__)
require 'json'

module Newspilot
  module Resource
    attr_accessor :attributes
    attr_accessor :id

    def initialize(attributes = {})
      @attributes = attributes
    end

    def id
      (attributes['id'] ||= nil)
    end

    def self.included(base)
      base.extend ClassMethods
    end

    def method_missing(method, *_args, &_block)
      return @attributes[method.to_s] if @attributes.key?(method.to_s)
    end

    def respond_to_missing?(method_name, include_private = false)
      @attributes.key?(method.to_s) || super
    end

    module ClassMethods
      def construct_from_response(payload)
        resource_body = if payload.size > 2
                          payload
                        else
                          payload[1].first
                        end
        resource_body.delete('link')
        new resource_body
      end

      def find(id)
        response = Newspilot.get(collection_with_id(id))
        construct_from_response JSON.parse(response.body)
      end

      def where(options = {})
        options = preprocess_options(options)
        response = Newspilot.get collection_name, options
        response = JSON.parse(response.body)
        response.first[1].map { |attributes| construct_from_response attributes }
      end

      alias_method :all, :where

      # private

      def resource_name
        Utils.demodulize name
      end

      def collection_name
        Utils.pluralize Utils.camelize_lower(resource_name)
      end

      def collection_with_id(id)
        "#{collection_name}/#{id}"
      end

      def preprocess_options(options)
        options = options.merge(query_depts) if Newspilot.configuration.departments?
        [:photoDate, :prodDate].each do |date_param|
          options[date_param] = date_range_parse(options[date_param]) if options[date_param]
        end
        options = updated_after(options) if options[:updated_after]
        options
      end

      # add departments from the configuration to the query
      def query_depts
        { 'respDepartment' => Newspilot.configuration.departments }
      end

      def updated_after(options)
        updated_after = options.delete(:updated_after)
        return options unless updated_after.is_a?(Time)
        updated_range = updated_after.strftime('%Y-%m-%dT%H:%M:%S.%L%:z') + ' TO ' +
                        (updated_after + 2.years).strftime('%Y-%m-%dT%H:%M:%S.%L%:z')
        options.merge(updatedDate: updated_range)
      end

      def date_range_parse(date_range)
        return unless date_range.is_a?(Range)
        a = date_range.to_a
        return if !a.first.is_a?(Date) && !a.last.is_a?(Date)

        "#{a.first.strftime('%Y-%m-%d')} TO " \
          "#{a.last.strftime('%Y-%m-%d')}"
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.0987724268177526, 'fraction_numerical': 0.0013220018885741266, 'mean_word_length': 2.2853598014888337, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '7373030', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1543, 'n_words': 317}