I am new to Ruby and had a quick question.
I am trying to get all of the posts from a user's timeline, so I figured I would need to do a user_timeline api call to twitter and then filter out the posts manually. Then, while reading the Ruby Twitter documentation, I found this:
puts Twitter.user_timeline("twitter_handle").first.text
...and that will return the post already parsed out.
Is there a way to get more than just the first post automatically parsed out like that, or is that just an array method for the first and last object in the array?
Thanks
It looks like user_timeline just returns an array, so you ought to be able to use Ruby's normal array methods with it.
Twitter.user_timeline("twitter_handle").each do |tweet|
puts tweet
end
You need to iterate over each object.
Twitter.user_timeline("twitter_handle").each do |tweet|
puts tweet.text
end
The first and last methods are just convenience methods on arrays.
As you can see from source, Twitter::Client#user_timeline returns array from 20 most recent Twitter::Status, so that you can use up to 20 parsed records.
Related
I'm still new(ish), to POM, but I've found the syntax and general structure quite strong, so now I'm looking to advanced techniques.
I have a dynamic page, and for each of the sections I am running the following code/psuedo code
if has_SECTVAR1?
$LOG.info("Stuff")
end
if has_SECTVAR2?
$LOG.info("Stuff")
end
What I want to do is something like this.
ALLSECTIONARRAYS.each do |var|
if has_var?
$LOG.info("Stuff")
end
end
Any thoughts?
You can get an array of element names using #mapped_items. The more interesting part is checking if those exist on the page by calling #has_element?.
The abstract version of what you want to do is call a method on an object given its name as a string. To do this, use #send:
MyObject.send("method_name", *args)
Or in your case:
MyPage.send("has_element?")
Finally, to iterate over all elements:
MyPage.mapped_items.each do |item|
if MyPage.send("has_#{item}?")
$LOG.info("Stuff")
end
end
I'm modifying a ruby gem.
Currently returning a git log object between 2 revision numbers correctly:
def log(repo, prev_rev, this_rev)
repo.log.between(prev_rev, this_rev)
end
And storing the commit message using:
gitlog_item.each do |commit|
#message = commit.message
My question is what other information is available from this object? Specifically I'm looking for something that can identify the commit uniquely such as an ID, or date & time.
If you want to see the entirety of what your gitlog_item contains, you can simply inspect it, like so:
puts gitlog_item.inspect
That'll output the entire object to your console, so you can see everything that's available to you in that object.
This should be the documentation you are looking for.
Methods sha and committer_data seems to do for you.
I'm new to Ruby and trying to learn the ropes. I have an object returned from a call to the Twitter API. I'd like to loop through the :users below and be able to print out their different attributes... like :id, :screenname, etc.
BTW, these are all followers on twitter. So the object is called "Followers" and this is what I'm calling to get the inspect... "followers.inspect"
The gist of the followers.inspect is here:
https://gist.github.com/anonymous/7966898/raw/938a0048d08f44415229b98a1adef96c4946ce8d/gistfile1.txt
My problem is that I'm having a hard time figuring out how this .inspect print out matches up to what I should be looking for.
So the code I've written to try to work through this is...
followers.users.each do |i|
puts "#{i.name} has user id #{i.id_str}"
end
The error I'm getting is that there is no method called users.
Sorry, I'm sure this is a very simple question but have just gotten stuck!
As far as I can quickly see, the object you have contains a hash #attrs with a key :users.
So, it should be something like:
followers.attrs[:users].each do |i|
puts "#{i.name} has user id #{i.id_str}"
end
Maybe you'll also need to use i[:name] since all those users are also hashes, not objects with methods, but my Ruby is a bit rusty, so I don't know if those methods are automatically available when a key is present in a hash.
Anyway: {:key => "value"} in inspect means that it's a hash with keys and values, that you access like: nameofhash[:key]
[{:key => "value"},{:key => "value"}] means it's a array with two seperate hashes.
i am new to ruby and nanoc. I am trying to sort articles based on time. So I get more accurate results on my blog.
This is what I am using in my sorted_articles_time.rb file under /helpers/
def sorted_articles_time
articles.sort_by do |a|
attribute_to_time(a[:time])
end.reverse
end
But then I get the error
NoMethodError: private method `sorted_articles_time' called for #<Nanoc::Site:0x007fd93b0a3f40>
What am I doing wrong ? And is there a way to overwrite the existing sorted_articles method ?
Thanks
UPDATE: I already have it initiated in the rake file. So I think my rake file is fine here.
time1 = Time.new
#time = time1.inspect
Are you calling #site.sorted_article_times? If so, leave off the #site part. Helpers are intended to be called as functions, not as methods on #site.
Use a scrope from your model. Check this activeactive_record_querying this is the best place to sort your articles.
So I need to access this service from my rails app. I'm using soap4r to read the WSDL and dynamically generate methods for accessing the service.
From what I've read, I should be able to chain methods to access the nested XML nodes, but I can't get it to work. I tried using the wsdl2ruby command and read through the generated code. From what I can tell, the soap library is not generating these accessor methods. I'm pretty new to ruby, so I don't know if I'm just missing something?
I know when I inspect the element, I can see the data I want. I just can't get to it.
For instance if I use the following code:
require "soap/wsdlDriver"
wsdl = "http://frontdoor.ctn5.org/CablecastWS/CablecastWS.asmx?WSDL"
driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver
response = driver.getChannels('nill')
puts response.inspect
I get the following output:
ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}binding
ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}operation
ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}body
ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}address
#<SOAP::Mapping::Object:0x80b96394 {http://www.trms.com/CablecastWS/}GetChannelsResult=#<SOAP::Mapping::Object:0x80b96178 {http://www.trms.com/CablecastWS/}Channel=[#<SOAP::Mapping::Object:0x80b95f5c {http://www.trms.com/CablecastWS/}ChannelID="1" {http://www.trms.com/CablecastWS/}Name="CTN 5">, #<SOAP::Mapping::Object:0x80b9519c {http://www.trms.com/CablecastWS/}ChannelID="2" {http://www.trms.com/CablecastWS/}Name="PPAC 2">, #<SOAP::Mapping::Object:0x80b94620 {http://www.trms.com/CablecastWS/}ChannelID="14" {http://www.trms.com/CablecastWS/}Name="Test Channel">]>>
So the data is definitely there!
Here is the code generated by wsdl2ruby for the method being used above:
# {http://www.trms.com/CablecastWS/}GetChannels
class GetChannels
def initialize
end
end
# {http://www.trms.com/CablecastWS/}GetChannelsResponse
# getChannelsResult - ArrayOfChannel
class GetChannelsResponse
attr_accessor :getChannelsResult
def initialize(getChannelsResult = nil)
#getChannelsResult = getChannelsResult
end
end
Sorry for the long post, I figured the more info the more likely someone can point me in the right direction.
Thanks
-ray
Answer
require "soap/wsdlDriver"
wsdl = "http://frontdoor.ctn5.org/CablecastWS/CablecastWS.asmx?WSDL"
driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver
response = driver.getChannels('nill')
for item in response.getChannelsResult.channel
puts item.name
puts item.channelID
end
How I got the Answer
You can figure out the methods of response via
response.methods
This will get you a long list of methods that are hard to sort through, so I like to subtract out the generic methods. Ruby lets you subtract arrays.
response.methods - Object.new.methods
Using this technique, I found the getChannelsResult method for response. I repeated the process
resonse.getChannelsResult.methods - Object.new.methods
I found the channel method for its result. Again!
response.getChannelsResult.channel.methods - Object.new.methods
This returned a bunch of methods including: sort, min, max etc. So I guessed Array. A simple confirmation was in order
response.getChannelsResult.channel.class
Sure enough it returned Array. To make life simple, I just worked with the first item of the array to get its methods
response.getChannelsResult.channel.first.methods - Object.new.methods
Whoalla, I found two more methods "name" and "channelID"