Add job to cache products JSON

This commit is contained in:
Rohan Mitchell
2016-01-15 14:03:58 +11:00
parent 6df8f73bb0
commit 0d0eb6117f
2 changed files with 30 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
require 'open_food_network/products_renderer'
RefreshProductsCacheJob = Struct.new(:distributor_id, :order_cycle_id) do
def perform
Rails.cache.write "products-json-#{distributor_id}-#{order_cycle_id}", products_json
end
private
def products_json
OpenFoodNetwork::ProductsRenderer.new(distributor_id, order_cycle_id).products_json
end
end

View File

@@ -0,0 +1,15 @@
require 'spec_helper'
require 'open_food_network/products_renderer'
describe RefreshProductsCacheJob do
let(:distributor_id) { 123 }
let(:order_cycle_id) { 456 }
it "renders products and writes them to cache" do
OpenFoodNetwork::ProductsRenderer.any_instance.stub(:products_json) { 'products' }
run_job RefreshProductsCacheJob.new distributor_id, order_cycle_id
expect(Rails.cache.read("products-json-123-456")).to eq 'products'
end
end