Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ end
_ℹ️ If you run the `init` generator you should set the attributes in the generated initializer (`config/initializers/ruber`)_

## Cache
Ruber uses a caching solution to improve efficiency (e.g., for caching tokens). By default, it uses a simple file cache (see below), but you can change the cache method by setting the `Ruber.cache` attribute:
Ruber uses a caching solution to improve efficiency (e.g., for caching tokens). By default, it uses a simple memory cache,
but you can change the cache method by setting the `Ruber.cache` attribute.

```ruby
Ruber.cache = Redis.new
Expand All @@ -52,9 +53,12 @@ Ruber.cache = Rails.cache
Ruber.cache = YourCustomCache.new
```

> Try not to use the memory cache in production, as it will be cleared on every request.


### File cache

File cache is the default cache option. It uses a yaml file to store the cached data.
Ruber comes with a simple File cache option. It uses a yaml file to store the cached data.

```yml
---
Expand Down
11 changes: 8 additions & 3 deletions lib/generators/ruber/templates/initializers/ruber.tt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ Ruber.configure do |config|
config.client_id = 'uber-client-id'
config.client_secret = 'uber-client-secret'

# --------------------------------------------------------------------------
# Cache
# --------------------------------------------------------------------------
# Ruber comes with a File cache option. It uses a yaml file to store the cached data.
# Uncomment the following line to use the file cache. Make sure the path is in your
# .gitignore to avoid pushing your token to the repo.
# config.cache = Ruber::FileCache.new('tmp/ruber_cache.yaml')

# If you want to use a different cache option (like redis, memcache, etc).
# It might be any object that responds to read/write/delete/clear
# config.cache = Rails.cache

# If you want to use a file cache, you can set the path here
# config.file_cache_path = 'tmp/uber_cache.yaml'
end
25 changes: 0 additions & 25 deletions lib/ruber/cache.rb

This file was deleted.

6 changes: 3 additions & 3 deletions lib/ruber/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# frozen_string_literal: true

require_relative "configuration/null_cache"
require_relative "configuration/memory_cache"
require_relative "configuration/file_cache"

module Ruber
class Configuration
attr_accessor :customer_id, :client_id, :client_secret, :file_cache_path
attr_accessor :customer_id, :client_id, :client_secret

def cache
@cache ||= FileCache.new
@cache ||= MemoryCache.new
end

def cache=(store)
Expand Down
9 changes: 4 additions & 5 deletions lib/ruber/configuration/file_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

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)
def initialize(file_cache_path)
@file_cache_path = file_cache_path
@store = YAML::Store.new(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)
File.delete(@file_cache_path) if File.exist?(@file_cache_path)
end

def delete(key) = @store.transaction { @store.delete(key) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Ruber
class Configuration
class NullCache
class MemoryCache
def read(key) = memory_store[key]
def write(key, value, _options = {}) = memory_store[key] = value
def clear = memory_store.clear
Expand Down
1 change: 1 addition & 0 deletions test/ruber/authenticator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class AuthenticatorTest < Minitest::Test
include WebMock::API

def setup
Ruber.cache = Ruber::FileCache.new("test/tmp/ruber_cache.yaml")
Ruber.cache.clear
end

Expand Down
13 changes: 3 additions & 10 deletions test/ruber/configuration/file_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,16 @@
module Ruber
class FileCacheTest < Minitest::Test
def setup
File.delete(Ruber.configuration.file_cache_path) if File.exist?(Ruber.configuration.file_cache_path)
file_cache_path = "ruber_cache.yaml"
File.delete(file_cache_path) if File.exist?(file_cache_path)

@cache = FileCache.new
@cache = FileCache.new(file_cache_path)
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")
Expand Down
1 change: 0 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class Minitest::Test # rubocop:disable Style/ClassAndModuleChildren
def before_setup
super

Ruber.configuration.file_cache_path = "file_cache_test.yaml"
Ruber.configuration.customer_id = "a_customer_id"
Ruber.configuration.client_secret = "a_client_secret"
end
Expand Down
Empty file added test/tmp/ruber_cache.yaml
Empty file.
Loading