Class: RBook::Onix::Product

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProduct

Returns a new instance of Product.



15
16
17
18
19
# File 'lib/rbook/onix/product.rb', line 15

def initialize
  @contributors = []
  @supply_details = []
  @sales_restrictions = []
end

Instance Attribute Details

#bicmainsubjectObject

Returns the value of attribute bicmainsubject.



11
12
13
# File 'lib/rbook/onix/product.rb', line 11

def bicmainsubject
  @bicmainsubject
end

#contributorsObject (readonly)

Returns the value of attribute contributors.



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

def contributors
  @contributors
end

#cover_imageObject

Returns the value of attribute cover_image.



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

def cover_image
  @cover_image
end

#descriptionObject

Returns the value of attribute description.



11
12
13
# File 'lib/rbook/onix/product.rb', line 11

def description
  @description
end

#editionObject

Returns the value of attribute edition.



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

def edition
  @edition
end

#formObject

Returns the value of attribute form.



11
12
13
# File 'lib/rbook/onix/product.rb', line 11

def form
  @form
end

#notification_typeObject

Returns the value of attribute notification_type.



9
10
11
# File 'lib/rbook/onix/product.rb', line 9

def notification_type
  @notification_type
end

#pagesObject

Returns the value of attribute pages.



10
11
12
# File 'lib/rbook/onix/product.rb', line 10

def pages
  @pages
end

#priceObject

Returns the value of attribute price.



10
11
12
# File 'lib/rbook/onix/product.rb', line 10

def price
  @price
end

#product_identifierObject

Returns the value of attribute product_identifier.



9
10
11
# File 'lib/rbook/onix/product.rb', line 9

def product_identifier
  @product_identifier
end

#publication_dateObject

Returns the value of attribute publication_date.



10
11
12
# File 'lib/rbook/onix/product.rb', line 10

def publication_date
  @publication_date
end

#publisherObject

Returns the value of attribute publisher.



11
12
13
# File 'lib/rbook/onix/product.rb', line 11

def publisher
  @publisher
end

#record_referenceObject

Returns the value of attribute record_reference.



9
10
11
# File 'lib/rbook/onix/product.rb', line 9

def record_reference
  @record_reference
end

#sales_restrictionsObject (readonly)

Returns the value of attribute sales_restrictions.



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

def sales_restrictions
  @sales_restrictions
end

#subtitleObject

Returns the value of attribute subtitle.



10
11
12
# File 'lib/rbook/onix/product.rb', line 10

def subtitle
  @subtitle
end

#supplierObject

Returns the value of attribute supplier.



11
12
13
# File 'lib/rbook/onix/product.rb', line 11

def supplier
  @supplier
end

#supply_detailsObject (readonly)

Returns the value of attribute supply_details.



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

def supply_details
  @supply_details
end

#titleObject

Returns the value of attribute title.



10
11
12
# File 'lib/rbook/onix/product.rb', line 10

def title
  @title
end

#websiteObject

Returns the value of attribute website.



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

def website
  @website
end

Class Method Details

.load_from_element(element) ⇒ Object

Attempts to create a product object using the supplied xml element WARNING: Deprecated method

Raises:

  • (ArgumentError)


93
94
95
96
97
98
99
100
101
# File 'lib/rbook/onix/product.rb', line 93

def Product.load_from_element(element)
  raise ArgumentError, 'load_from_element expects a REXML element object' unless element.class == REXML::Element

  if REXML::XPath.first(element, '//Product').nil? 
    raise LoadError, 'supplied REXML document object does not appear contain a valid product fragment'
  end

  self.load_from_string(element.to_s)
end

.load_from_string(str) ⇒ Object

Attempts to create a product object using the supplied xml fragment



22
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rbook/onix/product.rb', line 22

