Skip to content

Instantly share code, notes, and snippets.

View mariozig's full-sized avatar

Mario Zigliotto mariozig

  • Intuit
  • San Mateo, CA
View GitHub Profile

#Heroku, Ruby on Rails and PhantomJS

In this post, I’m going to show you how to modify an existing Ruby on Rails app running on Heroku’s Cedar stack to use PhantomJS for screen scraping. If you’ve never heard of PhantomJS, it’s a command-line WebKit-based browser (that supports JavaScript, cookies, etc.).

Let’s get started. This is a high-level overview of the required steps:

  • Modify your app to use multiple Heroku buildpacks.
  • Extend your app to use both the Ruby as well as the PhantomJS buildpacks.
  • Confirm that everything worked.
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
bloggy_web latest fd72d1901622 10 hours ago 912 MB
<none> <none> a51a0506164a 10 hours ago 903.4 MB
bloggy_square latest 4246ada965f2 10 hours ago 903.4 MB
<none> <none> 6e1e68d610ba 10 hours ago 903.4 MB
docker_square latest 85c7ac711d78 11 hours ago 808.9 MB
postgres 9.4.1 03119fe33f25 4 days ago 213.9 MB
ruby 2.2.1 ef6e4b7dc7cd 3 weeks ago 775.1 MB
kirkdockerized_web latest 83edc1a1d29a 4 months ago 347.8 MB
## Issue:
NameError in Admin/tweetsController#index
undefined local variable or method `tweets' for #<Class:0x7fcbe98f68f0>
Rails.root: /home/rails/
Application Trace | Framework Trace | Full Trace
app/models/tweet.rb:23:in `add_tweets'
app/models/tweet.rb:15:in `update_twitter_account'
app/models/tweet.rb:9:in `update_tweets'
io@love:~/rails/m3$ git branch -a
admin
authlogic
inlineadmin
list
* master
remotes/origin/add_devise
remotes/origin/admin
remotes/origin/authlogic
remotes/origin/master
Padrino.after_load do
require 'will_paginate/view_helpers/base'
require 'will_paginate/view_helpers/link_renderer'
require 'will_paginate/finders/active_record'
WillPaginate::Finders::ActiveRecord.enable!
end
@mariozig
mariozig / gist:1268326
Created October 6, 2011 19:06
Sort an array of hashes
array_of_hashes = ModelOneAddress.all + ModelTwoAddress.all
array_of_hashes.sort_by { |hsh| hsh[:zip] }
@mariozig
mariozig / scoped friendly_id w sti.rb
Created October 8, 2011 20:05
scoped friendly_id w/ sti
## The setup
# This assumes you already have friendly_id in your gemfile and all that good stuff.
class Car < ActiveRecord::Base
# Slugs via FriendlyId
extend FriendlyId
friendly_id :name, :use => :scoped, :scope => :type
end
class Ford < Car
end
@mariozig
mariozig / gist:1428970
Created December 4, 2011 02:54
Install the mysql2postgres gem
mario@red demo $ git clone https://github.com/maxlapshin/mysql2postgres.git
Cloning into mysql2postgres...
cd myremote: Counting objects: 516, done.
remote: Compressing objects: 100% (269/269), done.
remote: Total 516 (delta 262), reused 486 (delta 237)
Receiving objects: 100% (516/516), 84.68 KiB, done.
Resolving deltas: 100% (262/262), done.
mario@red demo $ cd mysql2postgres/
# Optional: I use RVM and wanted to use ruby 1.9.2
mario@red mysql2postgres $ rvm ruby-1.9.2-p290
@mariozig
mariozig / gist:1428996
Created December 4, 2011 03:06
Create config
# No Config File
mario@red mysql2postgres $ ls -la | grep yml
mario@red mysql2postgres $ mysql2psql
/Users/mario/.rvm/gems/ruby-1.9.2-p290/gems/mysql2psql-0.1.0/lib/mysql2psql/config.rb:14:in `initialize': (Mysql2psql::ConfigurationFileInitialized)
No configuration file found.
A new file has been initialized at: /Users/mario/ruby/demo/mysql2postgres/mysql2psql.yml
Please review the configuration and retry..
# Aw yeah, there she is
@mariozig
mariozig / gist:1429007
Created December 4, 2011 03:10
Do da damn thing
mario@red mysql2postgres $ mysql2psql
Table creation 0 min, loading 7 min, indexing 0 min, total 7 min