From eead60819b6574ae4620f01078b8f7c1d71ad057 Mon Sep 17 00:00:00 2001 From: Jordan Prince Date: Tue, 5 Mar 2013 16:15:43 -0600 Subject: [PATCH] Added event machine compatibility by adding use_em option to opts. --- Gemfile | 3 +++ lib/google_directions.rb | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index a1b93f3..565b730 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,6 @@ source :rubygems +gem 'em-http-request' +gem 'em-synchrony' + gemspec diff --git a/lib/google_directions.rb b/lib/google_directions.rb index 12fa114..acbf596 100644 --- a/lib/google_directions.rb +++ b/lib/google_directions.rb @@ -1,7 +1,8 @@ # encoding: UTF-8 require 'cgi' require 'net/http' -require 'open-uri' +require 'em-http-request' +require 'em-synchrony' require 'nokogiri' require 'extlib/hash' @@ -16,6 +17,7 @@ class GoogleDirections :alternative => :true, :sensor => :false, :mode => :driving, + :use_em => false } def initialize(origin, destination, opts=@@default_options) @@ -24,7 +26,16 @@ def initialize(origin, destination, opts=@@default_options) @options = opts.merge({:origin => transcribe(@origin), :destination => transcribe(@destination)}) @url = @@base_url + '?' + @options.to_params - @xml = open(@url).read + + if @options[:use_em] + HTTPI.adapter = :em_http + client = HTTPI + req = HTTPI::Request.new(@url) + @xml = client.request(:get, req).body.to_s + else + @xml = open(@url).read + end + @doc = Nokogiri::XML(@xml) @status = @doc.css('status').text end