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
Related
So, I've been following this guide: https://docs.github.com/pt/developers/webhooks-and-events/webhooks/creating-webhooks
This is my code:
require 'sinatra'
require 'json'
require 'openssl'
post '/payload' do
request.body.rewind
header_token = request.env["zzzzzzzzzzzzzzzzzzzz"]
payload_body = request.body.read
verify_signature(payload_body)
push = JSON.parse(payload_body)
"I got some JSON: #{push.inspect}"
end
def verify_signature(payload_body)
signature = 'sha256=' + (OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'),
ENV['jksdaskçjdaskçdhaskdhakdhaskdhaskdhaskdaskdasdkh'],
payload_body))
return halt 500, "Signatures didn't match!" unless Rack::Utils.secure_compare(signature, request.env['HTTP_X_HUB_SIGNATURE_256'])
end
When I load http://localhost:4567 i get :
Sinatra doesn’t know this ditty.
If I change Post to Get (trying to see if it is working) :
no implicit conversion of nil into String file: payload.rb location:
hexdigest line: 15
I'm pretty new to this and I'm attempting to create a prestashop module for a payment gateway, but this webhook thing is messing with me.
Im I doing something wrong? Is there a easy way? Is it everything correct?
Thanks
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.
i'm still fairly new to server side scripts and try myself a little bit on ruby to write me little helpers and to learn some new things.
I currently try to write a small ruby app which sends a json file of all images within a specific folder to my page where i can use those to handle them further in js.
I read quite a few introductions to ruby and rails and got a recommendation to look into rack as a lightweight communicator between server and app.
While the ruby part works fine, i have difficulties to understand how to send out the generated JSON as a reaction to a future ajax call (e.g.). Hope someone can give me a few hints or sources to look into for further understanding. Thanks!
require 'json'
class listImages
def call(env)
imageDir = Dir.chdir("./img");
files = Dir.glob("img*")
n = 0
tempHash = {}
files.each do |i|
tempHash["img#{n}"] = i
n += 1
end
File.open("temp.json","w") do |f|
f.write(tempHash.to_json)
end
[200,{"Content-Type" => "application/javascript"}, ["temp.json"]]
end
puts "All done!"
end
run listImages.new
if $0 == __FILE__
require 'rack'
Rack::Handler::WEBrick.run MyApp.new
end
You don't have to save the JSON to a file before you can send it. Just send it directly:
[200, {"Content-Type" => "application/json"}, [tempHash.to_json]]
With your current code, you are only sending the String "temp.json".
That said, the rest of your code looks a little bit messy/not conform Ruby coding standards:
Start your classnames with an uppercase: class ListImages, not class listImages.
Use underscores, not camelcase for variable names: image_dir, not imageDir.
The puts "All done!" statement is outside the method definition and will be called early, when the class is loaded.
You define a class ListImages but in the last line of your code you refer to MyApp.
This question already has an answer here:
How to create instance of Twitter::Tweet to create retweeted_by_user
(1 answer)
Closed 8 years ago.
I am using the Ruby Twitter gem. I want to call retweeted_by_user(user, options = {}). How do I create instance of object to call this method. It is instance method, correct?
I did this for Twitter::REST::CLIENT
client = client = Twitter::REST::Client.new do |config|
config.consumer_key = "..."
config.consumer_secret = "..."
config.access_token = "..."
config.access_token_secret = "..."
end
but it does not work for Twitter::Tweet
client = client = Twitter::Tweet.new do |config|
config.consumer_key = "..."
config.consumer_secret = "..."
config.access_token = "..."
config.access_token_secret = "..."
end
1) What does the code you posted have to do with calling retweeted_by_user()?
2) I want to call retweeted_by_user(user, options = {}). How do I create instance of object to call this method. It is instance method, correct?
If you go to the twitter gem website:
https://github.com/sferik/twitter
...then click on the Documentation link, it takes you here:
http://rdoc.info/gems/twitter
...then if you click on Method List in the upper right corner, and look down the list for retweeted_by_user(), and click on that link, it takes you to the method definition here:
http://rdoc.info/gems/twitter/Twitter/REST/Timelines#retweeted_by_user-instance_method
...then if you scroll to the top of that page, you can see that the method is defined in a module called:
Twitter::REST::Timelines
In ruby, modules are included in a class, and then instances of that class can call the (instance)methods defined in the module. What classes include the Twitter::REST::Timelines module? At the top of the page it says:
Included in:API
If you click on the API link, it takes you here:
http://rdoc.info/gems/twitter/Twitter/REST/API
and at the top of that page it says Module: Twitter::REST::API.
Okay, a module can also include another module, which means it's as if the including module itself defines the methods in the included module. But we still need to find a class somewhere that includes that module, and at the top of the page under Module: Twitter::REST::API, it says Included in:Client, and if you click on the Client link, it takes you here:
http://rdoc.info/gems/twitter/Twitter/REST/Client
and that page says Class: Twitter::REST::Client...and that means that objects of the class Twitter:REST::Client can call the method retweeted_by_user(), so you can write something like this:
results = Twitter::REST::Client.new.retweeted_by_user(....)
In fact, on the page documenting Twitter::REST::Client, there is a heading called Instance Methods, and if you scroll down the list of methods the docs show all the instance methods that come from included modules, e.g.
Methods included from Timelines: #home_timeline, #mentions_timeline,
#retweeted_by_me, #retweeted_by_user, #retweeted_to_me,
#retweets_of_me, #user_timeline
In regards to your code here:
client = client = Twitter::REST::Client.new do |config|
config.consumer_key = "..."
config.consumer_secret = "..."
config.access_token = "..."
config.access_token_secret = "..."
end
the docs say this about Twitter::REST::Client.new(),
Constructor Details
This class inherits a constructor from Twitter::Client
If you click on the Twitter::Client link, it says:
- (Twitter::Client) initialize(options = {}) {|_self| ... }
What that means is: if you call Twitter::REST:Client.new() and you specify a block with one parameter, ruby will pass the new Twitter::Rest::Client instance to the block, and then you can call retweeted_by_user() on that object, e.g.:
Twitter::REST::Client.new do |twitter_rest_client_instance|
results = twitter_rest_client_instance.retweeted_by_user(....)
...
end
I've been looking for the answer to my problem forever. For some reason I get this error: send_tweet': undefined methodupdate' for nil:NilClass (NoMethodError)'
whenever I try to run my ruby script. I don't know how to fix this.
Here's the code:
class TwitterConnect
def intialize
#client = Twitter::REST::Client.new do |config|
config.consumer_key = "CONSUMER KEY"
config.consumer_secret ="CONSUMER SECRET"
config.access_token = "ACCESS TOKEN"
config.access_token_secret = "ACCESS TOKEN SECRET"
end
#client.middleware.insert_after Twitter::Response::RaiseError, CustomMiddleware
end
def send_tweet (twitterMessage = "Hello world!")
#client.update("New TwitterConnect object intialized")
puts twitterMessage
end
end
If this is all the code you have, your send_tweet method isn't working because #client is never defined. You'll need to fix a couple things to make it work.
First, to have any access, you'll need to create a new instance of the class.
tc = TwitterConnect.new
Then, you'll be able to access your client instance variable within your newly created TwitterConnect object.
tc.send_tweet
If, alternatively, you are already doing this step and still getting the error, the problem is likely in the authentication of your config variables within the initialize method.
Hope that helps!