dimanche 10 avril 2016

Ruby, algorithm to reply to only new texts, save id to database

It is a bot to reply to a text (like: to comment users comments), only once per a text, so I want to keep its id in the database. BUT it adds the ids many times, first one * texts.length, second * texts.length-1 etc. Any ideas how to fix it? :(

  def check_texts_since(last_text_id)
    options = {
      since_id: @last_text_id
    }
    CLIENT.texts_timeline(options)
  end

  def reply_to_all_texts
    texts = Text.all
    @last_text_id = texts.any? ? texts.last.reply_id : 1 
    texts = check_texts_since(@last_text_id)
      for reply in texts do
        text = Text.new(reply_id: reply.id)
        reply = generate_reply_to(reply)
        if CLIENT.update(reply)
          if text.save
            @last_text_id = text.reply_id
          end
        end
      end
      puts @last_text_id
  end

Aucun commentaire:

Enregistrer un commentaire