Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add `:strip_params` option for Caching middleware

  • Loading branch information...
commit 6756ffc73ff2ad02235b0646f6b1236aa921f8b1 1 parent 9f9ec14
@mislav authored
View
1  lib/faraday_stack.rb
@@ -1,6 +1,7 @@
# encoding: utf-8
require 'faraday'
require 'forwardable'
+require 'faraday_stack/addressable_patch'
module FaradayStack
extend Faraday::AutoloadHelper
View
18 lib/faraday_stack/addressable_patch.rb
@@ -0,0 +1,18 @@
+require 'addressable/uri'
+
+# fix `normalized_query` by sorting query key-value pairs
+# (rejected: https://github.com/sporkmonger/addressable/issues/28)
+class Addressable::URI
+ class << self
+ alias old_normalize_component normalize_component
+
+ def normalize_component(component, character_class = CharacterClasses::RESERVED + CharacterClasses::UNRESERVED)
+ normalized = old_normalize_component(component, character_class)
+ if character_class == Addressable::URI::CharacterClasses::QUERY
+ pairs = normalized.split('&').sort_by { |pair| pair[0, pair.index('=') || pair.length] }
+ normalized = pairs.join('&')
+ end
+ normalized
+ end
+ end
+end
View
17 lib/faraday_stack/caching.rb
@@ -2,9 +2,10 @@ module FaradayStack
class Caching < Faraday::Middleware
attr_reader :cache
- def initialize(app, cache = nil)
+ def initialize(app, cache = nil, options = {})
super(app)
@cache = cache || Proc.new.call
+ @options = options
end
def call(env)
@@ -23,7 +24,19 @@ def call(env)
end
def cache_key(env)
- env[:url].request_uri
+ url = env[:url]
+ if params_to_strip.any?
+ url = url.dup
+ url.query_values = url.query_values.reject { |k,| params_to_strip.include? k }
+ url.normalize!
+ else
+ url = url.normalize
+ end
+ url.request_uri
+ end
+
+ def params_to_strip
+ @params_to_strip ||= Array(@options[:strip_params]).map { |p| p.to_s }
end
def cache_on_complete(env)
Please sign in to comment.
Something went wrong with that request. Please try again.