Module: DisplayWords::Request::Authentication

Defined in:
lib/display_words/authentication.rb

Constant Summary collapse

ENDPOINT =
'/auth'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#token ⇒ Object

Returns the value of attribute token.



6
7
8
# File 'lib/display_words/authentication.rb', line 6

def token
  @token
end

#token_expire ⇒ Object

Returns the value of attribute token_expire.



6
7
8
# File 'lib/display_words/authentication.rb', line 6

def token_expire
  @token_expire
end

Instance Method Details

#authenticate(username, password) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/display_words/authentication.rb', line 8

def authenticate(username, password)
  options = authentication_options(username, password)

  response = post(ENDPOINT, options)

  @token_expire = Time.now + (2 * 60 * 60) #Token expires two hours from when it was given
  @token        = response['token']
end

#authentication_needed? ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/display_words/authentication.rb', line 21

def authentication_needed?
  !token || expired_token?
end

#authentication_options(username, password) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/display_words/authentication.rb', line 25

def authentication_options(username, password)
  credentials = {
    :auth => {
      :username => username,
      :password => password
    }
  }

  {
    :body => credentials.to_json,
    :headers => {
      "content_type" => "json"
    }
  }
end

#expired_token? ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/display_words/authentication.rb', line 17

def expired_token?
  token_expire <= Time.now
end