Class: Rest::Wrappers::NetHttpPersistentWrapper
- Inherits:
-
BaseWrapper
- Object
- BaseWrapper
- Rest::Wrappers::NetHttpPersistentWrapper
show all
- Defined in:
- lib/rest/wrappers/net_http_persistent_wrapper.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from BaseWrapper
#post_file, #to_json_parts
Constructor Details
Returns a new instance of NetHttpPersistentWrapper.
55
56
57
58
59
|
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 55
def initialize(client)
@client = client
@http = Net::HTTP::Persistent.new 'rest_gem'
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
|
Instance Attribute Details
#http ⇒ Object
Returns the value of attribute http.
53
54
55
|
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 53
def http
@http
end
|
Instance Method Details
68
69
70
71
72
73
74
75
|
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 68
def (post, req_hash, )
= {}
.merge!()
.merge!(req_hash[:headers]) if req_hash[:headers]
.each_pair do |k, v|
post[k] = v
end
end
|
#append_query_params(req_hash, uri) ⇒ Object
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 122
def append_query_params(req_hash, uri)
if req_hash[:params]
new_q = URI.encode_www_form(req_hash[:params])
if uri.query
new_q = uri.query + "&" + new_q
end
uri.query = new_q
end
end
|
#close ⇒ Object
213
214
215
|
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 213
def close
http.shutdown
end
|
61
62
63
64
65
66
|
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 61
def
{
}
end
|
#delete(url, req_hash = {}) ⇒ Object
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 197
def delete(url, req_hash={})
r = nil
uri = URI(url)
append_query_params(req_hash, uri)
post = Net::HTTP::Delete.new fix_path(uri.request_uri)
(post, req_hash, )
post.body = stringed_body(req_hash[:body]) if req_hash[:body]
response = http.request uri, post
r = NetHttpPersistentResponseWrapper.new(response)
case response
when Net::HTTPClientError, Net::HTTPServerError
raise Rest::HttpError.new(r, r.code.to_i)
end
r
end
|
#fix_path(path) ⇒ Object
133
134
135
136
|
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 133
def fix_path(path)
return "/" if path.nil? || path == ""
path
end
|
#get(url, req_hash = {}, options = {}) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 77
def get(url, req_hash={}, options={})
@client.logger.debug "limit #{options[:limit]}"
options[:limit] ||= 10
r = nil
begin
uri = URI(url)
append_query_params(req_hash, uri)
post = Net::HTTP::Get.new fix_path(uri.request_uri)
(post, req_hash, )
response = http.request uri, post
if @client.logger.debug?
@client.logger.debug "Response class: #{response.class.name}"
@client.logger.debug "Response headers: #{response..to_hash.inspect}"
end
r = NetHttpPersistentResponseWrapper.new(response)
case response
when Net::HTTPClientError, Net::HTTPServerError
raise Rest::HttpError.new(r, r.code.to_i)
end
end
r
end
|
#patch(url, req_hash = {}) ⇒ Object
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 181
def patch(url, req_hash={})
r = nil
uri = URI(url)
append_query_params(req_hash, uri)
post = Net::HTTP::Patch.new fix_path(uri.request_uri)
(post, req_hash, )
post.body = stringed_body(req_hash[:body]) if req_hash[:body]
response = http.request uri, post
r = NetHttpPersistentResponseWrapper.new(response)
case response
when Net::HTTPClientError, Net::HTTPServerError
raise Rest::HttpError.new(r, r.code.to_i)
end
r
end
|
#post(url, req_hash = {}) ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 139
def post(url, req_hash={})
r = nil
uri = URI(url)
append_query_params(req_hash, uri)
post = Net::HTTP::Post.new fix_path(uri.request_uri)
(post, req_hash, )
post.body = stringed_body(req_hash[:body]) if req_hash[:body]
post.set_form_data req_hash[:form_data] if req_hash[:form_data]
Rest.logger.debug "POST request to #{uri}. body: #{post.body}"
response = http.request uri, post
r = NetHttpPersistentResponseWrapper.new(response)
case response
when Net::HTTPClientError, Net::HTTPServerError
raise Rest::HttpError.new(r, r.code.to_i)
end
r
end
|
#put(url, req_hash = {}) ⇒ Object
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 165
def put(url, req_hash={})
r = nil
uri = URI(url)
append_query_params(req_hash, uri)
post = Net::HTTP::Put.new fix_path(uri.request_uri)
(post, req_hash, )
post.body = stringed_body(req_hash[:body]) if req_hash[:body]
response = http.request uri, post
r = NetHttpPersistentResponseWrapper.new(response)
case response
when Net::HTTPClientError, Net::HTTPServerError
raise Rest::HttpError.new(r, r.code.to_i)
end
r
end
|
#stringed_body(body) ⇒ Object
157
158
159
160
161
162
163
|
# File 'lib/rest/wrappers/net_http_persistent_wrapper.rb', line 157
def stringed_body(body)
return nil unless body
if body.is_a?(Hash)
return body.to_json
end
body
end
|