Class: Jbcn::CodeCredentials

Inherits:
Credentials show all
Defined in:
lib/jbcn/credentials.rb

Constant Summary collapse

AUTH_ENDPOINT =
"https://ssl.jobcan.jp/employee?code="

Instance Attribute Summary collapse

Attributes inherited from Credentials

#token

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ CodeCredentials

Returns a new instance of CodeCredentials.



17
18
19
# File 'lib/jbcn/credentials.rb', line 17

def initialize(code)
  @code = code
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



15
16
17
# File 'lib/jbcn/credentials.rb', line 15

def code
  @code
end

Instance Method Details

#authenticate(faraday) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jbcn/credentials.rb', line 21

def authenticate(faraday)
  response = faraday.get(AUTH_ENDPOINT + @code) rescue
    fail(AuthError.new(credentials: self))

  unless response.status == 200
    fail(AuthError.new(credentials: self, response: response))
  end

  token = response.body[/<input type="hidden" class="token" name="token" value="([^"]+)">/, 1]
  unless token
    fail(AuthTokenNotFoundError.new(credentials: self, response: response))
  end

  token
end