Class: YmlBuilder::CommonOffer

Inherits:
Object
  • Object
show all
Defined in:
lib/yml_builder/common_offer.rb

Direct Known Subclasses

OfferSimple, OfferVendorModel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommonOffer



23
24
25
# File 'lib/yml_builder/common_offer.rb', line 23

def initialize
  init_class
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object (private)



90
91
92
93
94
95
96
# File 'lib/yml_builder/common_offer.rb', line 90

def method_missing(method_sym, *arguments, &block)
  if @params.include?(method_sym.to_s.gsub(/=$/, '').to_sym)
    processing_method(method_sym, arguments.first)
  else
    super
  end
end

Instance Attribute Details

#availableObject

Наличие товара на складе

Examples:

Примеры использования

offer = YmlBuilder::Offer.new('simple')
puts offer.available = true


17
18
19
# File 'lib/yml_builder/common_offer.rb', line 17

def available
  @available
end

#bidObject

Returns the value of attribute bid.



18
19
20
# File 'lib/yml_builder/common_offer.rb', line 18

def bid
  @bid
end

#idObject

Уникальный идентификатор товара

Examples:

Примеры использования

offer = YmlBuilder::Offer.new('simple')
offer.id = 10


7
8
9
# File 'lib/yml_builder/common_offer.rb', line 7

def id
  @id
end

#mandatoriesObject

Список обязательных полей для данного типа оффера



21
22
23
# File 'lib/yml_builder/common_offer.rb', line 21

def mandatories
  @mandatories
end

#typeObject

Уникальный идентификатор типа товара, в соответствии с классификацией Yandex.MArket. Устанавливается в конструкторе

Examples:

Примеры использования

offer = YmlBuilder::Offer.new('simple')
puts offer.type                             #=> 'simple'


12
13
14
# File 'lib/yml_builder/common_offer.rb', line 12

def type
  @type
end

Instance Method Details

#add_cover_picture(url) ⇒ Object

Метод добавляет ссылку на фотографию товара в начало списка, и ограничивает список 10-ю фотографиями

Examples:

Примеры использования

offer = YmlBuilder::Offer.new('simple')
offer.add_cover_picture('http://example-site.ru/cover_image1.jpg')


44
45
46
47
48
49
# File 'lib/yml_builder/common_offer.rb', line 44

def add_cover_picture(url)
  @picture.unshift(url)
  @picture.uniq!
  warn "Предупреждение: число картинок превышает 10 (offer_id=#{@id}). Сокращаем до 10" if @picture.count > 10
  @picture = @picture[0,9]
end

#add_param(name:, unit: nil, value:) ⇒ Object

Метод добавляет характеристики товара (для секции ‘param’)

Examples:

Примеры использования

offer = YmlBuilder::Offer.new('simple')
offer.add_param(name: "Количество", unit: "шт.", value: 100)
offer.add_param(name: "Обложка", value: "мягкая")


60
61
62
# File 'lib/yml_builder/common_offer.rb', line 60

def add_param(name:, unit: nil, value:)
  @meta[name] = { unit: unit, value: value}
end

#add_picture(url) ⇒ Object

Метод добавляет ссылку на фотографию товара в конец списка, и ограничивает список 10-ю фотографиями

Examples:

Примеры использования

offer = YmlBuilder::Offer.new('simple')
offer.add_picture('http://example-site.ru/image1.jpg')


32
33
34
35
36
37
# File 'lib/yml_builder/common_offer.rb', line 32

def add_picture(url)
  @picture << url
  @picture.uniq!
  warn "Предупреждение: число картинок превышает 10 (offer_id=#{@id}). Сокращаем до 10" if @picture.count > 10
  @picture = @picture[0,9]
end

#to_yml(ident = 4) ⇒ String

Метод формирует фрагмент YML файла каталога Яндекс.Маркет для одного товара



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/yml_builder/common_offer.rb', line 68

def to_yml(ident = 4)
  out = Array.new
  out << header_line

  @params.each do |key, value|
    if [:picture, :param].include?(key)
      out += to_yml_subsections(key)
    elsif @mandatories.include?(key)
      out << to_yml_mandatories(key, value)
    else
      out << to_yml_optional(key, value)
    end
  end
  out.compact!

  out << footer_line
  out.map! { |line| ' '.rjust(ident, ' ') + line }
  out.join("\n")
end