<commit_before>require 'active_support'
require 'active_support/core_ext/string/inflections'  # For #model_class constantize
require 'active_support/core_ext/array/access'
require 'active_support/core_ext/object/try'          # For #find
require 'uri'

class GlobalID
  class << self
    attr_accessor :app

    def create(model, options = {})
      app = options.fetch :app, GlobalID.app
      raise ArgumentError, "An app is required to create a GlobalID. Pass the :app option or set the default GlobalID.app." unless app
      new URI("gid://#{app}/#{model.class.name}/#{model.id}")
    end

    def find(gid, options = {})
      parse(gid).try(:find, options)
    end

    def parse(gid)
      gid.is_a?(self) ? gid : new(gid)
    rescue URI::Error
      parse_encoded_gid(gid)
    end

    def parse_encoded_gid(gid)
      new Base64.urlsafe_decode64(repad_gid(gid)) rescue nil
    end
    
    private
      # We removed the base64 padding character = during #to_param, now we're adding it back so decoding will work
      def repad_gid(gid)
        padding_chars = 4 - gid.length % 4
        gid + ('=' * padding_chars)
      end
  end

  attr_reader :uri, :app, :model_name, :model_id<commit_msg>Fix padding bug<commit_after>require 'active_support'
require 'active_support/core_ext/string/inflections'  # For #model_class constantize
require 'active_support/core_ext/array/access'
require 'active_support/core_ext/object/try'          # For #find
require 'uri'

class GlobalID
  class << self
    attr_accessor :app

    def create(model, options = {})
      app = options.fetch :app, GlobalID.app
      raise ArgumentError, "An app is required to create a GlobalID. Pass the :app option or set the default GlobalID.app." unless app
      new URI("gid://#{app}/#{model.class.name}/#{model.id}")
    end

    def find(gid, options = {})
      parse(gid).try(:find, options)
    end

    def parse(gid)
      gid.is_a?(self) ? gid : new(gid)
    rescue URI::Error
      parse_encoded_gid(gid)
    end

    def parse_encoded_gid(gid)
      new Base64.urlsafe_decode64(repad_gid(gid)) rescue nil
    end
    
    private
      # We removed the base64 padding character = during #to_param, now we're adding it back so decoding will work
      def repad_gid(gid)
        padding_chars = gid.length.modulo(4).zero? ? 0 : (4 - gid.length.modulo(4))
        gid + ('=' * padding_chars)
      end
  end

  attr_reader :uri, :app, :model_name, :model_id
{'fraction_non_alphanumeric': 0.10852713178294573, 'fraction_numerical': 0.0073439412484700125, 'mean_word_length': 3.066334991708126, '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': '6480983', 'n_tokens_mistral': 828, 'n_tokens_neox': 792, 'n_words': 216}