Module: Connection

Included in:
Andeltsa::TwitterRequest
Defined in:
lib/connection.rb

Class Method Summary collapse

Class Method Details

.check_connectionObject

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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