class OzonParser::Page
Constants
- BASE_OZON_URL
< Hash include Hashie::Extensions::MethodAccess
Attributes
result[RW]
Public Class Methods
field(name, &block)
click to toggle source
# File lib/ozon_parser/page.rb, line 12 def self.field(name, &block) if block_given? attr_writer name.to_sym send :define_method, name.to_sym do unless instance_variable_defined?("@#{name}") value = instance_eval(&block) value = clean_up(value) if value.is_a?(String) instance_variable_set("@#{name}", value) end instance_variable_get("@#{name}") end else attr_accessor name.to_sym end end
new(options = {})
click to toggle source
@param [Object] options @option options [String] :source Сырой HTML в виде строки @option options [String] :url Урла
# File lib/ozon_parser/page.rb, line 31 def initialize(options = {}) @source = options.delete(:source) @url = options.delete(:url) end
Public Instance Methods
agent()
click to toggle source
# File lib/ozon_parser/page.rb, line 36 def agent options = { proxy: @proxy } @agent ||= OzonParser::Agent.new options end
clean_up(str)
click to toggle source
# File lib/ozon_parser/page.rb, line 65 def clean_up(str) str.strip.sub(/^\.{3}/, '').sub(/\,$/, '').gsub("\u00A0", "\u0020").strip end
file(file_url)
click to toggle source
# File lib/ozon_parser/page.rb, line 61 def file(file_url) agent.get_file file_url end
format_date(str)
click to toggle source
# File lib/ozon_parser/page.rb, line 73 def format_date(str) ['января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря'].each_with_index { |month, i| str = str.sub(month, (i + 1).to_s) } str.strip.gsub(/[^\d]+/, '-') end
json()
click to toggle source
# File lib/ozon_parser/page.rb, line 50 def json return source if source.is_a?(Hash) @json ||= MultiJson.decode source if source rescue Exception => e raise DecodeError, "#{e.to_s}, #{agent.history.inspect}", e.backtrace end
root()
click to toggle source
# File lib/ozon_parser/page.rb, line 57 def root @root ||= Nokogiri::HTML(source, 'utf-8') end
source()
click to toggle source
# File lib/ozon_parser/page.rb, line 44 def source @source ||= agent.get(@url) || throw('Define source or url... =(') rescue Exception => e raise ConnectionError, "#{e.to_s}, #{agent.history.inspect}", e.backtrace end
untypofy(str)
click to toggle source
# File lib/ozon_parser/page.rb, line 69 def untypofy(str) str.gsub("\u0097", "—") end