<commit_before>module Redrax
  class Container
    extend Redrax::DocsLinkable
    
    attr_reader :client, :name, :count, :size

    def self.from_hash(client, params = {})
      new(client, *params.values_at("name", "count", "size"))
    end

    def initialize(client, name, count = 0, size = 0)
      @client = client
      @name   = name
      @count  = 0
      @size   = 0
    end

    docs "http://docs.rackspace.com/files/api/v1/cf-devguide/content/GET_listcontainerobjects_v1__account___container__containerServicesOperations_d1e000.html"
    # @return [PaginatedFiles] An `Array` of `File` objects proxying for files
    #   stored on this `Container`.
    def files(options = {})
      resp = client.request(
        method:   :get,
        path:     name,
        params:   options,
        expected: (200..299)
      )
      PaginatedFiles.new(
        resp.map { |f| Redrax::File.from_hash(client, f) },
        self,
        options
      )
    end

    # @return [Redrax::Metadata] The `Metadata` object for this `Container`
    def metadata
      @metadata ||= Metadata.new(client, self)
    end

    # Behavior for manipulating metadata on a container
    class Metadata
      extend Redrax::DocsLinkable

      attr_reader :client, :container_name
      
      def initialize(client, container)
        @client = client
        @container_name = container.name
      end

      docs "http://docs.rackspace.com/files/api/v1/cf-devguide/content/HEAD_retrievecontainermeta_v1__account___container__containerServicesOperations_d1e000.html"
      # @return [Hash] The metadata defined on a container. Keys will be 
      #   stripped of their "X-Container-Meta-" prefix, e.g.
      #   "X-Container-Meta-foo" will just be "foo" in the `Hash`.
      def get
        client.request(
          method:   :head,
          path:     container_name,
          expected: [204]
        ).each_with_object({}) { |header, h|
          k, v = header[0].downcase, header[1]
          if k =~ /x-container-meta/
            name = k.gsub(/^x-container-meta-/, "")
            h[name] = v
          end 
        }
      end

      docs "http://docs.rackspace.com/files/api/v1/cf-devguide/content/POST_updateacontainermeta_v1__account___container__containerServicesOperations_d1e000.html"
      # @param args [Hash] Key-values to set as metadata on this container.
      def update(args = {})
        headers = MetadataMarshaller.new.call(
          args, 
          "X-Container-Meta-",
          wrong: "X-Remove-Container-Meta-"
        )
        client.request(
          method:   :post,
          path:     container_name,
          headers:  headers,
          expected: [204]
        )
      end

      docs "http://docs.rackspace.com/files/api/v1/cf-devguide/content/POST_deletecontainermeta_v1__account___container__containerServicesOperations_d1e000.html"
      # @params keys [Array] The list of metadata fields to delete from this 
      #   container.
      def delete(*keys)
        headers = MetadataMarshaller.new.call(
          keys.each_with_object({}) { |k, h| h[k] = 1 },
          "X-Remove-Container-Meta-",
          wrong: "X-Container-Meta-"
        )
        client.request(
          method:   :post,
          path:     container_name,
          headers:  headers,
          expected: [204]
        )        
      end<commit_msg>Extract method on metadata extraction from response headers. Previously, this was defined at the wrong level of abstraction.<commit_after>module Redrax
  class Container
    extend Redrax::DocsLinkable
    
    attr_reader :client, :name, :count, :size

    def self.from_hash(client, params = {})
      new(client, *params.values_at("name", "count", "size"))
    end

    def initialize(client, name, count = 0, size = 0)
      @client = client
      @name   = name
      @count  = 0
      @size   = 0
    end

    docs "http://docs.rackspace.com/files/api/v1/cf-devguide/content/GET_listcontainerobjects_v1__account___container__containerServicesOperations_d1e000.html"
    # @return [PaginatedFiles] An `Array` of `File` objects proxying for files
    #   stored on this `Container`.
    def files(options = {})
      resp = client.request(
        method:   :get,
        path:     name,
        params:   options,
        expected: (200..299)
      )
      PaginatedFiles.new(
        resp.map { |f| Redrax::File.from_hash(client, f) },
        self,
        options
      )
    end

    # @return [Redrax::Metadata] The `Metadata` object for this `Container`
    def metadata
      @metadata ||= Metadata.new(client, self)
    end

    # Behavior for manipulating metadata on a container
    class Metadata
      extend Redrax::DocsLinkable

      attr_reader :client, :container_name
      
      def initialize(client, container)
        @client = client
        @container_name = container.name
      end

      docs "http://docs.rackspace.com/files/api/v1/cf-devguide/content/HEAD_retrievecontainermeta_v1__account___container__containerServicesOperations_d1e000.html"
      # @return [Hash] The metadata defined on a container. Keys will be 
      #   stripped of their "X-Container-Meta-" prefix, e.g.
      #   "X-Container-Meta-foo" will just be "foo" in the `Hash`.
      def get
        extract_metadata(
          client.request(
            method:   :head,
            path:     container_name,
            expected: [204]
          )
        )
      end

      docs "http://docs.rackspace.com/files/api/v1/cf-devguide/content/POST_updateacontainermeta_v1__account___container__containerServicesOperations_d1e000.html"
      # @param args [Hash] Key-values to set as metadata on this container.
      def update(args = {})
        headers = MetadataMarshaller.new.call(
          args, 
          "X-Container-Meta-",
          wrong: "X-Remove-Container-Meta-"
        )
        client.request(
          method:   :post,
          path:     container_name,
          headers:  headers,
          expected: [204]
        )
      end

      docs "http://docs.rackspace.com/files/api/v1/cf-devguide/content/POST_deletecontainermeta_v1__account___container__containerServicesOperations_d1e000.html"
      # @params keys [Array] The list of metadata fields to delete from this 
      #   container.
      def delete(*keys)
        headers = MetadataMarshaller.new.call(
          keys.each_with_object({}) { |k, h| h[k] = 1 },
          "X-Remove-Container-Meta-",
          wrong: "X-Container-Meta-"
        )
        client.request(
          method:   :post,
          path:     container_name,
          headers:  headers,
          expected: [204]
        )        
      end

      private

      def extract_metadata(headers)
        headers.each_with_object({}) { |header, h|
          k, v = header[0].downcase, header[1]
          if k =~ /x-container-meta/
            name = k.gsub(/^x-container-meta-/, "")
            h[name] = v
          end 
        }
      end
{'fraction_non_alphanumeric': 0.10539180445722501, 'fraction_numerical': 0.013227893601725377, 'mean_word_length': 2.115091804746977, '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': '886034', 'n_tokens_mistral': 2188, 'n_tokens_neox': 2086, 'n_words': 475}