def Product.load_from_string(str)
  doc = Hpricot::XML(str)
  
  product = Product.new

  tmp = doc.search("//Product/recordreference")
  product.record_reference = tmp.inner_html unless tmp.inner_html.blank?

  tmp = doc.search("//Product/notificationtype")
  product.notification_type = tmp.inner_html unless tmp.inner_html.blank?

  tmp = doc.search("//Product/productidentifier/idvalue").first
  product.product_identifier = tmp.inner_html  unless tmp.inner_html.blank?

  tmp = doc.search("//Product/productform")
  product.form = tmp.inner_html unless tmp.inner_html.blank?

  tmp = doc.search("//Product/numberofpages")
  product.pages = tmp.inner_html unless tmp.inner_html.blank?

  tmp = doc.search("//Product/bicmainsubject")
  product.bicmainsubject = tmp.inner_html unless tmp.inner_html.blank?

  doc.search("//Product/mediafile").each do |mediafile|
    type = doc.search('//Product/mediafile/mediafiletypecode')
    # cover image
    if type.inner_html.eql?("04")
      url = doc.search('//Product/mediafile/mediafilelink')
      product.cover_image = url.inner_html unless url.inner_html.blank?
    end
  end

  tmp = doc.search("//Product/distinctivetitle")
  if !tmp.inner_html.blank?
    product.title = tmp.inner_html
    tmp = doc.search('//Product/subtitle')
    product.subtitle = tmp.inner_html unless tmp.inner_html.blank?
  else
    tmp = doc.search('//Product/title/titletext')
    product.title = tmp.inner_html unless tmp.inner_html.blank?
    tmp = doc.search('//Product/title/subtitle')
    product.subtitle = tmp.inner_html unless tmp.inner_html.blank?
  end

  tmp = doc.search('//Product/editionnumber')
  product.edition = tmp.inner_html unless tmp.inner_html.blank?

  tmp = doc.search('//Product/publicationdate')

  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)
    product.publication_date = tmpdate unless tmpdate.nil?
  end

  tmp = doc.search('//Product/publisher/publishername')
  product.publisher = tmp.inner_html unless tmp.inner_html.blank?

  doc.search('//Product/contributor').each do |contributor|
    product.add_contributor(Onix::Contributor.load_from_string("<contributor>#{contributor}</contributor>"))
  end

  doc.search('//Product/supplydetail').each do |detail|
    product.add_supply_detail(Onix::SupplyDetail.load_from_string("<SupplyDetail>#{detail}</SupplyDetail>"
    ))
  end

  return product
end

Instance Method Details

#add_contributor(newAuthor) ⇒ Object

Add a new contributor to this product

Raises:

  • (ArgumentError)


104
105
106
107
# File 'lib/rbook/onix/product.rb', line 104

def add_contributor(newAuthor)
  raise ArgumentError, 'Argument to add_contributor must be an ONIX::Contributor' if newAuthor.class != Contributor
  @contributors << newAuthor
end

#add_sales_restriction(restriction) ⇒ Object

Add a new contributor to this product

Raises:

  • (ArgumentError)


110
111
112
113
# File 'lib/rbook/onix/product.rb', line 110

def add_sales_restriction(restriction)
  raise ArgumentError, 'Argument to add_sales_restriction must be an ONIX::SalesRestriction' if restriction.class != SalesRestriction
  @sales_restrictions << restriction
end

#add_supply_detail(detail) ⇒ Object

Add a new contributor to this product

Raises:

  • (ArgumentError)


116
117
118
119
# File 'lib/rbook/onix/product.rb', line 116

def add_supply_detail(detail)
  raise ArgumentError, 'Argument to add_supply_detail must be an ONIX::SupplyDetail' if detail.class != SupplyDetail
  @supply_details << detail
end

#to_elementObject



213
214
215
# File 'lib/rbook/onix/product.rb', line 213

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

#to_sObject

return an XML string representing this contributor



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/rbook/onix/product.rb', line 122

def to_s
  raise 'product_identifier must be set' if self.product_identifier.nil?

  builder = Builder::XmlMarkup.new(:indent => 2)
  builder.Product do |p|

  if @record_reference
    p.RecordReference self.record_reference
  else
    p.RecordReference self.product_identifier
  end

  p.NotificationType '03'

  p.ProductIdentifier do |id|
    if ISBN::valid_isbn13?(self.product_identifier)
      p.ProductIDType '03'
    elsif ISBN::valid_isbn10?(self.product_identifier)
      p.ProductIDType '02'
    else
      p.ProductIDType '01'
    end
    p.IDValue self.product_identifier
  end

  p.ProductForm self.form

  p.NoSeries

  title = REXML::Element.new('Title')
  p.Title do |t|
    t.TitleType '01'
    t.TitleText self.title   unless self.title.nil?
    t.Subtitle self.subtitle unless self.subtitle.nil?
  end

  if website != nil
    p.Website do |w|
      w.WebsiteLink self.website
    end
  end

  unless cover_image.nil?
    p.MediaFile do |m|
      m.MediaFileTypeCode "04"
      m.MediaFileLinkTypeCode "01"
      m.MediaFileLink self.cover_image
    end
  end

  contributors.each do |contributor|
    p << contributor.to_s
  end

  p.NoEdition

  p.NumberOfPages self.pages.to_s unless self.pages.nil?

  if bicmainsubject != nil
    p.BICMainSubject = self.bicmainsubject
  end

  if description != nil
    p.OtherText do |t|
      t.TextTypeCode '01'
      t.Text self.description
    end
  end

  if publisher != nil
    p.Publisher do |pub|
      pub.PublishingRole '01'
      pub.PublisherName self.publisher
    end
  end

  supply_details.each do |detail|
    p << detail.to_s
  end

  sales_restrictions.each do |restriction|
    p << restriction.to_s
  end

  if publication_date != nil
    p.PublicationDate("%.4d%.2d%.2d" % [ publication_date.year, publication_date.month, publication_date.day ])
  end

  end
end