Class: BeautydateApi::APIConsumer

Inherits:
Object
  • Object
show all
Defined in:
lib/beautydate_api/api_consumer.rb

Instance Method Summary collapse

Instance Method Details

#authenticate(token) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/beautydate_api/api_consumer.rb', line 4

def authenticate token
  request = {
    method: 'POST',
    url: "#{BeautydateApi.base_uri}/consumers/auth",
    timeout: 30,
    headers: {
      user_agent: "BeautyDate/#{BeautydateApi::VERSION}; Ruby Client",
      content_type: 'application/vnd.api+json'
    },
    payload: {
      data: {
        type: 'consumers',
        attributes: {
          uuid: token
        }
      }
    }.to_json
  }
  result = JSON.parse(RestClient::Request.execute request)
  @bearer_key = result.dig('data', 'attributes', 'token')
  @expires_at = result.dig('data', 'attributes', 'token_expires_at')
  true
rescue
  raise AuthenticationException
end

#authenticated?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/beautydate_api/api_consumer.rb', line 30

def authenticated?
  @bearer_key.present?
end

#bearerObject



38
39
40
# File 'lib/beautydate_api/api_consumer.rb', line 38

def bearer
  "Bearer #{@bearer_key}"
end

#valid?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/beautydate_api/api_consumer.rb', line 34

def valid?
  authenticated? and Time.now <= Time.at(@expires_at)
end