How to use Caching in ruby gem? - ruby

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.

Related

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

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

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

Ruby on Rails with chargify

I want to integrate chargify to my rails app. I have user object and I want the user to be able to subscribe for one month and update the boolean column on user object. I prefer to use the API not hosted pages. How can I do that?
Is there any example for chargify on ruby on rails for handling subscriptions but with details about mvc for newbies?
Based on this thread and the Googles it looks like there is not a whole lot out there.
You could try looking at the Rails 2 example here and converting it or use the gem here (gem "chargify", "~> 0.3.0").
I know none of this is aimed at newbies but the info seems to sparse.
This might get you going. It seems that Chartify itself is written in Rails, and therfore their API is ruby code, which you can use...

Getting around ruby-units conflict with activesupport

I want to use the ruby-units with a rails 3 project, but it seems like it conflicts with activesupport.
It looks to me like both activesupport and ruby-units create a to() method for String. For some reason, ruby-units one wins, and so whenever to() is called inside rails it throws an error. (Unit not recognized)
I want to know the best way to deal with this. I don't care about having the to() method from ruby-units, I just don't want it to interfere with rails. I'd like to avoid forking if there's another approach.
To see my problem:
Add to your gemfile
gem 'ruby-units'
Open up rails console (I'm on rails 3 with ruby 1.9.2) and try apples.to(1)
Without ruby-units:
"ap"
With:
"'apples' Unit not recognized"
The answer to this is to require the ruby-units library before the rails library in the Gemfile:
gem 'ruby-units'
gem 'rails'
Obviously you then won't be able to use .to() on strings to access the ruby-units conversion.

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