Class: RBook::Onix::SupplyDetail

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#availability_codeObject

Returns the value of attribute availability_code.



8
9
10
# File 'lib/rbook/onix/supply_detail.rb', line 8

def availability_code
  @availability_code
end

#intermediary_availability_codeObject

Returns the value of attribute intermediary_availability_code.



8
9
10
# File 'lib/rbook/onix/supply_detail.rb', line 8

def intermediary_availability_code
  @intermediary_availability_code
end

#priceObject

Returns the value of attribute price.



8
9
10
# File 'lib/rbook/onix/supply_detail.rb', line 8

def price
  @price
end

#price_type_codeObject

Returns the value of attribute price_type_code.



8
9
10
# File 'lib/rbook/onix/supply_detail.rb', line 8

def price_type_code
  @price_type_code
end

#supplier_nameObject

Returns the value of attribute supplier_name.



8
9
10
# File 'lib/rbook/onix/supply_detail.rb', line 8

def supplier_name
  @supplier_name
end

Class Method Details

.load_from_element(element) ⇒ Object

Attempts to create a contributor object using the supplied xml element

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
# File 'lib/rbook/onix/supply_detail.rb', line 33

def self.load_from_element(element)
  raise ArgumentError, 'load_from_element expects a REXML element object' unless element.class == REXML::Element
  
  if REXML::XPath.first(element, '//SupplyDetail').nil? 
    raise LoadError, 'supplied REXML document object does not appear contain a valid SupplyDetail fragment'
  end
  
  self.load_from_string(element.to_s)
end

.load_from_string(str) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rbook/onix/supply_detail.rb', line 10

def self.load_from_string(str)
  doc = Hpricot::XML(str)
  supply_detail = SupplyDetail.new

  tmp = doc.search('//SupplyDetail/SupplierName')
  supply_detail.supplier_name = tmp.inner_html unless tmp.inner_html.blank?

  tmp = doc.search('//SupplyDetail/AvailabilityCode')
  supply_detail.availability_code = tmp.inner_html unless tmp.inner_html.blank?

  tmp = doc.search('//SupplyDetail/IntermediaryAvailabilityCode')
  supply_detail.intermediary_availability_code = tmp.inner_html unless tmp.inner_html.blank?
  
  tmp = doc.search('//SupplyDetail/Price/PriceAmount')
  supply_detail.price = BigDecimal(tmp.inner_html) unless tmp.inner_html.blank?

  tmp = doc.search('//SupplyDetail/Price/PriceTypeCode')
  supply_detail.price_type_code = tmp.inner_html.to_i unless tmp.inner_html.blank?

  return supply_detail
end

Instance Method Details

#to_elementObject



43
44
45
# File 'lib/rbook/onix/supply_detail.rb', line 43

def to_element
  REXML::Document.new(to_s).root
end

#to_sObject

Return an XML string representing this supply detail



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rbook/onix/supply_detail.rb', line 48

def to_s
  raise 'SupplyDetail must have a supplier name to create an element' if self.supplier_name.nil?
  raise 'SupplyDetail must have a availability code to create an element' if self.availability_code.nil?
  
  builder = Builder::XmlMarkup.new(:indent => 2)

  builder.SupplyDetail do |supply|
    supply.SupplierName self.supplier_name
    supply.AvailabilityCode self.availability_code unless self.availability_code.nil?
    supply.IntermediaryAvailabilityCode self.intermediary_availability_code unless self.intermediary_availability_code.nil?
    unless self.price.nil?
      tmp = REXML::Element.new('Price')
      supply.Price do |p|
        if self.price.kind_of?(BigDecimal)
          p.PriceAmount self.price.to_s("F")
        else
          p.PriceAmount self.price.to_s
        end
        if self.price_type_code.nil?
          p.PriceTypeCode "02"
        else
          p.PriceTypeCode self.price_type_code
        end
      end
    end
  end
end