understanding twitter gem readme
I have few questions about this portion of the readme of the twitter gem
Client = Twitter::REST::Client.new do |config| config.consumer_key = "YOUR_CONSUMER_KEY" config.consumer_secret = "YOUR_CONSUMER_SECRET" config.access_token = "YOUR_ACCESS_TOKEN" config.access_token_secret = "YOUR_ACCESS_SECRET" end
After configuration, requests can be made like so:
client.update("I'm tweeting with @gem!")
1) Where do you put the keys config block? I've seen some guides put it in a separate file, like config/twitter_credentials.rb, omniauth.rb, or devise.rb. If I'm using devise should all 4 keys be in the devise file?
2) when the .update method is called, who tweets the tweet? The app account or the logged in user consuming my site?
3) are the access tokens what allows users to tweet from their account, and not the app's account?
Code showing how to let users tweet from their own account would be appreciated, as they seem to be missing from the readme.
It doesn't really matter where you put the initializer code. But usually you put code relative to a certain gem in an initializer with the same name as the gem. So in this case, I'd put that code inside a file called twitter.rb. However, in this case I don't think you actually need an initializer.
When the update method is called, the user to whom the access_token and access_token_secret belong is the one who tweets.
Yes, the access_token is what allows users to tweet from their account. You can get that token and the corresponding secret from omniauth-twitter's response.
omniauth = env['omniauth.auth'] user.access_token = omniauth.credentials.token user.access_token_secret = omniauth.credentials.secret user.save
Of course, to be able to save the token and the secret you need to add the appropriate fields (access_token and access_token_secret) to your user table.
You can then tweet from a controller, like so:
class SomeController < ApplicationController def some_action client = Twitter::REST::Client.new do |config| config.consumer_key = ENV["TWITTER_CONSUMER_KEY"] config.consumer_secret = ENV["TWITTER_CONSUMER_SECRET"] config.access_token = current_user.access_token config.access_token_secret = current_user.access_token_secret end client.update("I'm tweeting with @gem!") end end