Class: Trader::SyncConverter

Inherits:
FixedConverter show all
Defined in:
lib/trade-o-matic/converters/sync_converter.rb

Direct Known Subclasses

JsonApiConverter, WebConverter

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FixedConverter

#apply, #current_rate

Methods inherited from Converter

#apply, #current_rate

Constructor Details

#initialize(_ttl, &_block) ⇒ SyncConverter

Returns a new instance of SyncConverter.



5
6
7
8
9
# File 'lib/trade-o-matic/converters/sync_converter.rb', line 5

def initialize(_ttl, &_block)
  super nil
  @ttl = _ttl
  @block = _block
end

Instance Attribute Details

#ttlObject (readonly)

Returns the value of attribute ttl.



3
4
5
# File 'lib/trade-o-matic/converters/sync_converter.rb', line 3

def ttl
  @ttl
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/trade-o-matic/converters/sync_converter.rb', line 11

def expired?
  @rate.nil? || @last_sync < (Time.now - @ttl)
end

#rateObject



15
16
17
18
19
20
21
# File 'lib/trade-o-matic/converters/sync_converter.rb', line 15

def rate
  if expired?
    @rate = @block.call
    @last_sync = Time.now
  end
  @rate
end