Class: RBook::Onix::StreamWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/rbook/onix/stream_writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(output, msg) ⇒ StreamWriter

Default constructor.

Raises:

  • (ArgumentError)


9
10
11
12
13
# File 'lib/rbook/onix/stream_writer.rb', line 9

def initialize(output, msg)
  raise ArgumentError, 'msg must be an RBook::Onix::Message object' unless msg.class == RBook::Onix::Message
  @output = output
  @msg = msg
end

Instance Method Details

#<<(product) ⇒ Object



29
30
31
# File 'lib/rbook/onix/stream_writer.rb', line 29

def << (product)
  @output.write(product.to_s)
end

#end_documentObject



33
34
35
# File 'lib/rbook/onix/stream_writer.rb', line 33

def end_document
  @output.write("</ONIXMessage>\n")
end

#start_document(encoding = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rbook/onix/stream_writer.rb', line 15

def start_document(encoding = nil)
  decl = REXML::XMLDecl.new
  doctype = REXML::DocType.new('ONIXMessage', "SYSTEM \"#{RBook::Onix::Message::ONIX_DTD_URL}\"")
  if encoding
    decl.encoding = encoding
  else
    decl.encoding = "UTF-8"
  end
  @output.write(decl.to_s+"\n")
  @output.write(doctype.to_s+"\n")
  @output.write("<ONIXMessage>\n")
  @output.write(@msg.header.to_s)
end