Class: BookmeterScraper::Agent

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/bookmeter_scraper/agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAgent

Returns a new instance of Agent.



12
13
14
15
16
17
18
19
# File 'lib/bookmeter_scraper/agent.rb', line 12

def initialize
  @agent = Mechanize.new do |a|
    a.user_agent_alias = Mechanize::AGENT_ALIASES.keys.reject do |ua_alias|
      %w(Android iPad iPhone Mechanize).include?(ua_alias)
    end.sample
  end
  @log_in_user_id = nil
end

Instance Attribute Details

#log_in_user_idObject (readonly)

Returns the value of attribute log_in_user_id.



9
10
11
# File 'lib/bookmeter_scraper/agent.rb', line 9

def 
  @log_in_user_id
end

Instance Method Details

#log_in(config) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bookmeter_scraper/agent.rb', line 21

def (config)
  raise ArgumentError if config.nil?

  page_after_submitting_form = nil
  @agent.get(BookmeterScraper::LOGIN_URI) do |page|
    page_after_submitting_form = page.form_with(action: '/login') do |form|
      form.field_with(name: 'mail').value     = config.mail
      form.field_with(name: 'password').value = config.password
    end.submit
  end

  if page_after_logging_in? page_after_submitting_form
    mypage = page_after_submitting_form.link_with(text: 'マイページ').click
    @log_in_user_id = extract_user_id(mypage)
  else
    nil
  end
end

#logged_in?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/bookmeter_scraper/agent.rb', line 40

def logged_in?
  !@log_in_user_id.nil?
end