In my rails controller, I have an array called "usernames" which just has 10 twitter handles without the "@" symbol. I am storing the latest tweet of these users in an array called "tweets" with the following assignment:
tweets = usernames.map{|username| $client_local.user_timeline(username,count: 1)[0].text}
where $client_local is:
$client_local = Twitter::REST::Client.new do |config|
config.consumer_key = 'xxxx'
config.consumer_secret = 'xxxx'
config.access_token = 'xxxx'
config.access_token_secret = 'xxxx'
end
I used the ruby benchmark module and found that the "tweets" assignment is taking 2.5 seconds to run. I am guessing it is taking so long because the user_timeline function is being called 10 times, thus making 10 separate connections.
Looking for a solution, I went to sferik's github for configuration options, and it appears that the "bearer tokan" is what I need according to this explanation on the page:
client.user("sferik")
Note: The first time this method is called, it will make two API requests. First, it will fetch an access token to perform the request. Then, it will fetch the requested user. The access token will be cached for subsequent requests made with the same client but you may wish to manually fetch the access token once and use it when initializing clients to avoid making two requests every time.
client.bearer_token
This token never expires and will not change unless it is invalidated. Once you've obtained a bearer token, you can use it to initialize clients to avoid making an extra request.
- I haven't been able to find my app's bearer token or figure out how to obtain one
- I'm not 100% if this is the solution to my problem, or if there is something else I should be looking for.
Ideas?
Searching online, I see people suggesting to make a list of these users on my Twitter account and then making a request to "lists/statuses". This is not an option because the 10 usernames in the "usernames" array are different each time the array is set.