Skip to content

Instantly share code, notes, and snippets.

@lmarburger
Last active May 25, 2020 11:39
Show Gist options
  • Save lmarburger/4489985 to your computer and use it in GitHub Desktop.
Save lmarburger/4489985 to your computer and use it in GitHub Desktop.
Using faraday and rack-cache to cache API calls
# removes "private" bit from Cache-Control headers
PrivateCacheBuster = Struct.new :app do
def call env
response = app.call env
response['cache-control'].sub!('private, ', '') if response['cache-control']
response
end
end
# removes nil request headers created by bug
# https://github.com/pengwynn/faraday_middleware/pull/35
NilHeaderExterminator = Struct.new :app do
def call env
env[:request_headers].reject! { |name, value| value.nil? }
app.call env
end
end
def api_client options = {}
options = { oauth_token: auth_token,
faraday_config_block: lambda { |conn|
conn.use FaradayMiddleware::RackCompatible, Rack::Cache::Context,
:metastore => "file:myappmeta",
:entitystore => "file:myappbody",
:ignore_headers => %w[Set-Cookie X-Content-Digest]
conn.use PrivateCacheBuster
conn.use NilHeaderExterminator
}
}
Octokit::Client.new options
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment