class OzonParser::Agent

Constants

AGENT_ALIASES

Attributes

current_proxy[R]
options[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/ozon_parser/agent.rb, line 10
def initialize(options = {})
  @agent = Mechanize.new { |agent|
    agent.open_timeout = OzonParser.config.open_timeout if OzonParser.config.open_timeout
    agent.read_timeout = OzonParser.config.read_timeout if OzonParser.config.read_timeout
  }
  @options = options
  @proxy = Proxy.new(options[:proxy]) if options[:proxy]
end

Public Instance Methods

create_temp(name, body, ext) click to toggle source
# File lib/ozon_parser/agent.rb, line 59
def create_temp(name, body, ext)
  temp_file = Tempfile.new([name, ".#{ext}"])
  temp_file.binmode
  temp_file.write body.string
  temp_file.flush
  temp_file
end
get(url, parameters = [], referer = nil, headers = {}) click to toggle source
# File lib/ozon_parser/agent.rb, line 23
def get(url, parameters = [], referer = nil, headers = {})
  @agent.reset
  @agent.follow_redirect = false

  set_proxy
  set_user_agent

  begin
    @page = @agent.get(url, [], referer)
    raise if @page.code == '302'
  rescue => e
    p e
    freeze_current_proxy
    raise e
  end

  @page.body
end
get_file(url) click to toggle source
# File lib/ozon_parser/agent.rb, line 42
def get_file(url)
  begin
    file = @agent.get url
  rescue => e
    freeze_current_proxy
    raise e
  end
  
  ext = file.filename.split('.').last
  if file.body_io.instance_of?(Tempfile)
    body = StringIO.new(file.body_io.read)
    create_temp(file.filename, body, ext)
  else
    create_temp(file.filename, file.body_io, ext)
  end
end
history() click to toggle source
# File lib/ozon_parser/agent.rb, line 19
def history
  @agent.history
end
set_proxy() click to toggle source
# File lib/ozon_parser/agent.rb, line 67
def set_proxy
  return if @options[:proxy] === false

  return unless @proxy || OzonParser.config.use_proxy?

  @current_proxy = @proxy || OzonParser.config.proxy_list.next
  @agent.set_proxy current_proxy.host, current_proxy.port
end
set_user_agent() click to toggle source
# File lib/ozon_parser/agent.rb, line 76
def set_user_agent
  @agent.user_agent_alias = next! AGENT_ALIASES
end

Private Instance Methods

freeze_current_proxy() click to toggle source
# File lib/ozon_parser/agent.rb, line 82
def freeze_current_proxy
  OzonParser.config.proxy_list.freeze_proxy current_proxy
end