Class: Sunbro::DynamicHTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/sunbro/dynamic_http.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ DynamicHTTP

Returns a new instance of DynamicHTTP.



5
6
7
8
# File 'lib/sunbro/dynamic_http.rb', line 5

def initialize(opts = {})
  @opts = opts
  new_session
end

Instance Attribute Details

#sessionObject (readonly)

Returns the value of attribute session.



3
4
5
# File 'lib/sunbro/dynamic_http.rb', line 3

def session
  @session
end

Instance Method Details

#closeObject



10
11
12
# File 'lib/sunbro/dynamic_http.rb', line 10

def close
  @session.driver.quit
end

#fetch_page(url, opts = {}) ⇒ Object

Create new Pages from the response of an HTTP request to url, including redirects



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sunbro/dynamic_http.rb', line 45

def fetch_page(url, opts={})
  begin
    tries ||= 5
    get_page(url, opts)
  rescue Capybara::Poltergeist::DeadClient, Errno::EPIPE, NoMethodError, Capybara::Poltergeist::BrowserError => e
    restart_session
    retry unless (tries -= 1).zero?
    close
    raise e
  end
end

#get_page(url, opts) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/sunbro/dynamic_http.rb', line 57

def get_page(url, opts)
  reset = opts.fetch(:reset) rescue true
  session.visit(url.to_s)
  page = create_page_from_session(url, session, opts)
  session.reset! if reset
  page
rescue Capybara::Poltergeist::TimeoutError => e
  restart_session
  return Page.new(url, :error => e)
end

#new_sessionObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sunbro/dynamic_http.rb', line 14

def new_session
  Capybara.register_driver :poltergeist do |app|
    Capybara::Poltergeist::Driver.new(
      app,
      js_errors: false,
      phantomjs_options: ['--load-images=no', '--ignore-ssl-errors=yes']
    )
  end
  Capybara.default_driver = :poltergeist
  Capybara.javascript_driver = :poltergeist
  Capybara.run_server = false
  @session = Capybara::Session.new(:poltergeist)
  @session.driver.headers = {
    'User-Agent' => user_agent
  }
  @session
end

#restart_sessionObject



36
37
38
39
# File 'lib/sunbro/dynamic_http.rb', line 36

def restart_session
  close
  new_session
end

#user_agentObject



32
33
34
# File 'lib/sunbro/dynamic_http.rb', line 32

def user_agent
  @opts[:agent] || Settings.phantomjs_user_agent
end