This repository has been archived by the owner on Feb 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
products.rb
47 lines (43 loc) · 1.65 KB
/
products.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
require 'time'
module Bol
module Parsers
class Products < Parser
def xpath
'*/xmlns:Product'
end
def parse_object(el)
Product.new.tap do |product|
%w[id title subtitle type publisher short_description long_description ean rating binding_description language_code language_description].each do |field|
_field = camelize(field)
if el.at(_field)
product.attributes[field.to_sym] = el.at(_field).content.gsub(/\n\s+/, ' ').strip
end
end
product[:offers] = []
el.at('Offers').elements.each do |offer|
o = %w[id first_edition special_edition state price list_price availability_code availability_description comment second_hand seller].inject({}) do |output, field|
_field = camelize(field)
if offer.at(_field)
output[field] = offer.at(_field).content.gsub(/\n\s+/, ' ').strip
end
output
end
product[:offers] << OpenStruct.new(o)
end if el.at('Offers')
product[:url] = el.at('Urls').at('Main').content.strip
if el.at('ReleaseDate')
product[:release_date] = Time.parse(el.at('ReleaseDate').content)
end
product[:authors] = []
el.xpath('xmlns:Authors/xmlns:Author').each do |author|
product[:authors] << author.at('Name').content
end
product[:cover] = {}
el.xpath('xmlns:Images').children.each do |image|
product[:cover][underscorize(image.name).to_sym] = image.content.strip
end
end
end
end
end
end