0

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.

ahnbizcad
  • 10,491
  • 9
  • 59
  • 85

2 Answers2

2

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.

danielM
  • 2,462
  • 2
  • 20
  • 21
  • what's the difference between a variable with `$` and without? – ahnbizcad Sep 19 '14 at 03:13
  • Will it tweet as the owner of the app, or will it prompt anyone anyone to log into their twitter account and tweet it as their own? – ahnbizcad Sep 19 '14 at 03:15
  • Without the $ would be a regular local variable. We need it available in a scope that can be seen by your controller. As for the tweet, it would go out as the owner. Specifically, the account that generated the keys. – danielM Sep 19 '14 at 03:18
  • so you're referring to the second set of keys, the owner of the app. The first set of keys is just for the app to connect to twitter and view stuff. What I really need is for users that are logged into their twitter accounts to click a button on my app, and tweet content from my app via their twitter account. Just like fmylife.com does – ahnbizcad Sep 19 '14 at 03:23
  • The twitter gem page says global variables are not threadsafe. – ahnbizcad Sep 19 '14 at 03:25
  • so it's the "your oauth token" that allows me to tweet as my twitter account linked to the app. Without that, i would not be able to use the `.update` function successfully? – ahnbizcad Sep 19 '14 at 03:26
  • I see. Based on those requirements, you need to configure a Twitter client for the user each time he or she tweets. Are you storing the user's oath tokens? – danielM Sep 19 '14 at 03:28
  • This tutorial might help you: http://richonrails.com/articles/sending-a-tweet-to-twitter – danielM Sep 19 '14 at 03:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61535/discussion-between-gwho-and-danielm). – ahnbizcad Sep 19 '14 at 03:29
0

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

Community
  • 1
  • 1
Zack Weiner
  • 664
  • 4
  • 14