Skip to content

Commit

Permalink
Merge fffe673 into cd3fc53
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Pikesley committed Jan 2, 2016
2 parents cd3fc53 + fffe673 commit 6dd9663
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 15 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ group :test do
gem 'guard-cucumber'
gem 'guard-rspec'
gem 'pry'
gem 'timecop'
gem 'vcr'
gem 'webmock'
gem 'cucumber-api-steps', require: false
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ GEM
thor (0.19.1)
thread_safe (0.3.5)
tilt (1.4.1)
timecop (0.8.0)
tins (1.6.0)
tzinfo (1.2.2)
thread_safe (~> 0.1)
Expand Down Expand Up @@ -228,6 +229,7 @@ DEPENDENCIES
sinatra
sinatra-assetpack
tilt (~> 1)
timecop
vcr
webmock

Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
* Individual URLs per dataset, maybe iframe the dashboards ala Sir Handel
* Front page with high-priority things?
* Github URLs back to the datas
* License the data
22 changes: 13 additions & 9 deletions lib/dashboard/fetcher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Dashboard
class Fetcher
@@redis = Redis.new
REDIS = Redis.new

def self.redis
REDIS
end

def self.headers
{
Expand All @@ -16,15 +20,15 @@ def self.query
}
end

def self.get url
if @@redis.get url
return Marshal.load(@@redis.get url)
def self.get url, ttl = 3600
begin
Marshal.load(self.redis.get url)
rescue TypeError
h = HTTParty.get url, headers: headers, query: query
self.redis.set url, Marshal.dump(h.body)
self.redis.expire url, ttl
Marshal.load(self.redis.get url)
end

h = HTTParty.get url, headers: headers, query: query
@@redis.set url, Marshal.dump(h.body)
@@redis.expire url, 3600
return Marshal.load(@@redis.get url)
end

def self.extract_repo url
Expand Down
7 changes: 6 additions & 1 deletion lib/views/grid.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
]

var layout = {
title: json['title'],
title: json['title'] + ' (' + githubLink(json) + ')',
xaxis: {
tickformat: "%b %Y"
},
Expand Down Expand Up @@ -69,11 +69,16 @@
}
content += '<h2>' + fixedDate + '</h2>'
content += '<h3>(' + age + ')</h3>'
content += githubLink(json)
content += '<hr />'

return cellContent(json['id'], content)
}

function githubLink(json) {
return '<a href="' + json['url'] + '">Source</a>'
}

function cellContent(id, content) {
return "<div class='col-md-4' id='#" + id + "'>" + content + "</div>"
}
Expand Down
37 changes: 37 additions & 0 deletions spec/caching_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Dashboard
describe Fetcher do
before :each do
Redis.new.flushall
end

after :all do
Redis.new.flushall
end

it 'stores a value in Redis', :vcr do
url = 'https://raw.githubusercontent.com/pikesley/snake-data/master/length.csv'
described_class.get(url)
expect(Marshal.load(Redis.new.get(url))).to eq(
"""\
Date,Length in m
2014-09-30,0.45
2014-10-09,0.50
2014-10-18,0.50
2014-12-12,0.55
2015-01-11,0.60
2015-01-28,0.65
2015-08-27,0.95
"""
)
end

it 'expires a value after the timeout', :vcr do
url = 'https://api.github.com/repos/pikesley/catface/contents/flea-treatment.csv?ref=master'
described_class.get(url, 1)
expect(Redis.new.get url).to_not be nil
expect(Redis.new.ttl url).to eq 1
sleep 1
expect(Redis.new.get url).to be nil
end
end
end
5 changes: 0 additions & 5 deletions spec/javascripts/dashboardSpec.js
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@
describe('ago', function() {
it('knows a thing was a month ago', function() {
Timecop.install()
})
})
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'coveralls'
Coveralls.wear_merged!
require 'timecop'

require 'dashboard'
require_relative 'support/vcr_setup'
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions spec/support/vcr/Dashboard_Fetcher/stores_a_value_in_Redis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6dd9663

Please sign in to comment.