<commit_before>module FbGraph
  class Collection < Array
    attr_reader :previous, :next, :total_count

    def initialize(collection = nil)
      collection = case collection
      when Array
        {:data => collection, :count => collection.size}
      when Hash
        collection[:data] ||= []
        collection
      when nil
        collection = {:data => [], :count => 0}
      else
        raise ArgumentError.new("Invalid collection")
      end

      # NOTE: Graph API returns {"data":{"to":[null]}} sometimes... :(
      collection[:data].delete_if(&:nil?)

      result = replace(collection[:data])
      @total_count = collection[:count]
      @previous, @next = {}, {}
      if (paging = collection[:paging])
        if paging[:previous]
          @previous = fetch_params(paging[:previous])
        end
        if paging[:next]
          @next = fetch_params(paging[:next])
        end
      end
    end

    private

    def fetch_params(url)
      query = URI.parse(url).query
      params = {}
      query.split('&').each do |q|
        key, value = q.split('=')
        if ['limit', 'offset', 'until', 'since'].include?(key)
          params[key.to_sym] = URI.unescape(value)
        end
      end
      params
    end
  end
end<commit_msg>Try to fix URI encode problems.<commit_after>module FbGraph
  class Collection < Array
    attr_reader :previous, :next, :total_count

    def initialize(collection = nil)
      collection = case collection
      when Array
        {:data => collection, :count => collection.size}
      when Hash
        collection[:data] ||= []
        collection
      when nil
        collection = {:data => [], :count => 0}
      else
        raise ArgumentError.new("Invalid collection")
      end

      # NOTE: Graph API returns {"data":{"to":[null]}} sometimes... :(
      collection[:data].delete_if(&:nil?)

      result = replace(collection[:data])
      @total_count = collection[:count]
      @previous, @next = {}, {}
      if (paging = collection[:paging])
        if paging[:previous]
          @previous = fetch_params(paging[:previous])
        end
        if paging[:next]
          @next = fetch_params(paging[:next])
        end
      end
    end

    private

    def fetch_params(url)
      query = URI.parse(URI.encode(url)).query
      params = {}
      query.split('&').each do |q|
        key, value = q.split('=')
        if ['limit', 'offset', 'until', 'since'].include?(key)
          params[key.to_sym] = URI.unescape(value)
        end
      end
      params
    end
  end
end
{'fraction_non_alphanumeric': 0.1370399373531715, 'fraction_numerical': 0.0007830853563038371, 'mean_word_length': 2.2017543859649122, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 11, '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': '3365146', 'n_tokens_mistral': 740, 'n_tokens_neox': 723, 'n_words': 182}