class UniParser::Page

Attributes

result[RW]

Public Class Methods

field(name, &block) click to toggle source
# File lib/uni_parser/page.rb, line 7
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/uni_parser/page.rb, line 26
def initialize(options = {})
  @source = options.delete(:source)
  @url = options.delete(:url)
end

Public Instance Methods

agent() click to toggle source
# File lib/uni_parser/page.rb, line 31
def agent
  options = {
    proxy: @proxy
  }

  @agent ||= UniParser::Agent.new options
end
clean_up(str) click to toggle source
# File lib/uni_parser/page.rb, line 60
def clean_up(str)
  str.strip.sub(/^\.{3}/, '').sub(/\,$/, '').gsub("\u00A0", "\u0020").strip
end
file(file_url) click to toggle source
# File lib/uni_parser/page.rb, line 56
def file(file_url)
  agent.get_file file_url
end
format_date(str) click to toggle source
# File lib/uni_parser/page.rb, line 64
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/uni_parser/page.rb, line 45
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/uni_parser/page.rb, line 52
def root
  @root ||= Nokogiri::HTML(source, 'utf-8')
end
source() click to toggle source
# File lib/uni_parser/page.rb, line 39
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