Class: BasisBand
- Inherits:
-
Object
- Object
- BasisBand
- Defined in:
- lib/basis-band.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
Instance Method Summary collapse
- #activities_cache_filename(date) ⇒ Object
- #activities_cache_files ⇒ Object
- #activities_for_all ⇒ Object
- #activities_for_day(token, date) ⇒ Object
- #cached_value(filename) ⇒ Object
- #fetch_activities_value(token, date, filename) ⇒ Object
- #fetch_metrics_value(userid, date, filename) ⇒ Object
- #fetch_result_w_cache(filename) ⇒ Object
-
#initialize(cache_dir) ⇒ BasisBand
constructor
A new instance of BasisBand.
- #metrics_cache_filename(date) ⇒ Object
- #metrics_cache_files ⇒ Object
- #metrics_for_all ⇒ Object
- #metrics_for_day(userid, date) ⇒ Object
- #with_cache(filename) ⇒ Object
Constructor Details
Instance Attribute Details
#api ⇒ Object
Returns the value of attribute api.
9 10 11 |
# File 'lib/basis-band.rb', line 9 def api @api end |
Instance Method Details
#activities_cache_filename(date) ⇒ Object
32 33 34 |
# File 'lib/basis-band.rb', line 32 def activities_cache_filename(date) File.join(@cache_dir, date + "_activities.json") end |
#activities_cache_files ⇒ Object
40 41 42 |
# File 'lib/basis-band.rb', line 40 def activities_cache_files() Dir[File.join(@cache_dir, "*_activities.json")] end |
#activities_for_all ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/basis-band.rb', line 52 def activities_for_all activities = activities_cache_files.collect { |f| date = File.basename(f, "_activities.json") [d, cached_value(f)] } Hash[activities] end |
#activities_for_day(token, date) ⇒ Object
22 23 24 25 26 |
# File 'lib/basis-band.rb', line 22 def activities_for_day(token, date) with_cache(activities_cache_filename(date)) { |filename| fetch_activities_value(token, date, filename) } end |
#cached_value(filename) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/basis-band.rb', line 64 def cached_value(filename) raw = nil begin File.open(filename, "r") { |f| raw = f.read } rescue # ignore exception end raw end |
#fetch_activities_value(token, date, filename) ⇒ Object
92 93 94 95 96 |
# File 'lib/basis-band.rb', line 92 def fetch_activities_value(token, date, filename) fetch_result_w_cache(filename) { @api.get_day_activities(token, date) } end |
#fetch_metrics_value(userid, date, filename) ⇒ Object
86 87 88 89 90 |
# File 'lib/basis-band.rb', line 86 def fetch_metrics_value(userid, date, filename) fetch_result_w_cache(filename) { @api.get_day_metrics(userid, date) } end |
#fetch_result_w_cache(filename) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/basis-band.rb', line 76 def fetch_result_w_cache(filename) r = yield if r File.open(filename, "w") { |f| f.write(r) } end r end |
#metrics_cache_filename(date) ⇒ Object
28 29 30 |
# File 'lib/basis-band.rb', line 28 def metrics_cache_filename(date) File.join(@cache_dir, date + "_metrics.json") end |
#metrics_cache_files ⇒ Object
36 37 38 |
# File 'lib/basis-band.rb', line 36 def metrics_cache_files() Dir[File.join(@cache_dir, "*_metrics.json")] end |
#metrics_for_all ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/basis-band.rb', line 44 def metrics_for_all metrics = metrics_cache_files.collect { |f| date = File.basename(f, "_metrics.json") [date, cached_value(f)] } Hash[metrics] end |
#metrics_for_day(userid, date) ⇒ Object
16 17 18 19 20 |
# File 'lib/basis-band.rb', line 16 def metrics_for_day(userid, date) with_cache(metrics_cache_filename(date)) { |filename| fetch_metrics_value(userid, date, filename) } end |
#with_cache(filename) ⇒ Object
60 61 62 |
# File 'lib/basis-band.rb', line 60 def with_cache(filename) cached_value(filename) || yield(filename) end |