10

I am getting an error with a Ruby script using the 'twitter' gem. The part of my script that is producing the error is

require 'twitter'
require 'net/http'
require 'json'

#### Get your twitter keys & secrets:
#### https://dev.twitter.com/docs/auth/tokens-devtwittercom
Twitter.configure do |config|
  config.consumer_key = 'xxxxxxx'
  config.consumer_secret = 'xxxxxxx'
  config.oauth_token = 'xxxxxx'
  config.oauth_token_secret = 'xxxxxxx'
end

The error says undefined method 'configure' for Twitter:Module (NoMethodError) However the 'twitter' and 'json' gems are both in my gemfile so I'm not sure why this method would be undefined.

user1893354
  • 5,778
  • 12
  • 46
  • 83

1 Answers1

17

You are doing it the "old" way. Starting in Version 5, global configuration is not longer available. So, basically you need to pass the config parameters when you initialize a client.

For example:

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

And then just use that client to do queries, such as:

client.sample do |tweet|
  puts tweet.text
end

For more information just refer to Sferik's Twitter Gem

Nobita
  • 23,519
  • 11
  • 58
  • 87
  • 3
    If I wanted to get a list of the most recent 4 tweets, how would I do this? I can't access client in any of the controllers as 'client' gets me a NameError -- this documentation is frustrating me. They don't display any controller code... – Peege151 Sep 29 '14 at 23:43