<commit_before>require 'yaml'

require 'iron_core'

module IronMQ

  class Client < IronCore::Client
    AWS_US_EAST_HOST = 'mq-aws-us-east-1.iron.io'

    attr_accessor :queue_name, :logger

    def initialize(options={})
      default_options = {
          :scheme => 'https',
          :host => IronMQ::Client::AWS_US_EAST_HOST,
          :port => 443,
          :api_version => 3,
          :user_agent => 'iron_mq_ruby-' + IronMQ::VERSION + ' (iron_core_ruby-' + IronCore.version + ')'
      }

      super('iron', 'mq', options, default_options, [:project_id, :token, :api_version])

      IronCore::Logger.error 'IronMQ', "Token is not set", IronCore::Error if @token.nil?

      check_id(@project_id, 'project_id')

      @logger = Logger.new(STDOUT)
      @logger.level = Logger::INFO
    end

    def headers
      super.merge({'Authorization' => "OAuth #{@token}"})
    end

    def base_url
      @base_url = "#{super}#{@api_version}/projects/#{@project_id}/queues"
    end

    def queues_list(options = {})
      is_raw = [options.delete(:raw),
                options.delete('raw')].compact.first
      response = parse_response(get('', options)) # GET base_url
      # p response
      # returns list of evaluated queues
      if is_raw
        response.map{ |q_info| ResponseBase.new(q_info) }
      else
        response["queues"].map{ |q_info| Queue.new(self, q_info["name"]) }
      end
    end

    alias_method :list, :queues_list
    alias_method :all, :queues_list

    def queues_get(name)
      IronMQ::Queue.new(self, name)
    end

    alias_method :queue, :queues_get

    # Backward compatibility for
    #   client.queues.get(:name => "my_queue")
    #   client.queues.get("name" => "my_queue")
    def get(*args)
      if args.size == 1 && args[0].is_a?(Hash)
        queue_name = (args[0][:name] || args[0]["name"]).to_s
        queue_name.empty? ? super : queues_get(queue_name)
      else
        super
      end
    end

    # Backward compatibility, adds possibility to call
    #   client.queues.all
    #   client.queues.list
    #   client.queues.queue(name)
    def queues
      self
    end

    def create_queue(queue_name, options = {})
      body = prepare_options(options)
      response = self.put("/#{CGI::escape(queue_name).gsub('+', '%20')}", {queue: body})
      queue_hash = JSON.parse(response.body.to_s)
      ResponseBase.new(queue_hash['queue'])
    end

    def update_queue(queue_name, options = {})
      response = self.patch("/#{CGI::escape(queue_name).gsub('+', '%20')}", {queue: prepare_options(options)})
      queue_hash = JSON.parse(response.body.to_s)
      ResponseBase.new(queue_hash['queue'])
    end

    private

    def prepare_options(options)
      push = {}
      if options[:subscribers]
        subscribers = options.delete(:subscribers).map{|val| {url: val}}
        push[:subscribers] = subscribers
      end
      push[:retries] = options[:retries] unless options[:retries].nil?
      push[:retries_delay] = options[:retries_delay] unless options[:retries_delay].nil?
      push[:error_queue] = options[:error_queue] unless options[:error_queue].nil?
      options[:push] = push
      options
    end
  end

end<commit_msg>fix create queue<commit_after>require 'yaml'

require 'iron_core'

module IronMQ

  class Client < IronCore::Client
    AWS_US_EAST_HOST = 'mq-aws-us-east-1.iron.io'

    attr_accessor :queue_name, :logger

    def initialize(options={})
      default_options = {
          :scheme => 'https',
          :host => IronMQ::Client::AWS_US_EAST_HOST,
          :port => 443,
          :api_version => 3,
          :user_agent => 'iron_mq_ruby-' + IronMQ::VERSION + ' (iron_core_ruby-' + IronCore.version + ')'
      }

      super('iron', 'mq', options, default_options, [:project_id, :token, :api_version])

      IronCore::Logger.error 'IronMQ', "Token is not set", IronCore::Error if @token.nil?

      check_id(@project_id, 'project_id')

      @logger = Logger.new(STDOUT)
      @logger.level = Logger::INFO
    end

    def headers
      super.merge({'Authorization' => "OAuth #{@token}"})
    end

    def base_url
      @base_url = "#{super}#{@api_version}/projects/#{@project_id}/queues"
    end

    def queues_list(options = {})
      is_raw = [options.delete(:raw),
                options.delete('raw')].compact.first
      response = parse_response(get('', options)) # GET base_url
      # p response
      # returns list of evaluated queues
      if is_raw
        response.map{ |q_info| ResponseBase.new(q_info) }
      else
        response["queues"].map{ |q_info| Queue.new(self, q_info["name"]) }
      end
    end

    alias_method :list, :queues_list
    alias_method :all, :queues_list

    def queues_get(name)
      IronMQ::Queue.new(self, name)
    end

    alias_method :queue, :queues_get

    # Backward compatibility for
    #   client.queues.get(:name => "my_queue")
    #   client.queues.get("name" => "my_queue")
    def get(*args)
      if args.size == 1 && args[0].is_a?(Hash)
        queue_name = (args[0][:name] || args[0]["name"]).to_s
        queue_name.empty? ? super : queues_get(queue_name)
      else
        super
      end
    end

    # Backward compatibility, adds possibility to call
    #   client.queues.all
    #   client.queues.list
    #   client.queues.queue(name)
    def queues
      self
    end

    def create_queue(queue_name, options = {})
      body = prepare_options(options)
      response = self.put("/#{CGI::escape(queue_name).gsub('+', '%20')}", {queue: body})
      queue_hash = JSON.parse(response.body.to_s)
      ResponseBase.new(queue_hash['queue'])
    end

    def update_queue(queue_name, options = {})
      response = self.patch("/#{CGI::escape(queue_name).gsub('+', '%20')}", {queue: prepare_options(options)})
      queue_hash = JSON.parse(response.body.to_s)
      ResponseBase.new(queue_hash['queue'])
    end

    private

    def prepare_options(options)
      if options[:subscribers]
        push = {}
        subscribers = options.delete(:subscribers).map{|val| {url: val}}
        push[:subscribers] = subscribers
        options[:push] = push
      end
      push[:retries] = options[:retries] unless options[:retries].nil?
      push[:retries_delay] = options[:retries_delay] unless options[:retries_delay].nil?
      push[:error_queue] = options[:error_queue] unless options[:error_queue].nil?
      options
    end
  end

end
{'fraction_non_alphanumeric': 0.14372564302416213, 'fraction_numerical': 0.004053000779423227, 'mean_word_length': 2.872057936028968, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '1510588', 'n_tokens_mistral': 2165, 'n_tokens_neox': 2098, 'n_words': 401}