-
Notifications
You must be signed in to change notification settings - Fork 1
FileCache store #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
641df4c
feature -> cache authenticator
bruno-costanzo 4cd106a
update readme
bruno-costanzo 80568f5
translate cache to configuration
bruno-costanzo 3a815d2
code suggestions
bruno-costanzo c6ebd4b
Move cache_key to authenticator
nicogaldamez 14df933
Refresh token if expired
nicogaldamez f76ec29
File cache store
nicogaldamez dcab6b6
Merge conflicts
nicogaldamez e6c941f
Assign file_cache_path on before_setup
nicogaldamez 613d21f
Add complex object test to file cache
nicogaldamez 6fb83b8
Changes based on PR comments
nicogaldamez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,4 @@ | |
| /pkg/ | ||
| /spec/reports/ | ||
| /tmp/ | ||
| file_cache_test.yaml | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "yaml/store" | ||
|
|
||
| module Ruber | ||
| class FileCache | ||
| def initialize | ||
| raise "FileCache requires a file path" unless Ruber.configuration.file_cache_path | ||
|
|
||
| @store = YAML::Store.new(Ruber.configuration.file_cache_path) | ||
| end | ||
|
|
||
| def read(key) = @store.transaction { @store[key] } | ||
| def write(key, value) = @store.transaction { @store[key] = value } | ||
|
|
||
| def clear | ||
| File.delete(Ruber.configuration.file_cache_path) if File.exist?(Ruber.configuration.file_cache_path) | ||
| end | ||
|
|
||
| def delete(key) = @store.transaction { @store.delete(key) } | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require File.expand_path("../../test_helper", __dir__) | ||
| require "yaml/store" | ||
| require "fileutils" | ||
|
|
||
| module Ruber | ||
| class FileCacheTest < Minitest::Test | ||
| def setup | ||
| File.delete(Ruber.configuration.file_cache_path) if File.exist?(Ruber.configuration.file_cache_path) | ||
|
|
||
| @cache = FileCache.new | ||
| end | ||
|
|
||
| def teardown | ||
| @cache.clear | ||
| end | ||
|
|
||
| def test_initialize_raises_error_if_no_file_path | ||
| Ruber.configuration.stub :file_cache_path, nil do | ||
| assert_raises(RuntimeError, "FileCache requires a file path") do | ||
| FileCache.new | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def test_write_and_read | ||
| @cache.write("foo", "bar") | ||
| assert_equal "bar", @cache.read("foo") | ||
| end | ||
|
|
||
| def test_write_and_read_complex_object | ||
| @cache.write("foo", { foo: "bar", bar: "baz" }) | ||
| assert_equal "bar", @cache.read("foo")[:foo] | ||
| assert_equal "baz", @cache.read("foo")[:bar] | ||
| end | ||
|
|
||
| def test_delete | ||
| @cache.write("key", "value") | ||
| @cache.delete("key") | ||
| assert_nil @cache.read("key") | ||
| end | ||
|
|
||
| def test_clear | ||
| @cache.write("key1", "value1") | ||
| @cache.write("key2", "value2") | ||
|
|
||
| @cache.clear | ||
| assert_nil @cache.read("key1") | ||
| assert_nil @cache.read("key2") | ||
| end | ||
|
|
||
| def test_clear_and_write_again | ||
| @cache.write("key1", "value1") | ||
| @cache.write("key2", "value1") | ||
| @cache.clear | ||
| @cache.write("key1", "value2") | ||
|
|
||
| assert "value2", @cache.read("key1") | ||
| assert_nil @cache.read("key2") | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,16 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| $LOAD_PATH.unshift File.expand_path("../lib", __dir__) | ||
| require "ruber" | ||
|
|
||
| require "minitest/autorun" | ||
| require "webmock/minitest" | ||
|
|
||
| require "ruber" | ||
|
|
||
| class Minitest::Test # rubocop:disable Style/ClassAndModuleChildren | ||
| def before_setup | ||
| super | ||
|
|
||
| Ruber.configuration.file_cache_path = "file_cache_test.yaml" | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tengo una duda respecto a este test.
¿Qué pasa si volvés a escribir la caché después de hacer
.clear? se vuelve a crear el archivo? tal vez se puede agregar al test ese caso para que quede claro.Porque veo que el archivo se borra y no logro ver si se vuelve a crear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sí, lo vuelve a crear. Ahí agrego un test más.