<commit_before>module VagrantCloud

  class Account

    attr_accessor :username
    attr_accessor :access_token

    # @param [String] username
    # @param [String] access_token
    def initialize(username, access_token)
      @username = username
      @access_token = access_token
    end

    # @param [String] name
    # @param [Hash]
    # @return [Box]
    def get_box(name, data = nil)
      Box.new(self, name, data)
    end

    # @param [String] name
    # @param [String] description
    # @param [Hash] params
    # @return [Box]
    def create_box(name, description = nil, **params)
      fail "Parameters must be a hash" unless params.is_a?(Hash)
      params[:name] = name

      # If description is provided, it will override the params entry.
      # This is provided for backwards compatibility.
      unless description.nil?
        params[:description] = description
        params[:short_description] = description
      end

      # Default boxes to public can be overridden by providing :is_private
      params[:is_private] = false unless defined? params[:is_private]

      data = request('post', '/boxes', {:box => params})
      get_box(name, data)
    end

    # @param [String] name
    # @param [String] description
    # @param [Hash] params
    # @return [Box]
    def ensure_box(name, description = nil, **params)
      fail "Parameters must be a hash" unless params.is_a?(Hash)

      # If description is provided, it will override the params entry.
      # This is provided for backwards compatibility.
      unless description.nil?
        params[:description] = description
        params[:short_description] = description
      end

      begin
        box = get_box(name)
        box.data
      rescue RestClient::ResourceNotFound => e
        box = create_box(name, params)
        # If we've just created the box, we're done.
        return box
      end

      # If params is empty, there's nothing to update.
      return box if params.empty?

      # Select elements from params that don't match what we have in the box
      # data. These are changed parameters and should be updated.
      update_params = params.select { |k,v|
        box.data[box.param_name(k)] != v
      }

      # Update the box with any params that had changed.
      box.update(update_params) unless update_params.empty?

      box
    end

    # @param [String] method
    # @param [String] path
    # @param [Hash] params
    # @return [Hash]
    def request(method, path, params = {})
      params[:access_token] = access_token
      result = RestClient::Request.execute(
        :method => method,
        :url => url_base + path,
        :payload => params,
        :ssl_version => 'TLSv1'
      )
      result = JSON.parse(result)
      errors = result['errors']
      raise(RuntimeError, "Vagrant Cloud returned error: #{errors}") if errors
      result
    end

    private

    # @return [String]
    def url_base
      'https://vagrantcloud.com/api/v1'
    end

  end
end<commit_msg>Work around lack of **args in Ruby 1.9.<commit_after>module VagrantCloud

  class Account

    attr_accessor :username
    attr_accessor :access_token

    # @param [String] username
    # @param [String] access_token
    def initialize(username, access_token)
      @username = username
      @access_token = access_token
    end

    # @param [String] name
    # @param [Hash]
    # @return [Box]
    def get_box(name, data = nil)
      Box.new(self, name, data)
    end

    # @param [String] name
    # @param [Array] params
    # @return [Box]
    def create_box(name, *args)
      params = box_params(*args)
      params[:name] = name

      data = request('post', '/boxes', {:box => params})
      get_box(name, data)
    end

    # @param [String] name
    # @param [String] description
    # @param [Hash] params
    # @return [Box]
    def ensure_box(name, *args)
      params = box_params(*args)

      begin
        box = get_box(name)
        box.data
      rescue RestClient::ResourceNotFound => e
        box = create_box(name, params)
        # If we've just created the box, we're done.
        return box
      end

      # If params is empty, there's nothing to update.
      return box if params.empty?

      # Select elements from params that don't match what we have in the box
      # data. These are changed parameters and should be updated.
      update_params = params.select { |k,v|
        box.data[box.param_name(k)] != v
      }

      # Update the box with any params that had changed.
      box.update(update_params) unless update_params.empty?

      box
    end

    # @param [String] method
    # @param [String] path
    # @param [Hash] params
    # @return [Hash]
    def request(method, path, params = {})
      params[:access_token] = access_token
      result = RestClient::Request.execute(
        :method => method,
        :url => url_base + path,
        :payload => params,
        :ssl_version => 'TLSv1'
      )
      result = JSON.parse(result)
      errors = result['errors']
      raise(RuntimeError, "Vagrant Cloud returned error: #{errors}") if errors
      result
    end

    private

    # @return [String]
    def url_base
      'https://vagrantcloud.com/api/v1'
    end

    # @param [Array] args
    # @return [Hash]
    def box_params(*args)
      # This dance is to simulate what we could have accomplished with **args
      # in Ruby 2.0+
      # Find and remove the first hash we find in *args. Set params to an
      # empty hash if we weren't passed one.
      # This could easily be changed to merge all hashes in *args if
      # necessary.
      params = args.select { |v| v.is_a?(Hash) }.first
      if params.is_a?(Hash)
        args.delete_if { |v| v == params }
      else
        params = {}
      end

      # Description should be the first item that's left in *args, if nothing
      # is left (we were only passed a hash), this will return nil.
      description = args.first

      # If description is provided, it will override the params entry.
      # This is provided for backwards compatibility.
      unless description.nil?
        params[:description] = description
        params[:short_description] = description
      end

      # Default boxes to public can be overridden by providing :is_private
      params[:is_private] = false unless defined? params[:is_private]

      params
    end

  end
end
{'fraction_non_alphanumeric': 0.0971267957526546, 'fraction_numerical': 0.0012492192379762648, 'mean_word_length': 2.394276629570747, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, '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': '215365', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1822, 'n_words': 624}