module Connection

Public Class Methods

check_connection() click to toggle source

The verify credentials endpoint returns a 200 status if the request is signed correctly.

# File lib/connection.rb, line 21
def self.check_connection
  address = URI("#{@base_url}/1.1/account/verify_credentials.json")
  # Set up Net::HTTP to use SSL, which is required by Twitter.
  http = Net::HTTP.new address.host, address.port
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  # Build the request and authorize it with OAuth.
  request = Net::HTTP::Get.new address.request_uri
  request.oauth! http, @consumer_key, @access_token
  # Issue the request and return the response.
  http.start
  response = http.request request
  response.code
end
tweets_request(username, date) click to toggle source
# File lib/connection.rb, line 36
def self.tweets_request(username, date)
  path    = "/1.1/statuses/user_timeline.json"
  query   = URI.encode_www_form(
    "screen_name" => username,
    "count" => 300)
  address = URI("#{@base_url}#{path}?#{query}")
  request = Net::HTTP::Get.new address.request_uri
  http = Net::HTTP.new address.host, address.port
  http.use_ssl     = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  request.oauth! http, @consumer_key, @access_token
  http.start
  response = http.request request
  if response.code != "200"
    puts "\nYou probably gave an invalid twitter handle"
    Helper.exit_message
    exit
  end
  tweets_json = File.open("./full_tweets_#{username}.json", "w+")
  tweets_json << response.body
  tweets_json.close
  tweets_hash = JSON.parse(File.read("./full_tweets_#{username}.json"))
  tweets_only = File.open("./tweets_only_#{username}.json", "w+")
  tweets_hash.each do |tweets|
    if Date.parse(tweets["created_at"]) < date
      break
    end
    tweets_only << tweets["text"] + " "
  end
  tweets_only.close
end