Module: GetUserTimeline

Included in:
TweetTop::User
Defined in:
lib/get_user_timeline.rb

Instance Method Summary collapse

Instance Method Details

#get_user_timeline(user_id, results = 5, format: "json") ⇒ Object

results: minimum 5, maximum 200



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/get_user_timeline.rb', line 5

def get_user_timeline(user_id, results = 5, format: "json") #results: minimum 5, maximum 200
    
    url = "https://api.twitter.com/2/users/#{user_id}/tweets"
    
    query_params = {
        "max_results" => results,
        "tweet.fields" => "conversation_id,created_at,public_metrics,id,referenced_tweets",
        "user.fields" => "description,username,url",
        "media.fields" => "url" 
    }

    options = {
        method: 'get',
        headers: {
          "Authorization" => "Bearer #{@bearer_token}"
          },
        params: query_params
    }

    request = Typhoeus::Request.new(url, options)
    response = request.run

    if format == "ruby"
        return JSON.parse(response.body.to_s) 
    elsif format == "code"
        return response.code 
    else
        return JSON.pretty_generate(JSON.parse(response.body))
    end
end