Class: Tr4n5l4te::Agent
- Inherits:
-
Object
- Object
- Tr4n5l4te::Agent
- Defined in:
- lib/tr4n5l4te/agent.rb
Constant Summary collapse
- DEFAULT_UA =
rubocop:disable Metrics/LineLength
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4'
Instance Attribute Summary collapse
-
#browser ⇒ Object
readonly
rubocop:enable Metrics/LineLength.
Instance Method Summary collapse
- #body ⇒ Object
- #cookies ⇒ Object
- #elements(selector_string) ⇒ Object
-
#initialize(options = {}) ⇒ Agent
constructor
A new instance of Agent.
- #load_cookies(cookie_file) ⇒ Object
- #set_cookie(name, value, options = {}) ⇒ Object
-
#store_cookies(cookie_file) ⇒ Object
rubocop:enable.
- #visit(url) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Agent
Returns a new instance of Agent.
28 29 30 31 32 |
# File 'lib/tr4n5l4te/agent.rb', line 28 def initialize( = {}) Capybara.ignore_hidden_elements = false @browser = [:browser] || Capybara.current_session browser.driver.headers = { 'User-Agent' => DEFAULT_UA } end |
Instance Attribute Details
#browser ⇒ Object (readonly)
rubocop:enable Metrics/LineLength
26 27 28 |
# File 'lib/tr4n5l4te/agent.rb', line 26 def browser @browser end |
Instance Method Details
#body ⇒ Object
81 82 83 |
# File 'lib/tr4n5l4te/agent.rb', line 81 def body browser.body end |
#cookies ⇒ Object
72 73 74 |
# File 'lib/tr4n5l4te/agent.rb', line 72 def browser.driver. end |
#elements(selector_string) ⇒ Object
85 86 87 |
# File 'lib/tr4n5l4te/agent.rb', line 85 def elements(selector_string) browser.all(selector_string) end |
#load_cookies(cookie_file) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/tr4n5l4te/agent.rb', line 34 def () return false unless = YAML.safe_load( File.read(), permitted_classes: [Capybara::Poltergeist::Cookie] ) browser.driver. .each do |key, | browser.driver.( key, .value, domain: .domain, path: .path, secure: .secure?, httponly: .httponly?, expires: .expires ) end end |
#set_cookie(name, value, options = {}) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/tr4n5l4te/agent.rb', line 60 def (name, value, = {}) browser.driver.( name.to_s, value.to_s, domain: .fetch(:domain, nil), path: .fetch(:path, nil), secure: .fetch(:secure, false), httponly: .fetch(:httponly, false), expires: .fetch(:expires, time_plus_years) ) end |
#store_cookies(cookie_file) ⇒ Object
rubocop:enable
54 55 56 57 58 |
# File 'lib/tr4n5l4te/agent.rb', line 54 def () FileUtils.mkdir_p(File.dirname()) data = YAML.dump(browser.driver.) File.open(, 'w') { |f| f.write(data) } end |
#visit(url) ⇒ Object
76 77 78 79 |
# File 'lib/tr4n5l4te/agent.rb', line 76 def visit(url) response = browser.visit(url) response.recursively_symbolize_keys! end |