<commit_before>require 'cgi'

module IronMQ
  class Messages

    attr_reader :client, :queue

    def initialize(client, queue=nil)
      @client = client
      @queue = queue
    end


    def path(options={})
      options[:queue_name] ||= ((@queue ? @queue.name : nil) || @client.queue_name)
      options[:project_id] = @client.project_id
      Messages.path(options)
    end

    def self.path(options)
      path = "#{Queues.path(options)}/messages"
      if options[:msg_id]
        path << "/#{options[:msg_id]}"
      end
      path
    end

    # options:
    #  :queue_name => can specify an alternative queue name
    #  :timeout => amount of time before message goes back on the queue
    def get(options={})
      if options.is_a?(String)
        # assume it's an id
        puts 'get_by_id'
        return Message.new(self, {'id'=>options}, options)
        #r = @client.get(path(:msg_id=>options))
        #p r
        #return r
      end
      res = @client.parse_response(@client.get(path(options), options))
      ret = []
      res["messages"].each do |m|
        ret << Message.new(self, m, options)
      end
      if options[:n] || options['n']
        return ret
      else
        if ret.size > 0
          return ret[0]
        else
          return nil
        end
      end
    end

    # options:
    #  :queue_name => can specify an alternative queue name
    #  :delay => time to wait before message will be available on the queue
    #  :timeout => The time in seconds to wait after message is taken off the queue, before it is put back on. Delete before :timeout to ensure it does not go back on the queue.
    #  :expires_in => After this time, message will be automatically removed from the queue.
    def post(payload, options={})
      batch = false
      if payload.is_a?(Array)
        batch = true
        msgs = payload
      else
        options[:body] = payload
        msgs = []
        msgs << options
      end
      to_send = {}
      to_send[:subscribers] = msgs
      res = @client.parse_response(@client.post(path(options), to_send))
      if batch
        return res
      else
        return ResponseBase.new({"id"=>res["ids"][0], "msg"=>res["msg"]})
      end
    end

    def delete(message_id, options={})
      path2 = "#{self.path(options)}/#{message_id}"
      res = @client.parse_response(@client.delete(path2))
      return ResponseBase.new(res)
    end

    def release(message_id, options={})
      path2 = "#{self.path(options)}/#{message_id}/release"
      res = @client.parse_response(@client.post(path2, options))
      return ResponseBase.new(res)
    end

  end

  class Message < ResponseBase

    def initialize(messages, res, options={})
      super(res)<commit_msg>Fixed refactoring from yesterday.<commit_after>require 'cgi'

module IronMQ
  class Messages

    attr_reader :client, :queue

    def initialize(client, queue=nil)
      @client = client
      @queue = queue
    end


    def path(options={})
      options[:queue_name] ||= ((@queue ? @queue.name : nil) || @client.queue_name)
      options[:project_id] = @client.project_id
      Messages.path(options)
    end

    def self.path(options)
      path = "#{Queues.path(options)}/messages"
      if options[:msg_id]
        path << "/#{options[:msg_id]}"
      end
      path
    end

    # options:
    #  :queue_name => can specify an alternative queue name
    #  :timeout => amount of time before message goes back on the queue
    def get(options={})
      if options.is_a?(String)
        # assume it's an id
        puts 'get_by_id'
        return Message.new(self, {'id'=>options}, options)
        #r = @client.get(path(:msg_id=>options))
        #p r
        #return r
      end
      res = @client.parse_response(@client.get(path(options), options))
      ret = []
      res["messages"].each do |m|
        ret << Message.new(self, m, options)
      end
      if options[:n] || options['n']
        return ret
      else
        if ret.size > 0
          return ret[0]
        else
          return nil
        end
      end
    end

    # options:
    #  :queue_name => can specify an alternative queue name
    #  :delay => time to wait before message will be available on the queue
    #  :timeout => The time in seconds to wait after message is taken off the queue, before it is put back on. Delete before :timeout to ensure it does not go back on the queue.
    #  :expires_in => After this time, message will be automatically removed from the queue.
    def post(payload, options={})
      batch = false
      if payload.is_a?(Array)
        batch = true
        msgs = payload
      else
        options[:body] = payload
        msgs = []
        msgs << options
      end
      to_send = {}
      to_send[:messages] = msgs
      res = @client.parse_response(@client.post(path(options), to_send))
      if batch
        return res
      else
        return ResponseBase.new({"id"=>res["ids"][0], "msg"=>res["msg"]})
      end
    end

    def delete(message_id, options={})
      path2 = "#{self.path(options)}/#{message_id}"
      res = @client.parse_response(@client.delete(path2))
      return ResponseBase.new(res)
    end

    def release(message_id, options={})
      path2 = "#{self.path(options)}/#{message_id}/release"
      res = @client.parse_response(@client.post(path2, options))
      return ResponseBase.new(res)
    end

  end

  class Message < ResponseBase

    def initialize(messages, res, options={})
      super(res)
{'fraction_non_alphanumeric': 0.11960677225559803, 'fraction_numerical': 0.0025486983433460767, 'mean_word_length': 2.3398176291793313, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, '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': '3359085', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1636, 'n_words': 476}