PhantomJS + CasperJS from Ruby - Reuse Code? - ruby

I am calling CasperJS from the backend of my Ruby on Rails application using Open3.popen3 to make a command line call. The filename (in my case CoffeeScript) is the first argument followed by options.
Many of my coffee files do similar tasks. I see examples of of how to reuse code with modules, but I think that's a NodeJS only thing.
Any suggestions how I might reuse common code in my situation? I'm really getting horribly un-DRY.
UPDATE:
hexid's answer is correct. What I was missing when I tried it before is that you need the rooted file path, not relative the current file path:
my_module = require('/rooted/path/to/the/file.coffee')

PhantomJS has support for CommonJS' require.
You won't, however, be able to require NodeJS modules because PhantomJS doesn't run on NodeJS, but instead on a version of Webkit that is included in QT.

Related

Chef Shared Ruby Functions or libraries

We have a local Ruby Library that we include in a lot of our ruby projects. It contains a lot of configuration information that would be extremely useful to use in our chef scripts. This allows us to put all of our configuration in one place, so we don't have to make multiple places everytime we change a database etc. Trying to keep the code DRY. That being said, the code are straight ruby functions, not in the chef DSL.
I have been trying to pull the library into Chef, but have found it very difficult. Which makes me think I'm going against some sort of pattern.
I have tried and didn't work.
- Add the Ruby Code via require_relative and includes.
- Add the Ruby Code to it's own cookbook, then wrap the cookbook.
- Create a local ruby gem (not retrievable via rubygems, or other repo)
What worked:
- Copying and pasting the code into a cookbook. (but it's not sharable.)
My real question, what is the best way to share this ruby code library amongst many cookbooks? Depending on the best way, how do you actually do it?, or pointers in the right direction.
thanks.
myles.
I would use Halite (disclaimer: my project) to convert the gem into a cookbook and then upload it as a cookbook. Then you can depend on it and require stuff from it like normal in your Chef code.
You can create a cookbook and copy the ruby code into libraries folder of the cookbook. Then it will be loaded by Chef automatically on node, as soon as you include the cookbook into run list.
Could you tell, what exactly you did, and what didn't work when you tried "Add the Ruby Code to it's own cookbook, then wrap the cookbook"?
I have created a ruby library. Basically, it reads the ip of the server in opsworks. Once it gets that it knows what environment it is in, and it will set a hash. This hash is then used to set the attributes. This way all the server configurations are stored in one place.
I can put the code in each different cookbook that needs it. However, I have to duplicate the code in each different cookbook. I would like to put the code in one place, and include it in each cookbook. That way if I have to modify it, I only have to do it in one place.
I tried making it a gem, and requiring it with no luck.
I also created a cookbook with nothing but the ruby code in a library. I then included "depends etc" that cookbook in another and tried to run the library functions, but I couldn't get it to work.
I may have been on the right track, but I stopped before I got it working.
Essentially I want to write a simple ruby code library, that I can use in any cookbook I want. There must be a way to do this, but I've run into some dead ends. I'll keep banging my head against the wall. Eventually, a hole will open up!

require 'command_line_argument_parser'?

I'm following a tutorial here for a simple web crawler (http://www.skorks.com/2009/07/how-to-write-a-web-crawler-in-ruby/) and one of the lines is to require 'command_line_argument_parser'.
My system is unable to find the gem. The post was written in 2009. Without the gem, the code I downloaded from this guy doesn't work.
Any idea how to proceed?
If the main point of the code is Web crawling, the cli arg parser can probably be switched out without upsetting much. I would just use Ruby's build-in optparse library. http://ruby-doc.org/stdlib-1.9.3/libdoc/optparse/rdoc/OptionParser.html
I found command_line_argument_parser in the ZIP archive for the tutorial. If you've extracted the file into the same directory as your crawler and it still doesn't work, try changing the require line to require_relative.
I'd also recommend switching to optparse if you have the time though, it's much more robust.

Approach for installing system service implemented as Ruby gem

After years of being away from Ruby, I'm back full-force and have just cut my first gem, which includes an executable. Everything works like a charm.
The problem I am facing, however, is that I ALSO have a startup script (not part of the gem istelf) that daemonizes the executable. Additionally, I'd also like for the startup script to point the executable at configuration in a place like /var/
To the best of my knowledge, there's no way with rubygems, gemspec, etc., to specify files getting blown out to other parts of your system during install (e.g. startup script to /etc/init.d, and config to /var/). It certainly wouldn't make sense if you COULD do that.
So... my question is... what IS the proper procedure for automating the installation of something like this. I'm using RHEL, and am wondering if it's, perhaps, time for me to get my feet wet with making my first RPM.
Any thoughts?
You can do it. However it is probably not quite the recommended approach. But yes it is possible to run arbitary code during gem installation using the extensions option.
From the RubyGems Manual:
Usage
spec.extensions << 'ext/rmagic/extconf.rb'
Notes
These files will be run when the gem is installed, causing the
C (or whatever) code to be compiled on the user’s machine.
Just place whatever ruby code you need into the extconf.rb (or equivalent) file.
Examples for building C-extensions from the RubyGems Guides:
http://guides.rubygems.org/c-extensions/

Ruby standalone app deployment/distribution

What's the best way to distribute a simple command-line Ruby app to clients in a way that would not require them to manually install Ruby and required Gems?
In my understanding this task boils down just to a couple of lines of SH/BAT code that does Ruby/Gems checks and if not found goes on with Ruby installation with RVM.
So do these lines of code exist already somewhere or will I need to write something on my own?
I've used this project for small scripts in the past, without any issues
http://www.erikveen.dds.nl/rubyscript2exe/
It creates an EXE file out of your ruby script.
If you need something cross-platform, the BAT/sh option is probably best. You could grab RVM, have RVM install ruby, use bundler for your gems, and then launch the script.
The closest I've found is (I believe) releasy: https://github.com/Spooner/releasy
I failed to find any way of producing a Ruby cross platform installation.
I've created a RubyAnywhere script that tries to solve this task.

Can Ruby + Crate + Windows work?

I've got a project for work I'd like to do in Ruby that will have to run on Windows, but perturbing the filesystem for a Ruby install or RubyScript2Exe unpack isn't an option (this is supposed to be the harness for a testing system). Has anyone successfully used Crate to package up something on Windows? If so, what was your build environment like and can you pass on any other hints?
I've tried and worked in getting Crate work under Windows, but is a more complicated system than I would expect.
If extraction of code for your system is your problem. I recommend take a look to Exerb, and specially: exerb-mingw hosted on GitHub exerb-mingw
It will generate a single executable like Ocra or RubyScript2Exe, but with the difference that the source code will not be extracted and extensions will be dynamically loaded.
This works perfectly with RubyInstaller packages, and is being used with Pik (Ruby version manager for Windows).
Hope this helps.
You can embed a Ruby interpreter and script into a C program, which may be easier than trying to run Crate. Here are some helpful links that describe how to do this, and may provide enough sample code to use as a skeleton for what you are trying to build.

Resources