Ruby Gems Documentation - ruby

I'm just trying to understand how to use particular ruby gems. For example, take this reddit gem. It says to have this code to start:
require 'snoo'
# Create a new instance of the client
reddit = Snoo::Client.new
# Log into reddit
reddit.log_in 'Username', 'Password'
# Send a private message to me (Paradox!)
reddit.send_pm 'Paradox', 'Snoo rubygem rocks!', "Hey Paradox, I'm trying your Snoo rubygem out and it rocks. Thanks for providing such an awesome thing!"
# Log back out of reddit
reddit.log_out
Great but in the documentation you can see that the Client class doesn't have very many exciting functions. The exciting functions are in the Account class but there is no way to get to it...because if I try something like this
reddit = Snoo::Account.new
I get this error:
`initialize': undefined method `new' for Snoo::Account:Module (NoMethodError)
Okay so there's no new method but how do I make an Account object and use its functions like log_in?

Snoo::Account is a Ruby Module, and has been mixed in to Snoo::Client already by the gem. All the functions of Snoo::Account are already available to you on the reddit object.
The synopsis documentation in the readme doesn't make this very clear. But otherwise the documentation on the gem looks good to me.

Taking a short look at the source code on github makes me believe this is a fault in the documentation, as client clearly includes the functionality of many other modules, including the Account module you would like to access. In your example code, try the following methods to confirm it for yourself:
reddit.methods.sort
reddit.is_a? Snoo::Account
I assume the documentation software didn't catch the includes as they were executed using a block.

Related

Aliasing the Module in Google-API-client Gem

Summarize the problem:
Being relatively new to Ruby/Gems and developing in general, some concepts evade me
I'm learning about the google-api-client Gem, and am attempting to understand the Basic Usage, and want to know how a developer knows which class to use, when instantiating an ojbect, during the "aliasing of the module" portion:
To use an API, include the corresponding generated file and instantiate the service. For example to use the Drive API:
require 'google/apis/drive_v2'
Drive = Google::Apis::DriveV2 # Alias the module
drive = Drive::DriveService.new # why is ::DriveService used here?
#etc
Describe what I've tried:
I've searched through the reference documentation for the google-api-client for a clue about the "decision" to instantiate drive with ::DriveService.new
The best reason I've come up with is: DriveService is instantiated because it is the "BaseService" of the "DriveV2" Class.... but I'm reaching for straws with this logic.
My specific question is:
How does a developer using APIs and this Google-API-client Gem know which object to instantiate?
I have to imagine there's a more elegant "way" to determine which object to instantiate at this point of accessing an API than digging through the documentation of the Gem....I mean...the "BaseService" information is coming from the documentation for this specific Gem.....
Maybe this is a matter of me losing "scope" per say by the Google API and the ambiguously named Gem maintained by Google...
But then again...if I'm using this Gem...then this documentation would always apply, because I wouldn't be able to use this Gem if it wasn't a Google-API....
from the documentation
The link above is the necessary detail regarding Authorization for an API key.
If you're like me, and this subject is new to you, there are three topics that you need to understand:
Authentication
Authorization
Accounting
The documentation for the google-api-client gem is robust enough to answer a lot of questions, however, my answer here is hopefully enough to get you pointed in the right direction.
I'm leaving the question up, in case anyone else needs some guidance regarding this same subject.

confluence4r add

We use Jira/Confluence as our wiki site. I've had a difficult time trying to figure out how to use the add. I'm guessing i'm missing something very obvious. When I go to this site: https://confluence.atlassian.com/display/DISC/Confluence4r to download the confluence4r file, not sure what I'm supposed to do thereafter. The file contains a module which makes sense why it doesn't do anything when running it. But should I being using the gem install functionality in some way? When I simply try to use it in a ruby script, i get the following error:
conf.rb:15:in `<main>': uninitialized constant Confluence (NameError)
Where I am supplying the information required per the script (URL, user & pass contained the correct values when used):
server = Confluence::Server.new("https://collab.sitename.com")
server.login("user", "pass")
puts server.getSpaces()
Any information how to get the working is appreciated.
Confluence4r isn't distributed as a rubygem, it's just a ruby script you can drop onto your filesystem and reference directly.
If you put Confluence4r.rb in the same directory as your own script, you'd need to require it like this:
require './confluence4r.rb'
You shouldn't need the "confluence" and "confluence-client" rubygems to use confluence4r; it's just a very thin wrapper around the Confluence XML-RPC API.

How do I get Twitter follow request with the Tweetstream Ruby gem?

According to this Ars Technica tutorial, "the streaming API makes it easy to detect when the user gets a new follower. To detect follow event objects, look for the "event" key and check if it has the string "follow" as the value."
http://arstechnica.com/information-technology/2010/04/tutorial-use-twitters-new-real-time-stream-api-in-python/2/
The tweetstream gem supposedly exposes all methods from the API. However I can't figure out, how to get to those follow requests!
Any ideas?
The tweetstream gem is fairly complete, but sometimes you need to delve into the source to find stuff:
client.on_event(:follow) do |event|
p event[:source][:screen_name]
end
https://github.com/intridea/tweetstream/blob/master/lib/tweetstream/client.rb#L375 :)

Api for google-api-client gem

How do I get the api of this gem? I could currently get the name and email of the user with
google_client.execute!(:api_method => GoogleLogic.get_google_oauth2.userinfo.get).data.name
google_client.execute!(:api_method => GoogleLogic.get_google_oauth2.userinfo.get).data.email
but no where are these methods written in the official page of this gem
http://code.google.com/p/google-api-ruby-client/
Since the ruby client is generated dynamically, it may not have API docs. You can see the API definition here:
https://www.googleapis.com/discovery/v1/apis/oauth2/v1/rest
It might be useful to you to see what methods/attributes are generated in the library.
Not sure if this is relevant anymore, but I found the documentation on the github page quite useful:
https://github.com/google/google-api-ruby-client
And also the samples:
https://github.com/google/google-api-ruby-client-samples
Lastly, the developers console in google is very useful.
http://console.developers.google.com/

What does the World() method do in ruby?

I'd like to know what does the following line do:
World(::Cucumber::Rails::Capybara::SelectDatesAndTimes)
Because I'm getting
uninitialized constant Cucumber::Rails::Capybara::SelectDatesAndTimes::XPath (NameError)
And I don't understand what it is supposed to do to tell what's wrong :-)
EDIT: I gisted my Gemfile: https://gist.github.com/822480 and my gem list on windows vista 32-bit (https://gist.github.com/822483) and ubuntu maverick 32-bit https://gist.github.com/822491. Both are running ruby 1.8.7. Notice that I'm using this fork of cucumber-rails: https://github.com/johnf/cucumber-rails because my ultimate goal is to get these datetime selectors to work with capybara.
I also found that on Linux I get another error message:
Unable to find '#<XPath::Union:0xb6e078b8>' (Capybara::ElementNotFound)
The World() method isn't one from Ruby, it's one specifically from Cucumber, regrettably they haven't even documented it.
http://rubydoc.info/github/aslakhellesoy/cucumber-rails/master/Cucumber/Rails/World:initialize
In this case, it looks like something it missing from your Environment, maybe Caprybara, please give more info, and share your Ruby/Rails/Cucumber/Bundler/etc versions, and your Gemfile.
World is just an instance of Object that you can use in every step definition, its like a library that you expose without the need to import (or require in every step definition file).
see more at: https://github.com/cucumber/cucumber/wiki/Configuring-the-Scenario-Execution-Context-(World)
(Note that I'm still learning, so this may be wrong.)
A world-level method involves multiple objects versus a class- or object-specific method.
If you are having an issue with calling a world method, then check to see if the world method is specifying an object that can't be found.

Resources