Class: RBook::Onix::Listener

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

Overview

a utility class for the ONIX Stream Reader. See RBook::ONIX::StreamReader for basic usage instructions.

Instance Method Summary collapse

Constructor Details

#initialize(queue) ⇒ Listener

Returns a new instance of Listener.



12
13
14
15
# File 'lib/rbook/onix/stream_reader.rb', line 12

def initialize(queue)
  @queue = queue
  @in_product = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



77
78
79
# File 'lib/rbook/onix/stream_reader.rb', line 77

def method_missing(*args)
  # do nothing
end

Instance Method Details

#doctype(name, pub_sys, long_name, uri) ⇒ Object



17
18
19
# File 'lib/rbook/onix/stream_reader.rb', line 17

def doctype(name, pub_sys, long_name, uri)   
  # do nothing
end

#end_documentObject



70
71
72
73
74
75
# File 'lib/rbook/onix/stream_reader.rb', line 70

def end_document
  # signal to the thread reading products off the queue that there are none left.
  # this event curently isn't supported by rexml, so if an ONIX file is truncated
  # and is missing </ONIXMessage>, it will hang. Patch submitted upstream
  @queue.push(nil)
end

#instruction(name, instruction) ⇒ Object



21
22
23
# File 'lib/rbook/onix/stream_reader.rb', line 21

def instruction(name, instruction)
  # do nothing
end

#tag_end(name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rbook/onix/stream_reader.rb', line 41

def tag_end(name)
  case name
  when "ONIXMessage"
    # the ONIXMessage tag indicates the end of the file, so add
    # nil to the queue to let the iterating thread know reading
    # is finished
    @queue.push(nil)
  when "Product"
    # A product tag is finished, so add it to the queue
    @product_fragment << "</Product>"
    begin
      product = RBook::Onix::Product.load_from_string(@product_fragment)
      @queue.push(product) unless product.nil?
    rescue Exception => e
      # error occurred while building the product from an XML fragment
      # pop the error on the queue so it can be raised by the thread
      # reading items off the queue
      @queue.push(e)
    end
    @in_product = false
  else
    @product_fragment << "</#{name}>" if @in_product
  end
end

#tag_start(name, attrs) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rbook/onix/stream_reader.rb', line 25

def tag_start(name, attrs)
  case name
  when "Product"
    # A new product tag has been read, so start
    # building a new product to add to the queue
    @in_product = true
    @product_fragment = "<Product>"
  else
    @product_fragment << "<#{name}>" if @in_product
  end
end

#text(text) ⇒ Object



37
38
39
# File 'lib/rbook/onix/stream_reader.rb', line 37

def text(text)
  @product_fragment << text.to_xs if @in_product
end

#xmldecl(version, encoding, standalone) ⇒ Object



66
67
68
# File 'lib/rbook/onix/stream_reader.rb', line 66

def xmldecl(version, encoding, standalone)
  # do nothing
end