-
-
Save sr/8459 to your computer and use it in GitHub Desktop.
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
class Hash | |
def stringify_keys! | |
keys.each do |key| | |
self[key.to_s] = delete(key) | |
end | |
self | |
end | |
def jsonify_necessary_values! | |
values.each do |key, value| | |
self[key] = value.to_json if %(key startkey endkey).include?(key) | |
end | |
self | |
end | |
end | |
module Addressable | |
class URI | |
alias :orig_query_values= :query_values= | |
def query_values=(params) | |
params.stringify_keys! | |
params.jsonify_necessary_values! | |
self.orig_query_values = params | |
end | |
end | |
end | |
class String | |
def to_uri(params={}) | |
Addressable::URI.parse(self).tap do |uri| | |
uri.query_values = params | |
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
require 'addressable/uri' | |
class Object | |
def tap | |
yield(self) | |
self | |
end | |
end | |
class Hash | |
def stringify_keys_and_jsonify_values!(values=%w(key startkey endkey)) | |
keys.each do |key| | |
value = self.delete(key) | |
value = value.to_json if values.include?(key.to_s) | |
self[key.to_s] = value | |
end | |
self | |
end | |
end | |
module Addressable | |
class URI | |
alias :orig_query_values= :query_values= | |
def query_values=(params) | |
params.stringify_keys_and_jsonify_values! | |
self.orig_query_values = params | |
end | |
end | |
end | |
class String | |
def to_uri(params={}) | |
Addressable::URI.parse(self).tap { |u| u.query_values = params } | |
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
module Addressable | |
class URI | |
alias :orig_query_values= :query_values= | |
def query_values=(params) | |
keys.each do |key| | |
self[key.to_s] = delete(key).to_json if %(key startkey endkey).include?(key.to_s) | |
end | |
self.orig_query_values = params | |
end | |
end | |
end | |
class String | |
def to_uri(params={}) | |
Addressable::URI.parse(self).tap do |uri| | |
uri.query_values = params | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment