Class: DeviceCloud::DataStreamClient

Inherits:
Object
  • Object
show all
Defined in:
lib/device_cloud/data_stream_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, client = nil) ⇒ DataStreamClient

Returns a new instance of DataStreamClient.



9
10
11
12
13
# File 'lib/device_cloud/data_stream_client.rb', line 9

def initialize(config, client = nil)
  @client = client || Client.new(config)
  @ttl = config["ttl"] || 60
  @last_updated = nil
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/device_cloud/data_stream_client.rb', line 5

def client
  @client
end

#last_updatedObject (readonly)

Returns the value of attribute last_updated.



7
8
9
# File 'lib/device_cloud/data_stream_client.rb', line 7

def last_updated
  @last_updated
end

#parsed_cacheObject (readonly)

Returns the value of attribute parsed_cache.



7
8
9
# File 'lib/device_cloud/data_stream_client.rb', line 7

def parsed_cache
  @parsed_cache
end

#raw_cacheObject (readonly)

Returns the value of attribute raw_cache.



7
8
9
# File 'lib/device_cloud/data_stream_client.rb', line 7

def raw_cache
  @raw_cache
end

#ttlObject

Returns the value of attribute ttl.



5
6
7
# File 'lib/device_cloud/data_stream_client.rb', line 5

def ttl
  @ttl
end

Instance Method Details

#cacheObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/device_cloud/data_stream_client.rb', line 39

def cache
  if !valid? || parsed_cache.nil?
    # We need to update the cache

    # Get all streams, and we will parse it out using xpath.
    @raw_cache = client.get DataStream::RESOURCE_PATH
    @parsed_cache = DataStream.parse raw_cache
    #get the thing
    @last_updated = Time.now
  end
  parsed_cache
end

#devicesObject



22
23
24
25
26
27
28
29
# File 'lib/device_cloud/data_stream_client.rb', line 22

def devices
  cache.map do |stream|
    {
        device_id: stream.device_id,
        path: stream.path
    }
  end.uniq
end

#invalidateObject



31
32
33
# File 'lib/device_cloud/data_stream_client.rb', line 31

def invalidate
  @last_updated = nil
end

#match(regex) ⇒ Object

match the stream_id against regex



16
17
18
19
20
# File 'lib/device_cloud/data_stream_client.rb', line 16

def match(regex)
  cache.select do |stream|
    stream.stream_id.match regex
  end
end

#valid?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/device_cloud/data_stream_client.rb', line 35

def valid?
  !@last_updated.nil? and (Time.now - @ttl) < @last_updated
end