Module: HTTPClient::Util

Included in:
HTTP::Message, HTTPClient, AuthBase, OAuth::Config, SSLConfig, Session
Defined in:
lib/httpclient/util.rb

Overview

A module for common function.

Constant Summary collapse

@@__warned =

show one warning message only once by caching message

it cached all messages in memory so be careful not to show many kinds of warning message.

{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.hash_find_value(hash, &block) ⇒ Object

Finds a value of a Hash.


183
184
185
186
# 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) ⇒ Object

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.


193
194
195
196
197
198
# File 'lib/httpclient/util.rb', line 193

def try_require(feature)
  require feature
  true
rescue LoadError
  false
end

.uri_dirname(uri) ⇒ Object

Returns parent directory URI of the given URI.


175
176
177
178
179
# 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) ⇒ 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.


166
167
168
169
170
171
# 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) ⇒ Object

Gets an URI instance.


148
149
150
151
152
153
154
155
156
157
158
# 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

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.


132
133
134
135
136
137
138
139
140
141
142
143
144
145
# 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) ⇒ Boolean

Returns:

  • (Boolean)

216
217
218
# File 'lib/httpclient/util.rb', line 216

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

#https?(uri) ⇒ Boolean

Checks if the given URI is https.

Returns:

  • (Boolean)

212
213
214
# File 'lib/httpclient/util.rb', line 212

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

115
116
117
118
119
120
121
122
123
# 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) ⇒ Object


205
206
207
208
209
# File 'lib/httpclient/util.rb', line 205

def warning(message)
  return if @@__warned.key?(message)
  warn(message)
  @@__warned[message] = true
end