...du verbe Drupaler (1er groupe)

ruby

Ruby nmap script to scan IP range for wireless camera and push IP to web server

Please note: the content on this page orginates from ericlondon.com.
When our son was born, my wife and I purchased and setup an IP camera in his room. Unfortunately its IP changes periodically and it can be annoying to look it up. I finally gave in and wrote a network IP scan script to find its IP, and copy it to a file on my server in a public location.

First I used Homebrew to install nmap on my Mac:

Ruby API development: module/class structure to set instance variables via do/block

Please note: the content on this page orginates from ericlondon.com.
In this blog post, I'll show an example Ruby module/class structure to allow users to set configuration settings via a do/block, like:


Api.config do |c|

  c.first_name = 'Eric'
  c.last_name = 'London'

end


The following code shows how a simple module/class structure could be defined to allow end-users to set instance variables on a module's singleton class:

Ruby: check for zombie children processes, kill them, and spawn a new parent process

Please note: the content on this page orginates from ericlondon.com.
I've recently been working on phantomjs code that can time out and leave behind zombie children processes. Here's a method I created to check for child threads, kill the parent to remove child processes, and spawn a new instance of the parent process.


def kill_zombie_children

  # process id of current thread
  parent_pid = Process.pid

  # get a list of processes and children

BASH shell wrapper script to setup RVM environment and execute Ruby scripts via cron

Please note: the content on this page orginates from ericlondon.com.
Here's a BASH shell wrapper script I use to execute Ruby scripts via cron. It ensures the following are setup correctly: environment variables, Ruby version, RVM gemset, and will pass command line arguments to Ruby. It assumes an .rvmrc file exists in the same directory as the script being executed.


#!/usr/bin/env bash

# check if rvm_path var is empty and should be sourced

Daily cron job to check for outdated Ruby gems via RVM

Please note: the content on this page orginates from ericlondon.com.
I am running Apache + Passenger on a server which is set to use the global RVM gemset for all vhosts. Here's a simple cron script to check for gem updates.

Check which RVM gemset you are using:

$ rvm current
ruby-1.9.3-p392@global

# search through apache conf to find RVM environment path:
$ grep -i passenger /etc/apache2/apache2.conf

Fetching/parsing iTunes RSS feeds with Nokogiri, storing the data in MongoDB, and displaying price sparks via Sinatra

Please note: the content on this page orginates from ericlondon.com.
Recently I've been tracking price drops in iTunes apps, so I thought I'd roll my own feed parser in Ruby and share the code. In this blog post I'll demonstrate the following: MongoDB for document database storage, MongoID for the Ruby library, Curb for feed fetching, Nokogiri/Nori for XML parsing, Sinatra for a simple web server, and Google charts for a price spark image.

Installed MongoDB via Homebrew

Create an iOS iPad application with a Rails JSON API backend

Please note: the content on this page orginates from ericlondon.com.
In this tutorial, I'll show how I created an iOS app to make a JSON API request to a Rails app and show the content as images in the iPad simulator.

In my rails app model file, I defined an "as_json" method to format the JSON output:
# file: app/models/image.rb

def as_json(options={})
  {
    :thumb_path => self.upload.url(:thumb),

Ruby Selenium script to authenticate to Github and download all RailsCasts Pro videos

Please note: the content on this page orginates from ericlondon.com.
Here's a Ruby script I wrote that uses Selenium to authenticate and crawl railscasts.com, and Curb to download every RailsCasts Pro video. This assumes you have a paid RailsCasts account (http://railscasts.com/pro), dowit.


#!/usr/bin/env ruby

require 'selenium-webdriver'
require 'set'
require 'curb'

# define github credentials
github_email = 'YOUR GITHUB EMAIL ADDRESS'

Pages

Subscribe to ruby