Class: OzonParser::Page
- Inherits:
-
Object
- Object
- OzonParser::Page
- Defined in:
- lib/ozon_parser/page.rb
Direct Known Subclasses
Constant Summary collapse
- BASE_OZON_URL =
include Hashie::Extensions::MethodAccess
'http://www.ozon.ru'
Instance Attribute Summary collapse
-
#result ⇒ Object
Returns the value of attribute result.
Class Method Summary collapse
Instance Method Summary collapse
- #agent ⇒ Object
- #clean_up(str) ⇒ Object
- #file(file_url) ⇒ Object
- #format_date(str) ⇒ Object
-
#initialize(options = {}) ⇒ Page
constructor
A new instance of Page.
- #json ⇒ Object
- #root ⇒ Object
- #source ⇒ Object
- #untypofy(str) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Page
Returns a new instance of Page.
31 32 33 34 |
# File 'lib/ozon_parser/page.rb', line 31 def initialize( = {}) @source = .delete(:source) @url = .delete(:url) end |
Instance Attribute Details
#result ⇒ Object
Returns the value of attribute result.
10 11 12 |
# File 'lib/ozon_parser/page.rb', line 10 def result @result end |
Class Method Details
.field(name, &block) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# 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 |
Instance Method Details
#agent ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/ozon_parser/page.rb', line 36 def agent = { proxy: @proxy } @agent ||= OzonParser::Agent.new end |
#clean_up(str) ⇒ Object
65 66 67 |
# 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) ⇒ Object
61 62 63 |
# File 'lib/ozon_parser/page.rb', line 61 def file(file_url) agent.get_file file_url end |
#format_date(str) ⇒ Object
73 74 75 76 77 78 |
# 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 ⇒ Object
50 51 52 53 54 55 |
# 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 ⇒ Object
57 58 59 |
# File 'lib/ozon_parser/page.rb', line 57 def root @root ||= Nokogiri::HTML(source, 'utf-8') end |
#source ⇒ Object
44 45 46 47 48 |
# 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) ⇒ Object
69 70 71 |
# File 'lib/ozon_parser/page.rb', line 69 def untypofy(str) str.gsub("\u0097", "—") end |