Class: SendpulseClient::Connection::Token

Inherits:
Struct
  • Object
show all
Defined in:
lib/sendpulse_client/connection.rb

Constant Summary collapse

URL =
'https://api.sendpulse.com/oauth/access_token'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil, created_at = nil) ⇒ Token

Returns a new instance of Token.



10
11
12
13
# File 'lib/sendpulse_client/connection.rb', line 10

def initialize(value = nil, created_at = nil)
  @value = value
  @created_at = created_at
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at

Returns:

  • (Object)

    the current value of created_at



7
8
9
# File 'lib/sendpulse_client/connection.rb', line 7

def created_at
  @created_at
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



7
8
9
# File 'lib/sendpulse_client/connection.rb', line 7

def value
  @value
end

Instance Method Details

#set!Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/sendpulse_client/connection.rb', line 15

def set!
  body = {
    client_id: SendpulseClient.configuration.client_id,
    client_secret: SendpulseClient.configuration.client_secret,
    grant_type: 'client_credentials'
  }

  @value = HTTParty.post(URL, body: body)['access_token']
  @created_at = Time.now
end

#to_sObject



26
27
28
# File 'lib/sendpulse_client/connection.rb', line 26

def to_s
  "Bearer #{@value}"
end

#valid?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/sendpulse_client/connection.rb', line 30

def valid?
  (Time.now - @created_at) / 3600 < 1.0
end