Module: HTTPClient::Util

Included in:
HTTPClient, BasicAuth, OAuth, OAuth::Config, Session
Defined in:
lib/httpclient/util.rb

Overview

A module for common function.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.hash_find_value(hash, &block) ⇒ Object

Finds a value of a Hash.



161
162
163
164
# File 'lib/httpclient/util.rb', line 161

def hash_find_value(hash, &block)
  v = hash.find(&block)
  v ? v[1] : nil
end

.uri_dirname(uri) ⇒ Object

Returns parent directory URI of the given URI.



153
154
155
156
157
# File 'lib/httpclient/util.rb', line 153

def uri_dirname(uri)
  uri = uri.clone
  uri.path = uri.path.sub(/\/[^\/]*\z/, '/')
  uri
end

.uri_part_of(uri, part) ⇒ Object

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.



144
145
146
147
148
149
# File 'lib/httpclient/util.rb', line 144

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) ⇒ Object

Gets an URI instance.



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/httpclient/util.rb', line 126

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

Instance Method Details

#argument_to_hash(args, *field) ⇒ Object

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.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/httpclient/util.rb', line 110

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) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/httpclient/util.rb', line 172

def http?(uri)
  uri.scheme && uri.scheme.downcase == 'http'
end

#https?(uri) ⇒ Boolean

Checks if the given URI is https.

Returns:

  • (Boolean)


168
169
170
# File 'lib/httpclient/util.rb', line 168

def https?(uri)
  uri.scheme && uri.scheme.downcase == 'https'
end

#keyword_argument(args, *field) ⇒ Object

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


93
94
95
96
97
98
99
100
101
# File 'lib/httpclient/util.rb', line 93

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