Class: Jbcn::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/jbcn/client.rb

Constant Summary collapse

CLOCK_ENDPOINT =
"https://ssl.jobcan.jp/employee/index/adit"

Instance Method Summary collapse

Instance Method Details

#authenticate(credentials) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/jbcn/client.rb', line 7

def authenticate(credentials)
  if (h = credentials).is_a?(Hash)
    if (code = h[:code])
      credentials = CodeCredentials.new(code)
    elsif ((client_id = h[:client_id]) &&
           (username = h[:username]) &&
           (password = h[:password]))
      credentials = UserCredentials.new(client_id: client_id, username: username, password: password)
    else
      fail(ArgumentError.new("missing keyword: either [code] or [client_id, email, password]"))
    end
  end
  @token = credentials.authenticate(faraday)
end

#clock(in_out, group_id:, note: "", night_shift: false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jbcn/client.rb', line 22

def clock(in_out, group_id:, note: "", night_shift: false)
  unless @token
    fail(RuntimeError, "not authenticated")
  end
  unless [:in, :out].include?(in_out)
    fail(ArgumentError.new("expected :in or :out"))
  end

  params = build_params(in_out, group_id.to_s, note.to_s, !!night_shift)
  response = faraday.post(CLOCK_ENDPOINT, params) rescue fail(ClockError)

  # Jobcan would normally return responses with status code 200
  # regardless of whether the request was success or not,
  # so status != 200 is really an unexpected condition.
  unless response.status == 200
    fail(ClockError.new(response: response))
  end

  result = JSON.parse(response.body) rescue
    fail(ClockResponseParseError.new(response: response))

  if result["errors"]
    if result["errors"]["aditCount"] == "duplicate"
      fail(ClockRequestDuplicateError.new(response: response, result: result))
    end
    fail(ClockError.new(response: response, result: result))
  end

  result
end