Instantiate multiple accounts

25 views
Skip to first unread message

Max Rose-Collins

unread,
Oct 24, 2013, 8:41:46 AM10/24/13
to twitter-...@googlegroups.com
How can I instantiate multiple accounts within a loop?

twitter_accounts.each do |twitter_account|
        username
= twitter_account['username']
        token
= twitter_account['oauth_token']
        secret
= twitter_account['oauth_token_secret']


        username
= Twitter::Client.new(
       
:oauth_token => token,
       
:oauth_token_secret => secret
       
)
end


This is what I have tried... doesn't seem to work though.
Where am I going wrong?

Thanks in advance!

Steve Agalloco

unread,
Oct 24, 2013, 9:56:26 AM10/24/13
to twitter-...@googlegroups.com
Looks like you are overwriting the client via the username variable during each loop iteration. Maybe try something like this?

twitter_clients = twitter_accounts.inject({}) do |clients, account|
  clients[account['username']] = Twitter::Client.new(
    :oauth_token        => account['oauth_token'], 
    :oauth_token_secret => account['oauth_token_secret']
  )
  clients
end

Then you'll have a hash of clients with the username as the key.

Max Rose-Collins

unread,
Oct 24, 2013, 12:46:42 PM10/24/13
to twitter-...@googlegroups.com
In the end I used this.

accounts = {}

twitter_accounts
.each do |twitter_account|
    username
= twitter_account['username']
    token
= twitter_account['oauth_token']
    secret
= twitter_account['oauth_token_secret']



    accounts
[username] = Twitter::Client.new(

       
:oauth_token => token,
       
:oauth_token_secret => secret
   
)
end

Is using inject a better way of going about it?
It looks a lot more concise!

Would I use it like this?
Thread.new{twitter_clients['username'].update("Tweet tweet")}


Reply all
Reply to author
Forward
0 new messages