Class: NarouAgent::Actions::UpdatePart

Inherits:
NarouAgent::Action show all
Defined in:
lib/narou_agent/actions/update_part.rb

Constant Summary

Constants included from UrlHelper

UrlHelper::NCODE_PATTERN

Instance Method Summary collapse

Methods inherited from NarouAgent::Action

#initialize

Methods included from UrlHelper

extract_part_id, ncode_to_i

Constructor Details

This class inherits a constructor from NarouAgent::Action

Instance Method Details

#run(ncode, part_id, subtitle, body, date = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/narou_agent/actions/update_part.rb', line 4

def run(ncode, part_id, subtitle, body, date = nil)
  driver.get(edit_part_url(ncode, part_id))

  driver.find_element(name: 'subtitle').tap do |subtitle_input|
    subtitle_input.clear
    subtitle_input.send_keys(subtitle)
  end
  driver.find_element(name: 'novel').tap do |novel_textarea|
    novel_textarea.clear
    novel_textarea.send_keys(body)
  end

  if !date.nil? && date > Time.now
    driver.script <<~JAVASCRIPT
      $('.hasDatepicker').datepicker('setDate', new Date('#{date.strftime('%F')}'));
    JAVASCRIPT
    Selenium::WebDriver::Support::Select.new(driver.find_element(:name, 'hour')).select_by(:value, date.hour.to_s)
  end

  driver.find_element(css: '#novelmanage[value="編集[確認]"]').click
  driver.find_element(css: '#novelmanage[value="編集[実行]"]').click

  driver.find_element(xpath: '//h2[text()="投稿済み小説編集[完了]"]')

  part_url(ncode, part_id)
rescue Selenium::WebDriver::Error::WebDriverError => e
  raise ActionFailedError.new(e)
end