Class: Feed::ItemListGenerator

Inherits:
ItemBaseGenerator
  • Object
show all
Defined in:
app/services/feed/item_list_generator.rb

Instance Method Summary collapse

Instance Method Details

#append_xml_images(xml, images) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'app/services/feed/item_list_generator.rb', line 84

def append_xml_images(xml, images)
  xml.images do
    images.each_with_index do |img, index|
      xml.image do
        xml.index index
        xml.title img.title
        xml.url img.image_url
      end
    end
  end
end

#append_xml_item_specifications(xml, specifications) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/services/feed/item_list_generator.rb', line 68

def append_xml_item_specifications(xml, specifications)
  xml.specifications do
    specifications.each do |token, values|
      xml.specification do
        xml.token token
        xml.name values[:name]
        xml.units values[:units]
        xml.output values[:output]
        xml.raw values[:raw]
        xml.grouping values[:grouping]
        xml.visibility values[:visibility]
      end
    end
  end
end

#append_xml_items(xml, items) ⇒ Object



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
# File 'app/services/feed/item_list_generator.rb', line 31

def append_xml_items(xml, items)
  xml.items do
    items.each do |i|
      logger.info "Generating product data for item id : #{i.id}, #{i.sku}"
      xml.item do
        xml.id i.id
        xml.sku i.sku
        xml.gtin i.upc
        xml.country_of_origin i.coo
        xml.harmonization_code i.harmonization_code
        xml.name i.public_name
        xml.page_title i.effective_seo_title
        xml.keywords i.effective_seo_keywords
        xml.short_description i.short_description
        xml.description_text i.public_description_text
        xml.description_html i.public_description_html
        xml.product_line_lineage i.primary_product_line&.lineage_expanded
        xml.product_line i.primary_product_line&.display_name
        xml.product_category_lineage i.product_category&.lineage_expanded
        xml.google_product_category_id i.google_product_category_id
        xml.product_type i.product_type
        xml.item_group_id i.item_group_name
        xml.shipping do
          xml.tag! 'packaged_weight', i.shipping_weight, unit: 'lbs'
          xml.tag! 'packaged_height', i.shipping_height, unit: 'in'
          xml.tag! 'packaged_width', i.shipping_width, unit: 'in'
          xml.tag! 'packaged_length', i.shipping_length, unit: 'in'
        end
        append_xml_item_specifications(xml, i.public_specifications)
        append_xml_images(xml, i.all_images)
        append_xml_videos(xml, i.videos)
        append_xml_catalogs(xml, i)
      end
    end
  end
end

#append_xml_videos(xml, videos) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/services/feed/item_list_generator.rb', line 96

def append_xml_videos(xml, videos)
  xml.videos do
    videos.each_with_index do |vid, index|
      xml.video do
        xml.index index
        xml.title vid.title
        xml.thumbnail_url vid.thumbnail_url
        xml.url vid.cms_url
      end
    end
  end
end

#build_xml(xml, items) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/feed/item_list_generator.rb', line 17

def build_xml(xml, items)
  xml.instruct!
  xml.feed do
    xml.title 'Warmlyyours.com Product Feed'
    xml.version '0.1'
    xml.link href: 'https://www.warmlyyours.com', rel: 'alternate', type: 'text/html'
    xml.updated Time.current.strftime('%Y-%m-%dT%H:%M:%SZ')
    xml.author do
      xml.name 'Warmlyyours.com, Inc.'
    end
    append_xml_items(xml, items)
  end
end

#process(item_filters: nil, item_limits: nil, output_file_path: nil) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/services/feed/item_list_generator.rb', line 2

def process(item_filters: nil, item_limits: nil, output_file_path: nil)
  xml = Builder::XmlMarkup.new(indent: 2)
  items = load_items(item_filters)
  build_xml(xml, items)
  xml_data = xml.target!
  if output_file_path
    File.open(output_file_path, 'wb') do |file|
      file.write xml_data
      file.flush
      file.fsync
    end
  end
  Result.new(output: xml_data, output_file_path:, item_count: items.size)
end