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 +4 -4
- data/lib/jbcn/client.rb +11 -3
- data/lib/jbcn/credentials.rb +43 -6
- data/lib/jbcn/error.rb +4 -3
- data/lib/jbcn/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bde689c52f2c16b04e9caadda4e40fc7411ad61a
|
4
|
+
data.tar.gz: 35d083194b0c7ea8c85381f1a9df04a77f85f221
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa364bb5f8c480b6ee59a03b103227e08c281bada28c7d016a7a13c9d5dbce18c88164e214f2a07f35031e47b5c0556330ee08cd838c32ac2bc834ec9bd605f9
|
7
|
+
data.tar.gz: 4eedcbdbf65e2be40f6999ce72b5c9cb350af73d469fe9ff609334292f854027488694346bff66b996a3a8f63eceba269e4805246e566448a137d3cf8140755e
|
data/lib/jbcn/client.rb
CHANGED
@@ -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)
|
9
|
-
|
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
|
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)
|
data/lib/jbcn/credentials.rb
CHANGED
@@ -19,22 +19,59 @@ module Jbcn
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def authenticate(faraday)
|
22
|
-
response = faraday.get(AUTH_ENDPOINT + @code) rescue
|
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
|
-
|
38
|
-
|
39
|
-
|
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
|
data/lib/jbcn/error.rb
CHANGED
@@ -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
|
|
data/lib/jbcn/version.rb
CHANGED