Using `wicked_pdf` in a non-rails project - ruby

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>')

Related

NameError: undefined local variable or method `broadcast_messages' for #<RailsStdoutLogging::StdoutLogger:0x007f4d099fb6b0>

Title error when accessing ActiveRecord model only on heroku with rails 5.0.0-beta1. Works on production env heroku. Problem persists anytime Rails.logger is set to STDOUT wether between gem 'rails_stdout_logging' or in config/environment/production.rb
I've reviewed implementation and see broadcast_messages as a method created by the accessor macro. I'm at a loss and am looking for suggestions to troubleshoot.
Turns out heroku's rails_stdout_logging gem which is included as part of heroku's rails_12factor gem has a bug with rails 5.0.0-beta1.
StdoutLogger inherits from stdlib's Logger rather than ActiveSupport's Logger via < ::Logger. I've got a workaround on my fork but it injects an ActiveSupport dependency when not using rails. I'm new to ruby so I don't know best practice to make a more eloquent fix.
See https://github.com/MattWalston/rails_stdout_logging.

Localizing numbers in Ruby without Rails

I've got a Ruby app that is not using Rails, but needs localization. I've been using the I18n gem, which works well for dates, times, and strings. It doesn't seem to do numbers, though, unless I'm missing something.
I found the NumberHelper class in ActionView from Rails, and this question says how you can include it in a non-Rails app. However, making my app dependent on the whole activemodel framework seems like overkill just to get a few numeric localization routines.
Is there a better way to approach this? Should I be using one of the other Ruby localization libraries instead?
I've used the gem R18n for that: https://github.com/ai/r18n
It can do the same stuff as I18n but is more flexible and can also localize numbers. It does not have dependencies of other gems.
Basic usage:
Add r18n-core to Gemfile
Initialize with `R18n.extension_places << 'path/to/yml/files'
And then either:
i18n = R18n::I18n.new('en')
i18n.t[:foo][:bar] # => "Translated text"
i18n.l(123.45) # => "123.45"
Or the more static version:
R18n.locale = 'en'
R18n.t[:foo][:bar]
R18n.l(123.45)
Helpers exists (in other gems) for rails and sinatra

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

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.

Rails 3.1 Engine plugin rspec testing (external gem Spree Commerce)

I created an engine inside an app that depends on an external gem: Spree.
And I did my best to set RSpec within that engine following this guide.
When I try testing a controller with some integrated testing:
tests_spec.rb:
require 'spec_helper'
describe "Tests" do
describe "GET /tests" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get tests_path
response.status.should be(200)
end
end
end
I get an error in the dummy app's environment.rb file, that says that following command couldn't be run:
# Initialize the rails application
Dummy::Application.initialize!
Because my controller is using Admin::ResourceController as an extension which is declared within the external Spree gem. As if rspec is unable to read that class from gemfile of the main app (the one that contains the engine plugin).
Can someone please shed some light on that?

Resources