Class: ApiResponseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/basis-band/api-response-model.rb

Instance Method Summary collapse

Constructor Details

#initialize(raw_text) ⇒ ApiResponseModel

Returns a new instance of ApiResponseModel.



5
6
7
8
9
# File 'lib/basis-band/api-response-model.rb', line 5

def initialize(raw_text)
  @json = JSON.parse(raw_text)
  # Use the local timezone intentionally
  @starttime = Time.at(@json['starttime'])
end

Instance Method Details

#hash_for_minute(min) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/basis-band/api-response-model.rb', line 22

def hash_for_minute(min)
  t = @starttime + (min * 60)
  res = {'t' => t.strftime("%Y/%m/%d %H:%M:%S"), 'state' => state_for_time(t.to_i)}
  for m in @json["metrics"].keys
    res[m] = @json["metrics"][m]["values"][min]
  end
  res
end

#metric_summary(metric) ⇒ Object



42
43
44
# File 'lib/basis-band/api-response-model.rb', line 42

def metric_summary(metric)
  {"avg" => @json["metrics"][metric]["avg"]}
end

#num_samplesObject



31
32
33
34
# File 'lib/basis-band/api-response-model.rb', line 31

def num_samples
  m = @json["metrics"].keys.first
  @json["metrics"][m]["values"].length
end

#samples_by_minuteObject



36
37
38
39
40
# File 'lib/basis-band/api-response-model.rb', line 36

def samples_by_minute
  (0...num_samples).map { |x|
    hash_for_minute(x)
  }
end

#state_for_time(t) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/basis-band/api-response-model.rb', line 11

def state_for_time(t)
  matches = @json["bodystates"].select { |s|
    t >= s[0] && t < s[1]
  }
  if matches.length == 1
    matches[0][2]
  else
    "unknown"
  end
end

#summaryObject



46
47
48
49
50
51
52
# File 'lib/basis-band/api-response-model.rb', line 46

def summary
  res = {}
  for m in @json["metrics"].keys
    res[m] = metric_summary(m)
  end
  res
end