Sha256: 641f1cb2c4695003faf864c65e9c486803163f4cd4b77751ce714a9ee6d596a8

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

#!/usr/bin/env ruby
# encoding: UTF-8
#
#=======================================================================================
#
# FILE: rae.rb
#
#
# DESCRIPTION: Libreríde acceso a la drae (diccionario de la real academia españ)
# desde ruby
#
# OPTIONS: [palabra]
# REQUIREMENTS: nokogiri, mechanize
# AUTHOR: Koldo Oteo (), koldo.oteo1@gmail.com
# WEB: http://koteo.lacoctelera.net
# VERSION: 1.1.2
# CREATED: 10/03/2010
# AGRADECIMIENTOS: A krawek de freenode.
#=======================================================================================
require 'rubygems'
require 'nokogiri'
require 'mechanize'


class Rae

  def pide_pal
    @pal = ARGV.first
    if !@pal
      puts "Programa para acceder al diccionario de la rae desde ruby."
      puts
      puts "Uso: rae [palabra]"
      puts "La palabra se debe escribir con acentos y con exactitud, para buscar en la rae sin problemas."
      puts
      puts "Reportar bugs a <koldo.oteo1@gmail.com>."
      puts
      exit
    end
  end

  def asig_proxy
    if ENV['http_proxy'] != nil then
      #ENV['http_proxy'].scan(/(^http|https) \:\/\/ (\d+.\d+.\d+.\d+|\w+.|\w+.\.\w+) \: (\d.+)/xi)
      @host, @port = ENV['http_proxy'].to_s.sub(%r{^\w+://}, "").split(":", 2)
    end
  end

  def accede_form
    @agent = Mechanize.new

    asig_proxy
    if @host != nil
      @agent.set_proxy("#{@host}", "#{@port}")
    end

    @agent.user_agent_alias = 'Mac Safari'
    @page = @agent.get 'http://buscon.rae.es/draeI/html/cabecera.htm'

    @form = @page.forms.first
    @form['TIPO_BUS'] = '3'
    @form['LEMA'] = @pal

    @page = @agent.submit(@form, @form.buttons.first)

  end

  def format
    @read_doc = Nokogiri::HTML(@page.body)
    @read_doc.css('p').each do |l|
      puts l.content.to_s.strip
    end
  end
end


ra = Rae.new

ra.pide_pal
ra.accede_form
ra.format

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rae-1.1.2 bin/rae