Class: Trader::SyncConverter

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

Direct Known Subclasses

JsonApiConverter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_ttl, &_block) ⇒ SyncConverter

Returns a new instance of SyncConverter.



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

def initialize(_ttl, &_block)
  @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)


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

def expired?
  @last_sync < Time.new - @ttl
end

#rateObject



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

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

  @rate
end