#!/usr/bin/ruby #filename: morsecode.rb require 'net/http' require 'rexml/document' include REXML class Morsecode def initialize @doc_mlookup = load_lookup('http://jamesrobertson.eu/p/xml/', 'morsecode.xml') @doc_charmcode = load_lookup('http://jamesrobertson.eu/p/xml/', 'charmcode.xml') end def load_lookup(url, filename) xml_data = Net::HTTP.get_response(URI.parse(url + filename)).body Document.new(xml_data) end def decode_string(doc, a_buffer) pstring = '' a_buffer.each do |word| pstring += doc.root.elements["code[@encode='" + word + "']"].text end pstring end def decode_kstring(buffer) decode_string(@doc_charmcode, buffer.scan(/./)) end def decode_mstring(buffer) decode_string(@doc_mlookup, buffer.split('4')) end def decode(buffer) decode_mstring(decode_kstring(buffer)) if buffer.length > 0 end end #test a = Morsecode.new puts a.decode('gwpwrwgw')
You need to create an account or log in to post comments to this site.