uninitialized constant Twitter::Search (NameError) ~ Ruby noob - ruby

I'm trying to run the code below for a Twitter bot I am building (not to spam people if that helps my cause) but I keep getting this error message:
uninitialized constant Twitter::Search (NameError)
I'm a total noob when it comes to programming, especially ruby. Please can someone help!
require 'rubygems'
require 'twitter'
Twitter.configure do |config|
config.consumer_key = "XXXXXXXXXXXXXXXXXXXXXX"
config.consumer_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
config.oauth_token = "XXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
config.oauth_token_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
end
Twitter.user
client = Twitter::Client.new
search = Twitter::Search.new
search.containing("news").containing("journalism").containing("newsrooms").result_type("recent").per_page(3).each do |x|
until x.text.length <= 90 do
y = x.text.chop!
end
y = "via: " + y
client.update("#{y}")
end

I am under the assumption that you are using the latest version of the twitter gem.
According to the documentation (Twitter Documentation), there is no Twitter::Search
Doing searches seems to be done via the Twitter::API.search method (Method Documentation here)

Related

I want to obtain following list in text instead of objects using twitter api in ruby programming language

I'd love to obtain following list in text (screen names) instead of objects using Twitter Api. I am new to Ruby programming language and this's my first attempt using api with ruby especially Twitter api. What I expect is list of screen names instead of objects and I will show you examples bellow:
Results I get currently:
current results
Desired and expected results
I've tried methods such as .full_text and .text appended to the objects and didn't get my desired results.I've searched almost everywhere especially here in Stackoverflow and didn't find my answer yet.
This's my code below:
require 'rubygems'
require 'bundler/setup'
require 'twitter'
require 'json'
require 'yaml'
client = Twitter::REST::Client.new do |config|
config.consumer_key = ""
config.consumer_secret = ""
config.access_token = ""
config.access_token_secret = ""
end
following_list = client.friends('User-exmaple')
begin
for friend in following_list
puts friend
end
rescue Twitter::Error::TooManyRequests => error
# NOTE: Your process could go to sleep for up to 15 minutes but if you
# retry any sooner, it will almost certainly fail with the same exception.
sleep error.rate_limit.reset_in + 1
retry
end
I hope this explains everything, thank you so much.
I solved it by appending screen_name method to friend, example below:
require 'rubygems'
require 'bundler/setup'
require 'twitter'
require 'json'
require 'yaml'
client = Twitter::REST::Client.new do |config|
config.consumer_key = ""
config.consumer_secret = ""
config.access_token = ""
config.access_token_secret = ""
end
following_list = client.friends('User-exmaple')
begin
for friend in following_list
puts friend.screen_name
end
rescue Twitter::Error::TooManyRequests => error
# NOTE: Your process could go to sleep for up to 15 minutes but if you
# retry any sooner, it will almost certainly fail with the same exception.
sleep error.rate_limit.reset_in + 1
retry
end
I hope this explains the solution I found.

Send direct messages with twitter gem

