Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Commit

Permalink
Fixed guide index caching bug
Browse files Browse the repository at this point in the history
The guide index was not caching on a per-language basis.  Thus, the cache would return whatever the first language that was pulled when the cache was built would be the language that would be returned until the cache expired.
  • Loading branch information
foscomputerservices committed Jun 5, 2019
1 parent 379ab2f commit b4c3462
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
14 changes: 10 additions & 4 deletions lib/dmtd_vbmapp_data/guide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,29 @@ def expire_cache(opts = {})

def index
expire_cache
unless defined?(@@guide_cache) && !@@guide_cache.nil?

lang = @client.language
lang = lang.nil? ? 'en'.to_sym : lang.to_sym
unless defined?(@@guide_cache) && !@@guide_cache.nil? && !@@guide_cache[:guide_index][lang].nil?
updated_index = retrieve_guide_index

guide_index = (defined?(@@guide_cache) && !@@guide_cache.nil?) ? @@guide_cache[:guide_index] : {}

if updated_index.is_a?(Array)
guide_index[lang] = updated_index
@@guide_cache = {
datestamp: DateTime.now.new_offset(0).to_date,
guide_index: updated_index
guide_index: guide_index
}
else
@@guide_cache = {
datestamp: DateTime.now.new_offset(0).to_date - 2.days, # expire immediately
guide_index: []
guide_index: {}
}
end
end

@@guide_cache[:guide_index]
@@guide_cache[:guide_index][lang]
end

def self.end_point
Expand Down
2 changes: 1 addition & 1 deletion lib/dmtd_vbmapp_data/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module DmtdVbmappData
VERSION = '1.4.0'.freeze
VERSION = '1.4.1'.freeze
end
5 changes: 4 additions & 1 deletion spec/guide_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ module DmtdVbmappData
it 'can provide chapters' do
prev_chapters = nil
AVAILABLE_LANGUAGES.each do |language|
guide = Guide.new(client: Client.new(date_of_birth: Date.today, gender: DmtdVbmappData::GENDER_FEMALE), language: language)
guide = Guide.new(client: Client.new(date_of_birth: Date.today, gender: DmtdVbmappData::GENDER_FEMALE, language: language))

expect(guide.chapters).to_not be nil
expect(guide.chapters.is_a?(Array)).to eq(true)
guide.chapters.each do |chapter|
expect(chapter.is_a?(DmtdVbmappData::GuideChapter)).to eq(true)
expect(chapter.title).to eq("CHAPTER 1") if language == 'en' && chapter.chapter_num == 1
expect(chapter.title).to eq("CAPÍTULO 1") if language == 'es' && chapter.chapter_num == 1
expect(chapter.title).to eq("CHAPITRE 1") if language == 'fr' && chapter.chapter_num == 1
end

expect(guide.chapters).to_not eq(prev_chapters) unless prev_chapters.nil?
Expand Down

0 comments on commit b4c3462

Please sign in to comment.