Class: BasisBand

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

Instance Method Summary collapse

Constructor Details

#initialize(userid) ⇒ BasisBand

Returns a new instance of BasisBand.



9
10
11
# File 'lib/basis-band.rb', line 9

def initialize(userid)
  @userid = userid
end

Instance Method Details

#all_cache_filesObject



37
38
39
# File 'lib/basis-band.rb', line 37

def all_cache_files()
  Dir[File.join(@cache_dir, "*.json")]
end

#cache_filename(date) ⇒ Object



33
34
35
# File 'lib/basis-band.rb', line 33

def cache_filename(date)
  File.join(@cache_dir, date + ".json")
end

#cached_value_for_day(date) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/basis-band.rb', line 41

def cached_value_for_day(date)
  raw = nil
  begin
    File.open(cache_filename(date), "r") { |f|
      raw = f.read
    }
  rescue
    # ignore exception
  end
  raw
end

#data_for_allObject



25
26
27
28
29
30
31
# File 'lib/basis-band.rb', line 25

def data_for_all
  all = all_cache_files.collect do |f|
    d = File.basename(f, ".json")
    [d, cached_value_for_day(d)]
  end
  Hash[all]
end

#data_for_day(date) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/basis-band.rb', line 17

def data_for_day(date)
  r = cached_value_for_day(date)
  if !r
    r = fetch_value_for_day(date)
  end
  r
end

#fetch_activities_for_day(date, token) ⇒ Object



64
65
66
67
68
# File 'lib/basis-band.rb', line 64

def fetch_activities_for_day(date, token)
  f = ApiFetch.new()
  raw = f.get_activities(token, date)
  raw
end

#fetch_value_for_day(date) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/basis-band.rb', line 53

def fetch_value_for_day(date)
  f = ApiFetch.new()
  raw = f.get_day(@userid, date)
  if raw && @cache_dir
    File.open(cache_filename(date), "w") { |f|
      f.write(raw)
    }
  end
  raw
end

#set_cache_dir(dir) ⇒ Object



13
14
15
# File 'lib/basis-band.rb', line 13

def set_cache_dir(dir)
  @cache_dir = dir
end