Class: Store::Flatfile

Inherits:
Store
  • Object
show all
Defined in:
lib/store/flatfile.rb

Overview

Flatfile - ideally a CSV of `time,valuen`

Instance Method Summary collapse

Constructor Details

#initialize(origin, settings) ⇒ Flatfile

Initalize and get settings


5
6
7
8
9
10
11
12
# File 'lib/store/flatfile.rb', line 5

def initialize origin, settings
	super
	@file      = mandatory_param :file_name, "store_settings"
	@metric    = mandatory_param :metric, "store_settings"
	@delimiter = optional_param  :delimiter, ",", "store_settings"

	@base_url  = @file #synonym for is_up error msg
end

Instance Method Details

#get_metric(m, start = nil, stop = nil, step = nil) ⇒ Object

Traverse the file for the parameters given

Raises:


42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/store/flatfile.rb', line 42

def get_metric m, start=nil, stop=nil, step=nil
		test_file

		data = []
		File.open(@file).each_line do |line|
			x, y = line.split(@delimiter)
			data << {x: x.to_i, y: y.to_f} if x.to_i.between?(start, stop)
		end

		raise Store::Error, "No data for #{m.metric_id} within selected time period" if data == []

		return data_sanitize data, start, stop, step

end

#get_metric_url(m, start, stop, step) ⇒ Object

In lieu of a href, use a file locator


36
37
38
39
# File 'lib/store/flatfile.rb', line 36

def get_metric_url m, start,stop,step
	 return "file://#{Rails.root}/#{@file}"

end

#get_metrics_listObject

In lieu of querying an endpoint, confirm the file exists, and return the metric from the settings


30
31
32
33
# File 'lib/store/flatfile.rb', line 30

def get_metrics_list
		test_file
		[@metric]
end

#is_up?Boolean

In lieu of pinging a href, confirm the existance of the file

Returns:

  • (Boolean)

20
21
22
# File 'lib/store/flatfile.rb', line 20

def is_up?
	File.exists?(@file)
end

#live?Boolean

Flat files don't auto update, therefore cannot be assumed to be live

Returns:

  • (Boolean)

25
26
27
# File 'lib/store/flatfile.rb', line 25

def live?
	false
end

#test_fileObject

Raise an error if the file doesn't exist

Raises:


15
16
17
# File 'lib/store/flatfile.rb', line 15

def test_file
	raise Store::Error, "Error: file #{@file} does not exist" unless is_up?
end