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
-
.hash_find_value(hash, &block) ⇒ Object
Finds a value of a Hash.
-
.uri_dirname(uri) ⇒ Object
Returns parent directory URI of the given URI.
-
.uri_part_of(uri, part) ⇒ Object
Returns true if the given 2 URIs have a part_of relationship.
Instance Method Summary collapse
-
#argument_to_hash(args, *field) ⇒ Object
Keyword argument to hash helper.
- #force_binary(str) ⇒ Object
-
#https?(uri) ⇒ Boolean
Checks if the given URI is https.
-
#keyword_argument(args, *field) ⇒ Object
Keyword argument helper.
-
#urify(uri) ⇒ Object
Gets an URI instance.
Class Method Details
.hash_find_value(hash, &block) ⇒ Object
Finds a value of a Hash.
109 110 111 112 |
# File 'lib/httpclient/util.rb', line 109 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.
101 102 103 104 105 |
# File 'lib/httpclient/util.rb', line 101 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.
92 93 94 95 96 97 |
# File 'lib/httpclient/util.rb', line 92 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 |
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.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/httpclient/util.rb', line 61 def argument_to_hash(args, *field) return nil if args.empty? if args.size == 1 and args[0].is_a?(Hash) r = args[0].values_at(*field) unless r.compact.empty? return args[0] end end h = {} field.each_with_index do |e, idx| h[e] = args[idx] end h end |
#force_binary(str) ⇒ Object
120 121 122 123 |
# File 'lib/httpclient/util.rb', line 120 def force_binary(str) str.force_encoding(Encoding::ASCII_8BIT) if defined?(Encoding::ASCII_8BIT) str end |
#https?(uri) ⇒ Boolean
Checks if the given URI is https.
116 117 118 |
# File 'lib/httpclient/util.rb', line 116 def https?(uri) 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
44 45 46 47 48 49 50 51 52 |
# File 'lib/httpclient/util.rb', line 44 def keyword_argument(args, *field) if args.size == 1 and args[0].is_a?(Hash) r = args[0].values_at(*field) unless r.compact.empty? return r end end args end |
#urify(uri) ⇒ Object
Gets an URI instance.
77 78 79 80 81 82 83 84 85 |
# File 'lib/httpclient/util.rb', line 77 def urify(uri) if uri.nil? nil elsif uri.is_a?(URI) uri else URI.parse(uri.to_s) end end |