module HTTPClient::Util
def argument_to_hash(args, *field)
including undefined keys. When an Array given, returns a Hash which only
Returns hash which has defined keys. When a Hash given, returns it
*field:: a list of arguments to be extracted.
args:: given arguments.
Keyword argument to hash helper.
def argument_to_hash(args, *field) return nil if args.empty? if args.size == 1 and Hash === args[0] h = args[0] if field.any? { |f| h.key?(f) } return h end end h = {} field.each_with_index do |e, idx| h[e] = args[idx] end h end
def hash_find_value(hash, &block)
def hash_find_value(hash, &block) v = hash.find(&block) v ? v[1] : nil end
def http?(uri)
def http?(uri) uri.scheme && uri.scheme.downcase == 'http' end
def https?(uri)
def https?(uri) uri.scheme && uri.scheme.downcase == 'https' end
def keyword_argument(args, *field)
end
...
def my_method(a, b, c)
instead of;
my_method(:b => 2, :a = 1)
my_method(1, 2, 3)
end
...
a, b, c = keyword_argument(args, :a, :b, :c)
def my_method(*args)
include Util
You can extract 3 arguments (a, b, c) with:
*field:: a list of arguments to be extracted.
args:: given arguments.
Keyword argument helper.
def keyword_argument(args, *field) if args.size == 1 and Hash === args[0] h = args[0] if field.any? { |f| h.key?(f) } return h.values_at(*field) end end args end
def try_require(feature)
It returns 'true' for the second require in contrast of the standard
Try to require a feature and returns true/false if loaded
def try_require(feature) require feature true rescue LoadError false end
def uri_dirname(uri)
def uri_dirname(uri) uri = uri.clone uri.path = uri.path.sub(/\/[^\/]*\z/, '/') uri end
def uri_part_of(uri, part)
* the same port number
* the same host String (no host resolution or IP-addr conversion)
* the same scheme
Returns true if the given 2 URIs have a part_of relationship.
def uri_part_of(uri, part) ((uri.scheme == part.scheme) and (uri.host == part.host) and (uri.port == part.port) and uri.path.upcase.index(part.path.upcase) == 0) end
def urify(uri)
def urify(uri) if uri.nil? nil elsif uri.is_a?(URI) uri elsif AddressableEnabled AddressableURI.parse(uri.to_s) else URI.parse(uri.to_s) end end
def warning(message)
def warning(message) return if @@__warned.key?(message) warn(message) @@__warned[message] = true end