Class: DeviceCloud::DataStreamClient
- Inherits:
-
Object
- Object
- DeviceCloud::DataStreamClient
- Defined in:
- lib/device_cloud/data_stream_client.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#last_updated ⇒ Object
readonly
Returns the value of attribute last_updated.
-
#parsed_cache ⇒ Object
readonly
Returns the value of attribute parsed_cache.
-
#raw_cache ⇒ Object
readonly
Returns the value of attribute raw_cache.
-
#ttl ⇒ Object
Returns the value of attribute ttl.
Instance Method Summary collapse
- #cache ⇒ Object
- #devices ⇒ Object
-
#initialize(config, client = nil) ⇒ DataStreamClient
constructor
A new instance of DataStreamClient.
- #invalidate ⇒ Object
-
#match(regex) ⇒ Object
match the stream_id against regex.
- #valid? ⇒ Boolean
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
#client ⇒ Object
Returns the value of attribute client.
5 6 7 |
# File 'lib/device_cloud/data_stream_client.rb', line 5 def client @client end |
#last_updated ⇒ Object (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_cache ⇒ Object (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_cache ⇒ Object (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 |
#ttl ⇒ Object
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
#cache ⇒ Object
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 |
#devices ⇒ Object
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 |
#invalidate ⇒ Object
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
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 |