nice_http 1.7.2 → 1.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +607 -581
- data/lib/nice_http/http_methods.rb +526 -526
- data/lib/nice_http/manage_request.rb +237 -237
- data/lib/nice_http/manage_response.rb +280 -280
- data/lib/nice_http/utils.rb +109 -109
- data/lib/nice_http.rb +414 -411
- metadata +2 -2
data/lib/nice_http/utils.rb
CHANGED
@@ -1,109 +1,109 @@
|
|
1
|
-
module NiceHttpUtils
|
2
|
-
##################################################
|
3
|
-
# get a value of xml tag
|
4
|
-
# input:
|
5
|
-
# tag_name
|
6
|
-
# xml_string
|
7
|
-
# take_off_prefix: boolean (optional). true, false(default)
|
8
|
-
# output:
|
9
|
-
# the value or an array of all values found with this tag_name
|
10
|
-
####################################################
|
11
|
-
def self.get_value_xml_tag(tag_name, xml_string, take_off_prefix = false)
|
12
|
-
return nil if xml_string.nil?
|
13
|
-
xml_string2 = xml_string.dup()
|
14
|
-
if take_off_prefix
|
15
|
-
i = tag_name.index(":")
|
16
|
-
if !i.nil?
|
17
|
-
tag_name = tag_name[i + 1..tag_name.length]
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
ret = Array.new()
|
22
|
-
if xml_string2.to_s() != ""
|
23
|
-
if take_off_prefix
|
24
|
-
xml_string2.gsub!(/<[a-zA-Z0-9]+:#{tag_name} [^>]*>/i, "<" + tag_name + ">")
|
25
|
-
xml_string2.gsub!(/<\/[a-zA-Z0-9]+:#{tag_name}>/i, "</" + tag_name + ">")
|
26
|
-
xml_string2.gsub!(/<[a-zA-Z0-9]+:#{tag_name}>/i, "<" + tag_name + ">")
|
27
|
-
end
|
28
|
-
|
29
|
-
xml_string2.gsub!(/<#{tag_name} [^>]*>/i, "<" + tag_name + ">")
|
30
|
-
|
31
|
-
tag1 = "<" + tag_name + ">"
|
32
|
-
tag2 = "</" + tag_name + ">"
|
33
|
-
|
34
|
-
x = xml_string2.index(tag1)
|
35
|
-
if !x.nil?
|
36
|
-
x += tag1.size
|
37
|
-
begin
|
38
|
-
y = xml_string2.index(tag2)
|
39
|
-
if y.nil?
|
40
|
-
ret.push("")
|
41
|
-
x = nil
|
42
|
-
else
|
43
|
-
y -= 1
|
44
|
-
value = xml_string2[x..y]
|
45
|
-
ret.push(value)
|
46
|
-
xml_string2 = xml_string2[y + tag2.size..xml_string2.length]
|
47
|
-
x = xml_string2.index(tag1)
|
48
|
-
if !x.nil?
|
49
|
-
x += tag1.size
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end while !x.nil?
|
53
|
-
else
|
54
|
-
ret.push("")
|
55
|
-
end
|
56
|
-
else
|
57
|
-
ret.push("")
|
58
|
-
end
|
59
|
-
if ret.size == 1
|
60
|
-
return ret[0].to_s()
|
61
|
-
else
|
62
|
-
return ret
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
##################################################
|
67
|
-
# set a value on xml tag
|
68
|
-
# input:
|
69
|
-
# tag_name
|
70
|
-
# xml_string
|
71
|
-
# value
|
72
|
-
# take_off_prefix: boolean (optional). true, false(default)
|
73
|
-
# output:
|
74
|
-
# xml_string with the new value
|
75
|
-
####################################################
|
76
|
-
def self.set_value_xml_tag(tag_name, xml_string, value, take_off_prefix = false)
|
77
|
-
tag_name = tag_name.to_s
|
78
|
-
if take_off_prefix
|
79
|
-
i = tag_name.index(":")
|
80
|
-
tag_name = tag_name[i + 1..tag_name.length] unless i.nil?
|
81
|
-
end
|
82
|
-
if xml_string.to_s != ""
|
83
|
-
if take_off_prefix
|
84
|
-
old_value = NiceHttpUtils.get_value_xml_tag(tag_name, xml_string.dup, true)
|
85
|
-
xml_string.gsub!(/:#{tag_name}>#{Regexp.escape(old_value)}<\//i, ":" + tag_name + ">" + value + "</")
|
86
|
-
xml_string.gsub!(/<#{tag_name}>#{Regexp.escape(old_value)}<\//i, "<" + tag_name + ">" + value + "</")
|
87
|
-
else
|
88
|
-
xml_string.gsub!(/<#{tag_name}>.*<\/#{tag_name}>/i, "<" + tag_name + ">" + value + "</" + tag_name + ">")
|
89
|
-
end
|
90
|
-
return xml_string
|
91
|
-
else
|
92
|
-
return ""
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
##################################################
|
97
|
-
# returns the seed for Basic authentication
|
98
|
-
# input:
|
99
|
-
# user
|
100
|
-
# password
|
101
|
-
# output:
|
102
|
-
# seed string to be used on Authorization key header on a get request
|
103
|
-
####################################################
|
104
|
-
def self.basic_authentication(user:, password:)
|
105
|
-
require "base64"
|
106
|
-
seed = "Basic " + Base64.encode64(user + ":" + password)
|
107
|
-
return seed
|
108
|
-
end
|
109
|
-
end
|
1
|
+
module NiceHttpUtils
|
2
|
+
##################################################
|
3
|
+
# get a value of xml tag
|
4
|
+
# input:
|
5
|
+
# tag_name
|
6
|
+
# xml_string
|
7
|
+
# take_off_prefix: boolean (optional). true, false(default)
|
8
|
+
# output:
|
9
|
+
# the value or an array of all values found with this tag_name
|
10
|
+
####################################################
|
11
|
+
def self.get_value_xml_tag(tag_name, xml_string, take_off_prefix = false)
|
12
|
+
return nil if xml_string.nil?
|
13
|
+
xml_string2 = xml_string.dup()
|
14
|
+
if take_off_prefix
|
15
|
+
i = tag_name.index(":")
|
16
|
+
if !i.nil?
|
17
|
+
tag_name = tag_name[i + 1..tag_name.length]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
ret = Array.new()
|
22
|
+
if xml_string2.to_s() != ""
|
23
|
+
if take_off_prefix
|
24
|
+
xml_string2.gsub!(/<[a-zA-Z0-9]+:#{tag_name} [^>]*>/i, "<" + tag_name + ">")
|
25
|
+
xml_string2.gsub!(/<\/[a-zA-Z0-9]+:#{tag_name}>/i, "</" + tag_name + ">")
|
26
|
+
xml_string2.gsub!(/<[a-zA-Z0-9]+:#{tag_name}>/i, "<" + tag_name + ">")
|
27
|
+
end
|
28
|
+
|
29
|
+
xml_string2.gsub!(/<#{tag_name} [^>]*>/i, "<" + tag_name + ">")
|
30
|
+
|
31
|
+
tag1 = "<" + tag_name + ">"
|
32
|
+
tag2 = "</" + tag_name + ">"
|
33
|
+
|
34
|
+
x = xml_string2.index(tag1)
|
35
|
+
if !x.nil?
|
36
|
+
x += tag1.size
|
37
|
+
begin
|
38
|
+
y = xml_string2.index(tag2)
|
39
|
+
if y.nil?
|
40
|
+
ret.push("")
|
41
|
+
x = nil
|
42
|
+
else
|
43
|
+
y -= 1
|
44
|
+
value = xml_string2[x..y]
|
45
|
+
ret.push(value)
|
46
|
+
xml_string2 = xml_string2[y + tag2.size..xml_string2.length]
|
47
|
+
x = xml_string2.index(tag1)
|
48
|
+
if !x.nil?
|
49
|
+
x += tag1.size
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end while !x.nil?
|
53
|
+
else
|
54
|
+
ret.push("")
|
55
|
+
end
|
56
|
+
else
|
57
|
+
ret.push("")
|
58
|
+
end
|
59
|
+
if ret.size == 1
|
60
|
+
return ret[0].to_s()
|
61
|
+
else
|
62
|
+
return ret
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
##################################################
|
67
|
+
# set a value on xml tag
|
68
|
+
# input:
|
69
|
+
# tag_name
|
70
|
+
# xml_string
|
71
|
+
# value
|
72
|
+
# take_off_prefix: boolean (optional). true, false(default)
|
73
|
+
# output:
|
74
|
+
# xml_string with the new value
|
75
|
+
####################################################
|
76
|
+
def self.set_value_xml_tag(tag_name, xml_string, value, take_off_prefix = false)
|
77
|
+
tag_name = tag_name.to_s
|
78
|
+
if take_off_prefix
|
79
|
+
i = tag_name.index(":")
|
80
|
+
tag_name = tag_name[i + 1..tag_name.length] unless i.nil?
|
81
|
+
end
|
82
|
+
if xml_string.to_s != ""
|
83
|
+
if take_off_prefix
|
84
|
+
old_value = NiceHttpUtils.get_value_xml_tag(tag_name, xml_string.dup, true)
|
85
|
+
xml_string.gsub!(/:#{tag_name}>#{Regexp.escape(old_value)}<\//i, ":" + tag_name + ">" + value + "</")
|
86
|
+
xml_string.gsub!(/<#{tag_name}>#{Regexp.escape(old_value)}<\//i, "<" + tag_name + ">" + value + "</")
|
87
|
+
else
|
88
|
+
xml_string.gsub!(/<#{tag_name}>.*<\/#{tag_name}>/i, "<" + tag_name + ">" + value + "</" + tag_name + ">")
|
89
|
+
end
|
90
|
+
return xml_string
|
91
|
+
else
|
92
|
+
return ""
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
##################################################
|
97
|
+
# returns the seed for Basic authentication
|
98
|
+
# input:
|
99
|
+
# user
|
100
|
+
# password
|
101
|
+
# output:
|
102
|
+
# seed string to be used on Authorization key header on a get request
|
103
|
+
####################################################
|
104
|
+
def self.basic_authentication(user:, password:)
|
105
|
+
require "base64"
|
106
|
+
seed = "Basic " + Base64.encode64(user + ":" + password)
|
107
|
+
return seed
|
108
|
+
end
|
109
|
+
end
|