I'm working on a twitter bot with the twitter gem. This bot is liking and following peoples. But now i want to send private messages with this gem and i cant.
This is what Im testing :
def login_twitter_stream
client_streaming =
Twitter::Streaming::Client.new do |config|
config.consumer_key = ENV["TWITTER_CONSUMER_KEY"]
config.consumer_secret = ENV["TWITTER_CONSUMER_SECRET"]
config.access_token = ENV["TWITTER_ACCESS_TOKEN"]
config.access_token_secret = ENV["TWITTER_ACCESS_TOKEN_SECRET"]
end
return client_streaming
end
def login_twitter
client_REST =
Twitter::REST::Client.new do |config|
config.consumer_key = ENV["TWITTER_CONSUMER_KEY"]
config.consumer_secret = ENV["TWITTER_CONSUMER_SECRET"]
config.access_token = ENV["TWITTER_ACCESS_TOKEN"]
config.access_token_secret = ENV["TWITTER_ACCESS_TOKEN_SECRET"]
end
return client_REST
end
def direct_messages
client = login_twitter
client
.search("#helloworld", result_type: "recent")
.take(5)
.each do
client.create_direct_message(
"#{tweet.user}",
"hello this is a test!",
options = {}
)
end
end
direct_messages
And this is the error I have with a simple
client.create_direct_message("#username","hello this is a test!",options={})
event.message_create.target.recipient_id: '#username' is not a valid Long (Twitter::Error::BadRequest)
ans this one:
undefined local variable or method `tweet' for main:Object (NameError)
I hope u have the solution ! Have a great day.
get the id of the twitter you want to send a message with. You can use this link to get an Id.
Then you can execute the command:
client.create_direct_message('<replace with the twitter Id you obtained>',"hello this is a test!",options={})
the tweet.user in your example means, there is a tweets-table(a model class) and the twitter Id is saved in the user-column.

getting a Twitter::Cursor object with the Twitter gem

I'm playing around with the Ruby Twitter gem and wish to use the methods that are available on the Cursor object. For example, using the Twitter::Cursor I'm supposed to be able to get a an array of all friends by doing
client.friends.to_a
or get a most recent follower with
client.friends.first
However, in my attempt below, when tried to do client.friends.first for kanyewest, I got an error which showed that I'm using the Twitter::User object, not the Twitter::Cursor
undefined methodfriends' for #
How can I use the gem to get a cursor object that will allow me to query Kanye's friends.
client.friends.to_a
Note, I read the documentation for creating a new cursor object but I found it a little abstract. I'm not sure if you're supposed to call the constructor directly? If so, please show me how I'd do that
- (Twitter::Cursor) initialize(attrs, key, klass, request)
My Failing code
#!/usr/bin/env ruby
require 'rubygems'
require 'twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = "8nwa....."
config.consumer_secret = "Wj20r....."
config.access_token = "363......"
config.access_token_secret = "7eydU2n....."
end
kanyewest = client.user("kanyewest")
puts kanyewest.friends.first
client.friends("kanyewest").first should work. friends is a method on client not on Twitter::User

Ruby's Twitter REST api not working

I am trying to use the ruby interface for twitter api.The Streaming Api is working but Rest spi is not.
Code :
require 'rubygems'
require 'twitter'
client = Twitter::REST::Client.new do |config|
# you must set up an application using Twitter's developer site, and set these values:
# (See the Configuration example)
config.consumer_key = "xxx"
config.consumer_secret = "xxx"
config.access_token = "xxx-xxx"
config.access_token_secret = "xxx"
end
puts client.user('xyz')
I am always getting the following error -
Faraday::Builder is now Faraday::RackBuilder.
/home/atul/.rvm/gems/ruby-2.1.0/gems/twitter-5.5.1/lib/twitter/rest/client.rb:143:in `rescue in request': execution expired (Twitter::Error)
from /home/atul/.rvm/gems/ruby-2.1.0/gems/twitter-5.5.1/lib/twitter/rest/client.rb:131:in `request'
from /home/atul/.rvm/gems/ruby-2.1.0/gems/twitter-5.5.1/lib/twitter/rest/client.rb:97:in `get'
from /home/atul/.rvm/gems/ruby-2.1.0/gems/twitter-5.5.1/lib/twitter/rest/api/utils.rb:118:in `object_from_response'
from /home/atul/.rvm/gems/ruby-2.1.0/gems/twitter-5.5.1/lib/twitter/rest/api/users.rb:257:in `user'
from actresses.rb:17:in `<main>'
I am trying to increase the time out because of this solution but am able to figure out how to do that.
I don't understand what am I doing wrong as the same code is working for Streaming api after making the appropriate changes.
I'm not entirely clear on what kind of an issue you're having, but to set the timeout you may want to try
custom_options = Twitter::Default::CONNECTION_OPTIONS.merge(
request: { open_timeout: 5, timeout: 20 } ) # default timeout is 10
client = Twitter::REST::Client.new do |config|
# you must set up an application using Twitter's developer site, and set these values:
# (See the Configuration example)
config.consumer_key = "xxx"
config.consumer_secret = "xxx"
config.access_token = "xxx-xxx"
config.access_token_secret = "xxx"
config.connection_options = custom_options
end
I'm not sure if that will resolve that error, though.

Get list timeline by using Twitter gem

I want to get list timeline by using Twitter gem.
How can I call GET lists/statuses REST API from Twitter gem?
https://dev.twitter.com/docs/api/1.1/get/lists/statuses
I've searched through documentation but couldn't found proper method.
http://rdoc.info/gems/twitter
Problem solved
I overlooked the method list_timeline. It serves as lists/statuses.
You can use 'twitter' gem.
Create an app on Twitter. You need to give to it read & write access just for sake of your own experiments.
Use this sample code (with your credentials you've got from Twitter). Please note that I'm using some public list URI (list = URI.parse('https://twitter.com/sferik/presidents')
p client.list_timeline(list))
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
list = URI.parse('https://twitter.com/sferik/presidents')
client.list_timeline(list)
for more info please visit Twitter gem docs.
I suggest you start by reading the gem documentation. You'll also have to register your application with twitter to get your oauth parameters.
https://github.com/sferik/twitter
To get the timeline from a list using 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
# For each list of the user
client.lists.each do |list|
# Get the timeline with the list ID
client.list_timeline(list.id).each do |tweet|
puts tweet.text
end
end

Resources