Class: Toggl::Jobcan::Client

Inherits:
Object
  • Object
show all
Includes:
TogglSupport
Defined in:
lib/toggl/jobcan/client.rb

Overview

Jobcan client

Defined Under Namespace

Classes: JobcanLoginFailure

Constant Summary collapse

JOBCAN_URLS =
{
  login: 'https://id.jobcan.jp/users/sign_in',
  attendance_login: 'https://ssl.jobcan.jp/jbcoauth/login',
  attendance: 'https://ssl.jobcan.jp/employee/attendance',
  attendance_modify: 'https://ssl.jobcan.jp/employee/adit/modify/'
}.freeze
XPATHS =
{
  notice: %(//textarea[@name='notice']),
  load_button: %(//input[@value='表示']),
  submit: %(//input[@type='submit']),
  flash: %(//p[@class='flash flash__alert'])
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TogglSupport

#fetch_toggl_worktime, #toggl_time_format

Constructor Details

#initialize(credentials: nil, options: Selenium::WebDriver::Chrome::Options.new, toggl_worktime_config:, dryrun: false) ⇒ Client

Returns a new instance of Client.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/toggl/jobcan/client.rb', line 29

def initialize(
      credentials: nil,
      options: Selenium::WebDriver::Chrome::Options.new,
      toggl_worktime_config:,
      dryrun: false
    )
  @credentials = credentials
  options.add_argument('--headless')
  @driver = Selenium::WebDriver.for :chrome, options: options
  @toggl = Toggl::Worktime::Driver.new(
    config: Toggl::Worktime::Config.new(path: toggl_worktime_config)
  )
  @dryrun = dryrun
end

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



7
8
9
# File 'lib/toggl/jobcan/client.rb', line 7

def credentials
  @credentials
end

#driverObject (readonly)

Returns the value of attribute driver.



8
9
10
# File 'lib/toggl/jobcan/client.rb', line 8

def driver
  @driver
end

#togglObject (readonly)

Returns the value of attribute toggl.



9
10
11
# File 'lib/toggl/jobcan/client.rb', line 9

def toggl
  @toggl
end

Instance Method Details

#input_day_worktime(date, time_slots) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/toggl/jobcan/client.rb', line 90

def input_day_worktime(date, time_slots)
  time_slots.flatten.each do |timestamp|
    input_stamp = toggl_time_format(date, timestamp)
    puts "  - Input #{input_stamp}"
    navigate_to_attendance_modify_day(date)
    send_timestamp input_stamp
    send_notice
    @driver.find_element(:id, 'insert_button').submit unless @dryrun
  end
end

#loginObject

Raises:



44
45
46
47
48
49
50
51
52
53
# File 'lib/toggl/jobcan/client.rb', line 44

def 
  @driver.navigate.to JOBCAN_URLS[:login]
  send_credentials
  @driver.find_element(:xpath, XPATHS[:submit]).click
  raise JobcanLoginFailure if may_find_element(:xpath, XPATHS[:flash])

  # attendance login
  @driver.navigate.to JOBCAN_URLS[:attendance_login]
  @driver.navigate.to JOBCAN_URLS[:attendance]
end


77
78
79
80
81
# File 'lib/toggl/jobcan/client.rb', line 77

def navigate_to_attendance_modify_day(date)
  # https://ssl.jobcan.jp/employee/adit/modify?year=2018&month=3&day=14
  query_string = "year=#{date.year}&month=#{date.month}&day=#{date.day}"
  @driver.navigate.to JOBCAN_URLS[:attendance_modify] + '?' + query_string
end


65
66
67
68
69
70
71
72
73
74
75
# File 'lib/toggl/jobcan/client.rb', line 65

def navigate_to_attendance_month(year, month)
  @driver.navigate.to JOBCAN_URLS[:attendance]
  # Specify by month
  @driver.find_element(:id, 'search_type_month').click
  selector_year = select_support_for('year')
  selector_year.select_by(:text, year.to_s)
  selector_month = select_support_for('month')
  selector_month.select_by(:text, format('%02d', month))
  # load
  @driver.find_element(:xpath, XPATHS[:load_button]).click
end

#select_support_for(name) ⇒ Object



83
84
85
86
87
88
# File 'lib/toggl/jobcan/client.rb', line 83

def select_support_for(name)
  Selenium::WebDriver::Support::Select.new(
    @driver.find_element(:xpath),
    %(//select[@name='#{name}'])
  )
end

#send_credentialsObject



55
56
57
58
59
60
61
62
63
# File 'lib/toggl/jobcan/client.rb', line 55

def send_credentials
  [
    ['user_email', :email],
    ['user_password', :password]
  ].each do |id, method|
    element = @driver.find_element(:id, id)
    element.send_keys(@credentials.send(method))
  end
end

#send_noticeObject



106
107
108
109
110
# File 'lib/toggl/jobcan/client.rb', line 106

def send_notice
  notice_elem = @driver.find_element(:xpath, XPATHS[:notice])
  notice_elem.clear
  notice_elem.send_keys('.')
end

#send_timestamp(timestamp) ⇒ Object



101
102
103
104
# File 'lib/toggl/jobcan/client.rb', line 101

def send_timestamp(timestamp)
  time_elem = @driver.find_element(:id, 'ter_time')
  time_elem.send_keys(timestamp)
end