Module: GetUser

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

Instance Method Summary collapse

Instance Method Details

#get_user(usernames, format: "json") ⇒ Object



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
35
36
37
# File 'lib/get_user.rb', line 7

def get_user(usernames, format: "json")

    url = "https://api.twitter.com/2/users/by"

    query_params = {
     "usernames": usernames,
        "user.fields": "name,description,created_at"
    }

    options = {
     :method => :get,
     params: query_params
 }
 
    access_token = @credentials

    oauth_params = {:consumer => @client, :token => access_token}
    
    request = Typhoeus::Request.new(url, options)
 oauth_helper = OAuth::Client::Helper.new(request, oauth_params.merge(:request_uri => url))
 request.options[:headers].merge!({"Authorization" => oauth_helper.header})
 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

#get_user!(usernames, format: "json") ⇒ Object



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/get_user.rb', line 39

def get_user!(usernames, format: "json")
    
    url = "https://api.twitter.com/2/users/by"
    
    query_params = {
     "usernames": usernames,
        "user.fields": "name,description,created_at"
    }
    
    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

#get_user_by_id(user_ids, format: "json") ⇒ Object

“user_ids” must be a string. If more than one ID’s are given, they must be comma separated



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/get_user.rb', line 68

def get_user_by_id(user_ids, format: "json")#"user_ids" must be a string. If more than one ID's are given, they must be comma separated
    
    url = "https://api.twitter.com/2/users"
    
    query_params = {
     "ids": user_ids,
        "user.fields": "name,description,created_at"
    }
    
    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