Class: RBook::Onix::Message
- Inherits:
-
Object
- Object
- RBook::Onix::Message
- Defined in:
- lib/rbook/onix/message.rb
Constant Summary collapse
- VERSION =
0.1
- ONIX_DTD_URL =
"http://www.editeur.org/onix/2.1/reference/onix-international.dtd"
Instance Attribute Summary collapse
-
#from_company ⇒ Object
Returns the value of attribute from_company.
-
#from_email ⇒ Object
Returns the value of attribute from_email.
-
#from_person ⇒ Object
Returns the value of attribute from_person.
-
#message_note ⇒ Object
Returns the value of attribute message_note.
-
#products ⇒ Object
readonly
Returns the value of attribute products.
-
#sent_date ⇒ Object
Returns the value of attribute sent_date.
-
#to_company ⇒ Object
Returns the value of attribute to_company.
-
#to_person ⇒ Object
Returns the value of attribute to_person.
Class Method Summary collapse
- .check_ruby_version ⇒ Object
-
.load_from_string(str) ⇒ Object
Attempts to create a message object using thge supplied xml string.
-
.load_from_xmldoc(doc) ⇒ Object
Attempts to create a message object using thge supplied xml document.
Instance Method Summary collapse
-
#add_product(product) ⇒ Object
Adds a new product to this message.
- #header ⇒ Object
-
#initialize ⇒ Message
constructor
A new instance of Message.
-
#to_s ⇒ Object
Returns an XML string representing this message.
- #to_xml ⇒ Object
Constructor Details
#initialize ⇒ Message
Returns a new instance of Message.
14 15 16 |
# File 'lib/rbook/onix/message.rb', line 14 def initialize @products = [] end |
Instance Attribute Details
#from_company ⇒ Object
Returns the value of attribute from_company.
9 10 11 |
# File 'lib/rbook/onix/message.rb', line 9 def from_company @from_company end |
#from_email ⇒ Object
Returns the value of attribute from_email.
9 10 11 |
# File 'lib/rbook/onix/message.rb', line 9 def from_email @from_email end |
#from_person ⇒ Object
Returns the value of attribute from_person.
9 10 11 |
# File 'lib/rbook/onix/message.rb', line 9 def from_person @from_person end |
#message_note ⇒ Object
Returns the value of attribute message_note.
9 10 11 |
# File 'lib/rbook/onix/message.rb', line 9 def @message_note end |
#products ⇒ Object (readonly)
Returns the value of attribute products.
8 9 10 |
# File 'lib/rbook/onix/message.rb', line 8 def products @products end |
#sent_date ⇒ Object
Returns the value of attribute sent_date.
9 10 11 |
# File 'lib/rbook/onix/message.rb', line 9 def sent_date @sent_date end |
#to_company ⇒ Object
Returns the value of attribute to_company.
9 10 11 |
# File 'lib/rbook/onix/message.rb', line 9 def to_company @to_company end |
#to_person ⇒ Object
Returns the value of attribute to_person.
9 10 11 |
# File 'lib/rbook/onix/message.rb', line 9 def to_person @to_person end |
Class Method Details
.check_ruby_version ⇒ Object
18 19 20 |
# File 'lib/rbook/onix/message.rb', line 18 def self.check_ruby_version raise InvalidRubyVersionError, 'Ruby 1.8.6 or higher is required to use this class' unless RUBY_VERSION >= "1.8.6" end |
.load_from_string(str) ⇒ Object
Attempts to create a message object using thge supplied xml string
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rbook/onix/message.rb', line 23 def self.load_from_string(str) self.check_ruby_version # attempt to pass the string through REXML, which will auto convert it # to UTF-8 for us. If the file is invalid XML, then still attempt to use # HPricot to parse it, as some data may be extractable begin rexml_doc = REXML::Document.new(str) rescue REXML::ParseException # do nothin end if rexml_doc doc = Hpricot::XML(rexml_doc.to_s) else doc = Hpricot::XML(str) end msg = Onix::Message.new tmp = doc.search('/ONIXMessage/Header/FromCompany') msg.from_company = tmp.inner_html unless tmp.inner_html.blank? tmp = doc.search('/ONIXMessage/Header/FromPerson') msg.from_person = tmp.inner_html unless tmp.inner_html.blank? tmp = doc.search('/ONIXMessage/Header/FromEmail') msg.from_email = tmp.inner_html unless tmp.inner_html.blank? tmp = doc.search('/ONIXMessage/Header/SentDate') if !tmp.inner_html.blank? && tmp.inner_html.length == 8 tmpdate = Date.civil(tmp.inner_html[0,4].to_i, tmp.inner_html[4,2].to_i, tmp.inner_html[6,2].to_i) msg.sent_date = tmpdate unless tmpdate.nil? end tmp = doc.search('/ONIXMessage/Header/ToCompany') msg.to_company = tmp.inner_html unless tmp.inner_html.blank? tmp = doc.search('/ONIXMessage/Header/ToPerson') msg.to_person = tmp.inner_html unless tmp.inner_html.blank? doc.search('/ONIXMessage/Product').each do |product| msg.add_product(Onix::Product.load_from_string("<Product>#{product}</Product>")) end return msg end |
.load_from_xmldoc(doc) ⇒ Object
Attempts to create a message object using thge supplied xml document
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rbook/onix/message.rb', line 72 def self.load_from_xmldoc(doc) self.check_ruby_version raise ArgumentError, 'load_from_xmldoc expects a REXML document object' unless doc.class == REXML::Document if REXML::XPath.first(doc, '/ONIXMessage/').nil? || REXML::XPath.first(doc, '/ONIXMessage/Header').nil? || REXML::XPath.first(doc, '/ONIXMessage/Product').nil? raise LoadError, 'supplied REXML document object does not appear to be a valid ONIX file' end self.load_from_string(doc.to_s) end |
Instance Method Details
#add_product(product) ⇒ Object
Adds a new product to this message
85 86 87 88 |
# File 'lib/rbook/onix/message.rb', line 85 def add_product(product) raise ArgumentError, 'Argument must be a Onix::Product' if product.class != Onix::Product @products << product end |
#header ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rbook/onix/message.rb', line 111 def header builder = Builder::XmlMarkup.new(:indent => 2) builder.Header do |h| h.FromCompany @from_company if @from_company h.FromPerson @from_person if @from_person h.FromEmail @from_email if @from_email h. @message_note if @message_note h.SentDate @sent_date if @sent_date h.ToPerson @to_person if @to_person h.ToCompany @to_company if @to_company end end |
#to_s ⇒ Object
Returns an XML string representing this message
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rbook/onix/message.rb', line 91 def to_s raise 'from_compnay must be set to create an onix message' if from_company == nil raise 'from_person must be set to create an onix message' if from_person == nil raise 'at least one product must be added create an onix message' if @products.size == 0 builder = Builder::XmlMarkup.new(:indent => 2) builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8" builder.declare! :DOCTYPE, :ONIXMessage, :SYSTEM, "http://www.editeur.org/onix/2.1/reference/onix-international.dtd" builder.ONIXMessage do |msg| msg << self.header @products.each do |product| msg << product.to_s end end end |
#to_xml ⇒ Object
107 108 109 |
# File 'lib/rbook/onix/message.rb', line 107 def to_xml REXML::Document.new(to_s) end |