Start to doc modem class and put connectio method to private

master
Rodrigo Pinto 2012-05-18 17:35:45 -03:00
parent ff47ed219b
commit 1d4ce23ddb
1 changed files with 15 additions and 5 deletions

View File

@ -3,6 +3,11 @@ require_relative 'sms'
module Biju
class Modem
# Receives a hash of options where :port is mandatory
#
# Biju::Modem.new(:port => '/dev/ttyUSB0')
#
def initialize(options={})
raise Exception.new("Port is required") unless options[:port]
@connection = connection(options)
@ -10,15 +15,14 @@ module Biju
cmd("AT+CMGF=1")
end
def connection(options)
port = options.delete(:port)
SerialPort.new(port, default_options.merge!(options))
end
# Close the serial port connection.
def close
@connection.close
end
# Returns an Array of Sms if there is messages,
# if not other way return nil
#
def messages
sms = cmd("AT+CMGL=\"ALL\"")
msgs = sms.scan(/\+CMGL\:\s*?(\d+)\,.*?\,\"(.+?)\"\,.*?\,\"(.+?)\".*?\n(.*)/)
@ -27,6 +31,12 @@ module Biju
end
private
def connection(options)
port = options.delete(:port)
SerialPort.new(port, default_options.merge!(options))
end
# private method that generate default values for serial port connection
def default_options
{ :baud => 9600, :data_bits => 8, :stop_bits => 1, :parity => SerialPort::NONE }
end