Getting tweets and adding them to array.uniq


#1

Hey, i want to write a program that pulls tweets and saves them in a variable, i guess an array would make sense. I only need the urls of the tweets cause i want to send them to a discourse platform which then embed them automatically.

I have an method which pulls the tweets from twitter:

require 'bundler/setup'
require 'net/http'
require 'uri'
require 'twitter'
module GetTweet
 class Error < StandardError;
 end

 def self.catch_tweets
   client = Twitter::REST::Client.new do |config|
     config.consumer_key = "LULULULULULU"
     config.consumer_secret = "LULULULULU"
     config.access_token = "LULULULUL"
     config.access_token_secret = "LULULULUULU"
   end
   # getting all tweets in a hashmap with "mention" without retweet in option timeframe
   @tweets = client.search("#mysearchrequest -rt", since: "2018-8-27", until: "2018-11-7")
   # put the hashmap into an sorted array
   @sorted_tweets = @tweets.sort_by {|tweet| tweet.created_at}
   # give me every uniq tweet
   @sorted_tweets.uniq.each do |tweet|
     @tweet = tweet
     puts @tweet.full_text
   end
 end
 catch_tweets
end

and a method to send tweets to my discourse server

require 'net/http'
require 'uri'
require_relative 'get_tweet'

INSTANCE_URL = 'https://LULULULULULU.de/posts.json'
API_USERNAME = 'LULULULULULU'
API_KEY = 'LULULULULULU'


def self.send_request

  url = URI.parse(INSTANCE_URL)
  request = Net::HTTP::Post.new(url.path)
  request.set_form_data({'api_username' => API_USERNAME, 'api_key' => API_KEY, 'title' => "Neuer Post", 'topic_id' => 8, 'raw' => "#{tweets}"})
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  response = http.request(request)

  puts response.code

end

send_request

I want to make it possible that i have one main file which uses both methods to pull the tweets and then send them without pulling everytime the tweets which are already displayed but only the new ones.
I should probably go with an array which only adds the new tweets but i dont know how to achieve that.

I think about something like tweets = Array.new and then tweets.push(catch_tweets) which isn´t possible probably … so if anyone has a good idea and can help out


#2

If you need a array with sorted element, Hashes a more logical choice.