module HTTPClient::Util

A module for common function.

Constants

AddressableEnabled

Public Class Methods

hash_find_value(hash, &block) click to toggle source

Finds a value of a Hash.

# File lib/httpclient/util.rb, line 183
def hash_find_value(hash, &block)
  v = hash.find(&block)
  v ? v[1] : nil
end
try_require(feature) click to toggle source

Try to require a feature and returns true/false if loaded

It returns 'true' for the second require in contrast of the standard require returns false if the feature is already loaded.

# File lib/httpclient/util.rb, line 193
def try_require(feature)
  require feature
  true
rescue LoadError
  false
end
uri_dirname(uri) click to toggle source

Returns parent directory URI of the given URI.

# File lib/httpclient/util.rb, line 175
def uri_dirname(uri)
  uri = uri.clone
  uri.path = uri.path.sub(/\/[^\/]*\z/, '/')
  uri
end
uri_part_of(uri, part) click to toggle source

Returns true if the given 2 URIs have a part_of relationship.

  • the same scheme

  • the same host String (no host resolution or IP-addr conversion)

  • the same port number

  • target URI's path starts with base URI's path.

# File lib/httpclient/util.rb, line 166
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
urify(uri) click to toggle source

Gets an URI instance.

# File lib/httpclient/util.rb, line 148
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

Public Instance Methods

argument_to_hash(args, *field) click to toggle source

Keyword argument to hash helper.

args

given arguments.

*field

a list of arguments to be extracted.

Returns hash which has defined keys. When a Hash given, returns it including undefined keys. When an Array given, returns a Hash which only includes defined keys.

# File lib/httpclient/util.rb, line 132
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
http?(uri) click to toggle source
# File lib/httpclient/util.rb, line 216
def http?(uri)
  uri.scheme && uri.scheme.downcase == 'http'
end
https?(uri) click to toggle source

Checks if the given URI is https.

# File lib/httpclient/util.rb, line 212
def https?(uri)
  uri.scheme && uri.scheme.downcase == 'https'
end
keyword_argument(args, *field) click to toggle source

Keyword argument helper.

args

given arguments.

*field

a list of arguments to be extracted.

You can extract 3 arguments (a, b, c) with:

include Util
def my_method(*args)
  a, b, c = keyword_argument(args, :a, :b, :c)
  ...
end
my_method(1, 2, 3)
my_method(:b => 2, :a = 1)

instead of;

def my_method(a, b, c)
  ...
end
# File lib/httpclient/util.rb, line 115
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
warning(message) click to toggle source
# File lib/httpclient/util.rb, line 205
def warning(message)
  return if @@__warned.key?(message)
  warn(message)
  @@__warned[message] = true
end

Private Instance Methods

hash_find_value(hash, &block) click to toggle source

Finds a value of a Hash.

# File lib/httpclient/util.rb, line 183
def hash_find_value(hash, &block)
  v = hash.find(&block)
  v ? v[1] : nil
end
try_require(feature) click to toggle source

Try to require a feature and returns true/false if loaded

It returns 'true' for the second require in contrast of the standard require returns false if the feature is already loaded.

# File lib/httpclient/util.rb, line 193
def try_require(feature)
  require feature
  true
rescue LoadError
  false
end
uri_dirname(uri) click to toggle source

Returns parent directory URI of the given URI.

# File lib/httpclient/util.rb, line 175
def uri_dirname(uri)
  uri = uri.clone
  uri.path = uri.path.sub(/\/[^\/]*\z/, '/')
  uri
end
uri_part_of(uri, part) click to toggle source

Returns true if the given 2 URIs have a part_of relationship.

  • the same scheme

  • the same host String (no host resolution or IP-addr conversion)

  • the same port number

  • target URI's path starts with base URI's path.

# File lib/httpclient/util.rb, line 166
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
urify(uri) click to toggle source

Gets an URI instance.

# File lib/httpclient/util.rb, line 148
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