jbcn 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 951525ccc503519e49cb98ada2ac871fdae43d82
4
- data.tar.gz: 620abb4e2d0230396e5648ba024c7bc1e0366333
3
+ metadata.gz: bde689c52f2c16b04e9caadda4e40fc7411ad61a
4
+ data.tar.gz: 35d083194b0c7ea8c85381f1a9df04a77f85f221
5
5
  SHA512:
6
- metadata.gz: aa9357f5df69aa3d078a536ae6e95ffee28a2e98d4ad3ccd446d4aaf208374cfa35dfd1d4076667736b37e3ebac4b3e5d10479ee914eebd646e5540f78808461
7
- data.tar.gz: b595c710692edc242bd000b54afe975b1363170bb0bcb4fe32bb4115a3b7d9239b83f978eb8e5e5076f8e8f16382098c519b78d895c6b3a34bc3ff94a4c3df75
6
+ metadata.gz: aa364bb5f8c480b6ee59a03b103227e08c281bada28c7d016a7a13c9d5dbce18c88164e214f2a07f35031e47b5c0556330ee08cd838c32ac2bc834ec9bd605f9
7
+ data.tar.gz: 4eedcbdbf65e2be40f6999ce72b5c9cb350af73d469fe9ff609334292f854027488694346bff66b996a3a8f63eceba269e4805246e566448a137d3cf8140755e
@@ -5,8 +5,16 @@ module Jbcn
5
5
  CLOCK_ENDPOINT = "https://ssl.jobcan.jp/employee/index/adit"
6
6
 
7
7
  def authenticate(credentials)
8
- if credentials.is_a?(Hash) && (code = credentials[:code])
9
- credentials = CodeCredentials.new(code)
8
+ if (h = credentials).is_a?(Hash)
9
+ if (code = h[:code])
10
+ credentials = CodeCredentials.new(code)
11
+ elsif ((client_id = h[:client_id]) &&
12
+ (username = h[:username]) &&
13
+ (password = h[:password]))
14
+ credentials = UserCredentials.new(client_id: client_id, username: username, password: password)
15
+ else
16
+ fail(ArgumentError.new("missing keyword: either [code] or [client_id, email, password]"))
17
+ end
10
18
  end
11
19
  @token = credentials.authenticate(faraday)
12
20
  end
@@ -16,7 +24,7 @@ module Jbcn
16
24
  fail(RuntimeError, "not authenticated")
17
25
  end
18
26
  unless [:in, :out].include?(in_out)
19
- fail(ArgumentError, "expected :in or :out")
27
+ fail(ArgumentError.new("expected :in or :out"))
20
28
  end
21
29
 
22
30
  params = build_params(in_out, group_id.to_s, note.to_s, !!night_shift)
@@ -19,22 +19,59 @@ module Jbcn
19
19
  end
20
20
 
21
21
  def authenticate(faraday)
22
- response = faraday.get(AUTH_ENDPOINT + @code) rescue fail(AuthError)
22
+ response = faraday.get(AUTH_ENDPOINT + @code) rescue
23
+ fail(AuthError.new(credentials: self))
23
24
 
24
25
  unless response.status == 200
25
- fail(AuthError, response: response)
26
+ fail(AuthError.new(credentials: self, response: response))
26
27
  end
27
28
 
28
29
  token = response.body[/<input type="hidden" class="token" name="token" value="([^"]+)">/, 1]
29
30
  unless token
30
- fail(AuthTokenNotFoundError, response: response)
31
+ fail(AuthTokenNotFoundError.new(credentials: self, response: response))
31
32
  end
32
33
 
33
34
  token
34
35
  end
35
36
  end
36
37
 
37
- # TODO
38
- #class UserCredentials < Credentials
39
- #end
38
+ class UserCredentials < Credentials
39
+ AUTH_ENDPOINT = "https://ssl.jobcan.jp/login/pc-employee/try"
40
+
41
+ attr_reader :client_id, :username, :password
42
+
43
+ def initialize(client_id:, username:, password:)
44
+ @client_id = client_id
45
+ @username = username
46
+ @password = password
47
+ end
48
+
49
+ def authenticate(faraday)
50
+ response = faraday.post(AUTH_ENDPOINT, params) rescue
51
+ fail(AuthError.new(credentials: self))
52
+
53
+ unless response.status == 200
54
+ fail(AuthError.new(response: response))
55
+ end
56
+
57
+ token = response.body[/<input type="hidden" class="token" name="token" value="([^"]+)">/, 1]
58
+ unless token
59
+ fail(AuthTokenNotFoundError.new(response: response))
60
+ end
61
+
62
+ token
63
+ end
64
+
65
+ private
66
+
67
+ def params
68
+ {
69
+ client_id: @client_id,
70
+ email: @username, # email or staff code in fact
71
+ password: @password,
72
+ url: "/employee",
73
+ login_type: "1",
74
+ }
75
+ end
76
+ end
40
77
  end
@@ -5,10 +5,11 @@ module Jbcn
5
5
  end
6
6
 
7
7
  class AuthError < Error
8
- attr_reader :response
8
+ attr_reader :credentials, :response
9
9
 
10
- def initialize(message = nil, response: nil)
10
+ def initialize(message = nil, credentials: nil, response: nil)
11
11
  super(message || default_message)
12
+ @credentials = credentials
12
13
  @response = response
13
14
  end
14
15
 
@@ -23,7 +24,7 @@ module Jbcn
23
24
  private
24
25
 
25
26
  def default_message
26
- "token not found (maybe jobcan's html structure has changed)"
27
+ "token not found (maybe credentials are incorrect or jobcan's html structure has changed)"
27
28
  end
28
29
  end
29
30
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Jbcn
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jbcn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomoki Aonuma