Tweet with twitter gem

The documentation says

config = Twitter.configure do |config|
  config.consumer_key = 'YOUR_CONSUMER_KEY'
  config.consumer_secret = 'YOUR_CONSUMER_SECRET'
  config.oauth_token = 'YOUR_OAUTH_TOKEN'
  config.oauth_token_secret = 'YOUR_OAUTH_TOKEN_SECRET'
end

and

client.update('Hello, from Twitter Gem!')

But where do I put them? (also, why do documentation assume everyone knows where to put things?)


What is one valid design for doing this

For example, I have a twitter button on a post. When the twitter button is clicked, I want it to tweet that post (nevermind about handling shortening the post).

Should i put the tweeting client.update('Hello, from Twitter Gem!') in an action of a newly created twitter controller?

Or make it a button that calls a javascript file with that tweeting code in it?

I just need one valid way of getting the tweeting button to be functional.

Answers


I have used the following setup:

config/initializers/twitter.rb

$twitter = Twitter.configure do |config|
  config.consumer_key = 'YOUR_CONSUMER_KEY'
  config.consumer_secret = 'YOUR_CONSUMER_SECRET'
  config.oauth_token = 'YOUR_OAUTH_TOKEN'
  config.oauth_token_secret = 'YOUR_OAUTH_TOKEN_SECRET'
end

Then you can reference the $twitter variable in a controller of your choosing. Your view can post a message to the controller, you can do any preprocessing (if needed), and then call $twitter.update(message)

I like this setup because it allows me to access my configured twitter client from wherever I need it.


Although the question was about file paths, and @danielM is correct, the code is outdated. This was the config setup method youll need to call: Error with Ruby Twitter API


Need Your Help

irb history not working, among other issues (jruby / windows)

ruby-on-rails windows console jruby irb

I just set up jruby on my windows box. Got some problems with both irb and rails console:

Segfault when imbueing stringstream with custom locale

c++ memory-management locale stringstream

Based on the answers here to the question of how to format numbers with a comma, I am using the following code: