Can I programmatically retrieve a gem's spec from the gem itself? - ruby

I have a gem. Is there a way to retrieve the gem's specification from within the gem itself?
Specifically, I have some metadata that I want to retrieve and display when running one of the gem's executables. Is there a way to retrieve that metadata from within the gem?

Since your gem is definitionally already loaded by this point, Gem.loaded_specs['name-of-your-gem'] will give you what you want.
Documentation for Gem.loaded_specs

Related

Using `wicked_pdf` in a non-rails project

All the documentation for wicked_pdf assume you are using rails.
But I want to use this feature in a non-rails environment (a Jekyll plugin, written in Ruby).
Trying to use pdf = WickedPdf.new.pdf_from_string(page.content) returns Error: uninitialized constant WickedPdf.
How do I initialize WickedPdf without the rails generate wicked_pdf provided in their documentation?
I had to add gem 'activesupport' to my gemfile, then use require 'wicked_pdf' in my source code.
wicked_pdf depends on active_support/core_ext - as you can see in lib/wicked_pdf.rb. However you can use it outside of controller "context" like this...
pdf = WickedPdf.new.pdf_from_string('<h1>My life is wicked.</h1>')

How to use Caching in ruby gem?

My gem will going to call some service to get data based on Parameters given. I want to cache my gem method. I don't know How to implement caching in gem.

How do i implement factual in Ruby?

I chose "factual-api" gem after looking at the factual website. May I know how do I set it up and use the function to get the locations of those areas? Can I write it in the controller/model?
After trying to install the gem, I get this when I look at my gemfile:
Gem "factual-api" is unavailable in SDK 'RVM: ruby-1.9.2-p290
This inspection warns about unavailable gems inside the current SDK. The IDE needs the gems to provide come completion, navigation and analysis. An appropriate quick-fix is available to install the required gems.
When I run the command bundle install, I do not have any errors. But when I called:
factual = Factual.new("YOUR_KEY", "YOUR_SECRET")
I get this:
NameError: uninitialized constant Factual
What am I doing wrong now? What should be the right way to do so?
This answer is a bit late, but I'm posting it anyway so it can be marked as answered.
As mentioned in the comments the recommended approach is to use Factual's Ruby Driver which can be found at http://github.com/Factual/factual-ruby-driver or installed with the following in your Gemfile:
gem 'factual-api', :require => 'factual'
If not using a Gemfile remember to manually require 'factual' somewhere. I also like to put an initializer in the config folder with the OAuth details if it's a private repository that looks something like this:
class Factual
KEY = 'YOUR-OAUTH-KEY'
SECRET = 'YOUR-OAUTH-SECRET'
end
This allows you to access the Factual API anywhere in your app with
factual = Factual.new Factual::KEY, Factual::SECRET

What is the origin of System::Process.new?

Where is the origin of Process.new and where is it doccumented? I have looked in the Ruby docs at the process module and I cannot figure out how this is declared.
The code I am trying to replicate is in the Ruby God gem in lib/god/conditions/memory_usage.rb:66:
process = System::Process.new(self.pid)
#timeline.push(process.memory)
System::Process isn't part of Ruby, it comes from God (the gem) itself. You can view its source if you want.
It's referable as System::Process in the file you reference because you're already in the God module, so Ruby resolves it within that namespace.

Can I specify dynamic dependencies for my gem based on command line input?

Intro: I'm working on a gem that, by default, will pull information out of some XML data and do some sort of processing on the document. I'm using nokogiri to parse the XML. However, I wish to allow the user to parse the XML themselves and pass in the necessary information for my data processing methods to run, in case they don't want to install nokogiri or have already parsed the XML.
Question: Is there any way to allow the user to specify, during gem installation, that they don't wish to install the nokogiri dependency? For example (very hand-wavey here),
gem install crazy_gem --no-nokogiri
and in the gemspec perhaps something like
Gem::Specification.new do |s|
...
s.add_dependency 'nokogiri' unless Proc.new { install_flags('no-nokogiri') }
...
end
[edit] I don't want to focus too much on the gemspec code above, as I know it doesn't work--it's just an example of the kind of thing I want to do. [/edit]
gem install crazy_gem --ignore-dependencies works great until there are additional dependencies.
I don't think you can do exactly what you're after, but there's apossible solution if you reframe what your gem does. Rather than a gem that by default parses some XML and processes the data, but optionally allows you to pass in the pre-parsed data, what about a gem that is mainly concerned with the processing, but optionally will parse the XML for you (if you have Nokogiri).
To do this just leave Nokogiri out of your gemspec dependencies (you could add it as a development dependency or a requirement).
Inside your code, make sure you only call require 'nokogiri' in a begin..end block with a rescue LoadError and deal with it as appropriate.
Gemspecs are turned into static files at build time, so that wouldn't work. You could try using -f which bypasses dependency checks.

Resources