How to use PDFNet instance across the ruby app - ruby

I have to initialize the PDFNet library once in my app. I did it in my app.rb which is run first
# app.rb
require 'PDFNetC/Lib/PDFNetRuby'
include PDFNetRuby
PDFNet.Initialize(ENV['PDFTRON_LICENSE_KEY'])
Later in my controller I need to convert pdf to xod for which I will require the initialized PDFNet instance
# SomeController.rb
out_file_path = "#{ENV['TEMP_DIR']}/#{Time.now.to_i}.xod"
::PDFNetRuby::Convert.ToXod(in_file_path, out_file_path)
File.open(out_file_path, 'rb')
Will the above method get the license registerd instance of PDFNet?

PDFNet.Initialize is a process wide call, so if both code snippets are in the same process, both will be in licensed mode.

Related

Selenium Webdriver can't find "navigate" element in Ruby

I am new to this so it may be obvious, but I am struggling with getting my test to run the "navigate" element. Or Object. Whatever it is. Here is my code:
require "selenium-webdriver"
require "rspec"
# TEST: Sign up for blog
describe "Blog application" do
describe "signup to the blog application" do
it "confirm that a user can successfully signup" do
driver = Selenium::WebDriver::Firefox
# Go to signup form
driver.get "https://selenium-blog.herokuapp.com/signup"
# Fill out and submit form
username_field = driver.find_element(id: 'user_username')
username_field.send_keys("user")
There's more to this test but it isn't relevant to my question. What am I missing? Do I need to install another gem or driver? I've searched everywhere for this answer and can't find it.
I've also tried running it with:
driver.navigate.go
and that fails as well. Says it cannot find the "navigate" element.
Thanks for your help!
You are missing initializing of your wanted web driver. However you didn't post your current version of selenium, so I assumed it was last version.
Here is what documentation says about Selenium::WebDriver.
.for(browser) ⇒ Driver
.for(browser, opts) ⇒ Driver
Create a new Driver instance with the correct bridge for the given browser
One special argument is not passed on to the bridges, :listener. You can pass a listener for this option to get notified of WebDriver events. The passed object must respond to #call or implement the methods from AbstractEventListener.
So in your case you need to change you driver variable
driver = Selenium::WebDriver.for(:firefox)
# ...
WebDriver docs
Here is the simple code which should help you
require 'selenium-webdriver'
driver=Selenium::WebDriver.for :firefox
driver.navigate.to("https://www.google.com/")

is there a pure command-line oauth flow for installed apps?

I'm trying to get a pure command line oauth flow for an installed app and it's not easy to piece this together... Docs are sorely lacking... I started with the drive example (https://github.com/google/google-api-ruby-client-samples/tree/master/drive) but when it gets to client.authorization = flow.authorize(file_storage) it tries to start webrick to put up a web page. I need something that works similarly to the CLI tools provided by google: it needs to print out the URL I need to visit and then read in the response that I can copy&paste. Is this possible with the google ruby client?
Looks like the following monkey-patch works:
module Google
class APIClient
class InstalledAppFlow
def authorize_cli(storage)
puts "Please visit: #{#authorization.authorization_uri.to_s}"
printf "Enter the code: code="
code = gets
#authorization.code = code
#authorization.fetch_access_token!
if #authorization.access_token
if storage.respond_to?(:write_credentials)
storage.write_credentials(#authorization)
end
#authorization
else
nil
end
end
end
end
end

how to remove the log of ie web driver?

I am using watir-webdriver + ruby + win7 to test same pages. and I would get these logs while I start the ie explorer by using watir-webdriver:
Started InternetExplorerDriver server (32-bit)
2.32.3.0
Listening on port 5555
are there any methods to remove these logs? any help would be appreciated!
IEDriver supports a --silent flag that suppresses diagnostic output when the server is started.
Unfortunately, at least to my knowledge, it is not configurable when creating a browser instance. Instead, you need to directly modify the Selenium::Webdriver::IE::Server class' server_args method. You can modify the lib\selenium\webdriver\ie\server.rb file directly, but it is probably easier to monkey patch.
To monkey patch the silent flag, add the following to your code some point after requiring watir-webdriver (ie selenium-webdriver) but before opening the browser.
class Selenium::WebDriver::IE::Server
old_server_args = instance_method(:server_args)
define_method(:server_args) do
old_server_args.bind(self).() << "--silent"
end
end
For example, the following will no longer log any messages.
require 'watir-webdriver'
class Selenium::WebDriver::IE::Server
old_server_args = instance_method(:server_args)
define_method(:server_args) do
old_server_args.bind(self).() << "--silent"
end
end
b = Watir::Browser.new :ie

Using sessions in Espresso Framework

good progress so far.
got all controllers and actions up and running in matter of minutes.
However not sure how make sessions work.
trying:
class Cart
session :memory
# my actions
end
but it fails with an error that say to use sessions at app level.
how that? where is that app level?
I guess you are starting the app like this:
Cart.run
it is a good one unless you need to add some setup to your app.
creating app is as easy as:
app = EApp.new :automount
app.run
and if you have some setup for the app, use a block at initialization:
app = EApp.new :automount do
session :memory
end
app.run
Please note the first argument - :automount - it instructs the app to search for controllers and mount them automatically.
If you want to mount your controllers manually, omit first argument:
app = EApp.new do
session :memory
end
app.mount Cart
app.run

Delayed Job passenger production undefined method error

I am using delayed job version 2.1.4 with action mailer 3.0.8 to send my emails in background.
UserMailer.delay.newsletter(email)
It works with me fine in development and production Rails console.
But when it is called from my live production passenger server, it creates DJ but when this DJ runs, it throws
{undefined method `newsletter' for #<Class:0xc08afa0>
I think the problem
Any help?
The problem is with rails/passenger in production mode with class methods
So, I made a work around solution... I will try to call instance method and avoid to call class method.
Lets see an example; assuming I need to call the following method in background..
UserMailer.deliver_newsletter(email)
Then, I created a new class DelayClassMethod
class DelayClassMethod
def initialize(receiver_name, method_name, options={})
#receiver_name = receiver_name
#method_name = method_name
#parameters = options[:params] || []
end
def perform
eval(#receiver_name).send(#method_name, *#parameters)
end
end
and can run the method in background now by
DelayClassMethod.new("UserMailer", "deliver_newsletter", :params=>[email]).delay
So, I am running an instance method now in background which will run the class method.
Another example..
Assume I want to run
Product.list_all_by_user_and_category(user, category).delay
Then it can be run by
DelayClassMethod.new("Product", "list_all_by_user_and_category", :params => [user, category]).delay
My hunch is that you have multiple versions of the delayed_job in production. Are you using bundler to start your delayed job process?
I would recommend when doing bundle install in production, I would use the --binstubs flag to generate a wrapper around the delayed_job and use that executable to start the jobs.

Resources