class Drom::ListingParser

Attributes

parsed[R]
url[R]

Public Class Methods

new(url, page) click to toggle source
# File lib/drom/listing_parser.rb, line 5
def initialize(url, page)
  @url = url
  @page = page
  @parsed = {}
  parse_sections
end

Private Instance Methods

format_key(key) click to toggle source
# File lib/drom/listing_parser.rb, line 13
def format_key(key)
  key.gsub("\u00A0", "").delete(":")
end
format_value(value) click to toggle source
# File lib/drom/listing_parser.rb, line 17
def format_value(value)
  value.strip.gsub("\u00A0", "")
end
parse_sections() click to toggle source
# File lib/drom/listing_parser.rb, line 21
def parse_sections
  begin
    @parsed["Ссылка"] = @url

    @page.search("span[data-section = 'auto-description'] .b-text-gray").each do |e|
      @parsed[format_key(e.children.text)] = format_value(e.next_sibling.text)
    end

    @page.search("span[data-section = 'auto-description'] p").each do |e|
      k, v = e.text.split(":")
      @parsed[format_key(k)] = format_value(v)
    end

    @parsed["Мощность"] = format_value(/(\w+ л.с)/.match(@parsed["Мощность"])[0])
    @parsed["Фото"] = @page.search(".b-advItemGallery__thumbs .b-advItemGallery__photo").map { |e| e["href"] }
    @parsed["Цена"] = format_value(@page.search("div.b-media-cont.b-media-cont_theme_dot").text.strip.delete("руб."))
  rescue NoMethodError
    # ...
  end
end