Class: ActiveRestClient::Request
- Inherits:
-
Object
- Object
- ActiveRestClient::Request
- Defined in:
- lib/active_rest_client/request.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#forced_url ⇒ Object
Returns the value of attribute forced_url.
-
#get_params ⇒ Object
Returns the value of attribute get_params.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#method ⇒ Object
Returns the value of attribute method.
-
#object ⇒ Object
Returns the value of attribute object.
-
#original_url ⇒ Object
Returns the value of attribute original_url.
-
#path ⇒ Object
Returns the value of attribute path.
-
#post_params ⇒ Object
Returns the value of attribute post_params.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #append_get_parameters ⇒ Object
- #base_url ⇒ Object
- #call(explicit_parameters = nil) ⇒ Object
- #class_name ⇒ Object
- #do_request(etag) ⇒ Object
- #hal_response? ⇒ Boolean
- #handle_cached_response(cached) ⇒ Object
- #handle_hal_links_embedded(object, attributes) ⇒ Object
- #handle_response(response) ⇒ Object
- #http_method ⇒ Object
-
#initialize(method, object, params = {}) ⇒ Request
constructor
A new instance of Request.
- #new_object(attributes, name = nil) ⇒ Object
- #object_is_class? ⇒ Boolean
- #original_object_class ⇒ Object
- #prepare_params ⇒ Object
- #prepare_request_body(params = nil) ⇒ Object
- #prepare_url ⇒ Object
- #proxy ⇒ Object
- #request_body_type ⇒ Object
- #translator ⇒ Object
- #verbose? ⇒ Boolean
Constructor Details
#initialize(method, object, params = {}) ⇒ Request
Returns a new instance of Request.
9 10 11 12 13 14 15 16 17 |
# File 'lib/active_rest_client/request.rb', line 9 def initialize(method, object, params = {}) @method = method @method[:options] ||= {} @method[:options][:lazy] ||= [] @overriden_name = @method[:options][:overriden_name] @object = object @params = params @headers = HeadersList.new end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
7 8 9 |
# File 'lib/active_rest_client/request.rb', line 7 def body @body end |
#forced_url ⇒ Object
Returns the value of attribute forced_url.
7 8 9 |
# File 'lib/active_rest_client/request.rb', line 7 def forced_url @forced_url end |
#get_params ⇒ Object
Returns the value of attribute get_params.
7 8 9 |
# File 'lib/active_rest_client/request.rb', line 7 def get_params @get_params end |
#headers ⇒ Object
Returns the value of attribute headers.
7 8 9 |
# File 'lib/active_rest_client/request.rb', line 7 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
7 8 9 |
# File 'lib/active_rest_client/request.rb', line 7 def method @method end |
#object ⇒ Object
Returns the value of attribute object.
7 8 9 |
# File 'lib/active_rest_client/request.rb', line 7 def object @object end |
#original_url ⇒ Object
Returns the value of attribute original_url.
7 8 9 |
# File 'lib/active_rest_client/request.rb', line 7 def original_url @original_url end |
#path ⇒ Object
Returns the value of attribute path.
7 8 9 |
# File 'lib/active_rest_client/request.rb', line 7 def path @path end |
#post_params ⇒ Object
Returns the value of attribute post_params.
7 8 9 |
# File 'lib/active_rest_client/request.rb', line 7 def post_params @post_params end |
#url ⇒ Object
Returns the value of attribute url.
7 8 9 |
# File 'lib/active_rest_client/request.rb', line 7 def url @url end |
Instance Method Details
#append_get_parameters ⇒ Object
176 177 178 179 180 181 |
# File 'lib/active_rest_client/request.rb', line 176 def append_get_parameters if @get_params.any? params = @get_params.map {|k,v| "#{k}=#{CGI.escape(v.to_s)}"} @url += "?" + params.sort * "&" end end |
#base_url ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/active_rest_client/request.rb', line 39 def base_url if object_is_class? @object.base_url else @object.class.base_url end end |
#call(explicit_parameters = nil) ⇒ Object
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/active_rest_client/request.rb', line 85 def call(explicit_parameters=nil) @instrumentation_name = "#{class_name}##{@method[:name]}" result = nil cached = nil ActiveSupport::Notifications.instrument("request_call.active_rest_client", :name => @instrumentation_name) do @explicit_parameters = explicit_parameters @body = nil prepare_params prepare_url if fake = @method[:options][:fake] if fake.respond_to?(:call) fake = fake.call(self) end ActiveRestClient::Logger.debug " \033[1;4;32m#{ActiveRestClient::NAME}\033[0m #{@instrumentation_name} - Faked response found" return handle_response(OpenStruct.new(status:200, body:fake, headers:{"X-ARC-Faked-Response" => "true"})) end if object_is_class? @object.send(:_filter_request, @method[:name], self) else @object.class.send(:_filter_request, @method[:name], self) end append_get_parameters prepare_request_body self.original_url = self.url cached = original_object_class.read_cached_response(self) if cached if cached.expires && cached.expires > Time.now ActiveRestClient::Logger.debug " \033[1;4;32m#{ActiveRestClient::NAME}\033[0m #{@instrumentation_name} - Absolutely cached copy found" return handle_cached_response(cached) elsif cached.etag.to_s != "" #present? isn't working for some reason ActiveRestClient::Logger.debug " \033[1;4;32m#{ActiveRestClient::NAME}\033[0m #{@instrumentation_name} - Etag cached copy found with etag #{cached.etag}" etag = cached.etag response = do_request(etag) if response.status == 304 ActiveRestClient::Logger.debug " \033[1;4;32m#{ActiveRestClient::NAME}\033[0m #{@instrumentation_name} - Etag copy is the same as the server" return handle_cached_response(cached) end end end response = if proxy proxy.handle(self) do |request| request.do_request(etag) end else do_request(etag) end if object_is_class? && @object.record_response? @object.record_response(self.url, response) end result = handle_response(response) original_object_class.write_cached_response(self, response, result) result end end |
#class_name ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/active_rest_client/request.rb', line 23 def class_name if object_is_class? @object.name else @object.class.name end end |
#do_request(etag) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/active_rest_client/request.rb', line 191 def do_request(etag) http_headers = {} http_headers["If-None-Match"] = etag if etag http_headers["Accept"] = "application/hal+json, application/json;q=0.5" headers.each do |key,value| value = value.join(",") if value.is_a?(Array) http_headers[key] = value end if @method[:options][:url] || @forced_url @url = @method[:options][:url] || @method[:url] @url = @forced_url if @forced_url if connection = ActiveRestClient::ConnectionManager.find_connection_for_url(@url) @url = @url.slice(connection.base_url.length, 255) else parts = @url.match(%r{^(https?://[a-z\d\.:-]+?)(/.*)}).to_a if (parts.empty?) # Not a full URL, so use hostname/protocol from existing base_url uri = URI.parse(base_url) @base_url = "#{uri.scheme}://#{uri.host}#{":#{uri.port}" if uri.port != 80 && uri.port != 443}" @url = "#{base_url}#{@url}".gsub(@base_url, "") else _, @base_url, @url = parts end connection = ActiveRestClient::ConnectionManager.get_connection(@base_url) end else parts = @url.match(%r{^(https?://[a-z\d\.:-]+?)(/.*)}).to_a if (parts.empty?) # Not a full URL, so use hostname/protocol from existing base_url uri = URI.parse(base_url) @base_url = "#{uri.scheme}://#{uri.host}#{":#{uri.port}" if uri.port != 80 && uri.port != 443}" @url = "#{base_url}#{@url}".gsub(@base_url, "") base_url = @base_url end connection = ActiveRestClient::ConnectionManager.get_connection(base_url) end ActiveRestClient::Logger.info " \033[1;4;32m#{ActiveRestClient::NAME}\033[0m #{@instrumentation_name} - Requesting #{connection.base_url}#{@url}" if verbose? ActiveRestClient::Logger.debug "ActiveRestClient Verbose Log:" ActiveRestClient::Logger.debug " Request" ActiveRestClient::Logger.debug " >> #{http_method.upcase} #{@url} HTTP/1.1" http_headers.each do |k,v| ActiveRestClient::Logger.debug " >> #{k} : #{v}" end ActiveRestClient::Logger.debug " >> Body:\n#{@body}" end case http_method when :get response = connection.get(@url, http_headers) when :put response = connection.put(@url, @body, http_headers) when :post response = connection.post(@url, @body, http_headers) when :delete response = connection.delete(@url, http_headers) else raise InvalidRequestException.new("Invalid method #{http_method}") end if verbose? ActiveRestClient::Logger.debug " Response" ActiveRestClient::Logger.debug " << Status : #{response.status}" response.headers.each do |k,v| ActiveRestClient::Logger.debug " << #{k} : #{v}" end ActiveRestClient::Logger.debug " << Body:\n#{response.body}" end response end |
#hal_response? ⇒ Boolean
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/active_rest_client/request.rb', line 362 def hal_response? _, content_type = @response.headers.detect{|k,v| k.downcase == "content-type"} faked_response = @response.headers.detect{|k,v| k.downcase == "x-arc-faked-response"} if content_type && content_type.respond_to?(:each) content_type.each do |ct| return true if ct[%r{application\/hal\+json}i] return true if ct[%r{application\/json}i] end faked_response elsif content_type && (content_type[%r{application\/hal\+json}i] || content_type[%r{application\/json}i]) || faked_response true else false end end |
#handle_cached_response(cached) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/active_rest_client/request.rb', line 262 def handle_cached_response(cached) if cached.result.is_a? ActiveRestClient::ResultIterator cached.result else if object_is_class? cached.result else @object._copy_from(cached.result) @object end end end |
#handle_hal_links_embedded(object, attributes) ⇒ Object
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/active_rest_client/request.rb', line 378 def (object, attributes) attributes["_links"] = attributes[:_links] if attributes[:_links] attributes["_embedded"] = attributes[:_embedded] if attributes[:_embedded] if attributes["_links"] attributes["_links"].each do |key, value| if value.is_a?(Array) object._attributes[key.to_sym] ||= ActiveRestClient::ResultIterator.new value.each do |element| begin = attributes["_embedded"][key].detect{|| ["_links"]["self"]["href"] == element["href"]} object._attributes[key.to_sym] << new_object(, key) rescue NoMethodError object._attributes[key.to_sym] << ActiveRestClient::LazyAssociationLoader.new(key, element, self) end end else begin = attributes["_embedded"][key] object._attributes[key.to_sym] = new_object(, key) rescue NoMethodError object._attributes[key.to_sym] = ActiveRestClient::LazyAssociationLoader.new(key, value, self) end end end attributes.delete("_links") attributes.delete("_embedded") end attributes end |
#handle_response(response) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/active_rest_client/request.rb', line 275 def handle_response(response) @response = response status = @response.status || 200 if (200..399).include?(status) if @method[:options][:plain] return @response = response.body elsif is_json_response? if @response.respond_to?(:proxied) && @response.proxied ActiveRestClient::Logger.debug " \033[1;4;32m#{ActiveRestClient::NAME}\033[0m #{@instrumentation_name} - Response was proxied, unable to determine size" else ActiveRestClient::Logger.debug " \033[1;4;32m#{ActiveRestClient::NAME}\033[0m #{@instrumentation_name} - Response received #{@response.body.size} bytes" end result = generate_new_object else raise ResponseParseException.new(status:status, body:@response.body) end else if is_json_response? error_response = generate_new_object(mutable: false) else error_response = @response.body end if status == 400 raise HTTPBadRequestClientException.new(status:status, result:error_response, url:@url) elsif status == 401 raise HTTPUnauthorisedClientException.new(status:status, result:error_response, url:@url) elsif status == 403 raise HTTPForbiddenClientException.new(status:status, result:error_response, url:@url) elsif status == 404 raise HTTPNotFoundClientException.new(status:status, result:error_response, url:@url) elsif (400..499).include? status raise HTTPClientException.new(status:status, result:error_response, url:@url) elsif (500..599).include? status raise HTTPServerException.new(status:status, result:error_response, url:@url) end end result end |
#http_method ⇒ Object
81 82 83 |
# File 'lib/active_rest_client/request.rb', line 81 def http_method @method[:method] end |
#new_object(attributes, name = nil) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/active_rest_client/request.rb', line 316 def new_object(attributes, name = nil) @method[:options][:has_many] ||= {} name = name.to_sym rescue nil if @method[:options][:has_many][name] overriden_name = name object = @method[:options][:has_many][name].new else if object_is_class? object = @object.new else object = @object.class.new end end if hal_response? && name.nil? attributes = (object, attributes) end attributes.each do |k,v| k = k.to_sym if @method[:options][:lazy].include?(k) object._attributes[k] = ActiveRestClient::LazyAssociationLoader.new(overriden_name || k, v, self, overriden_name:(overriden_name||k)) elsif v.is_a? Hash object._attributes[k] = new_object(v, overriden_name || k) elsif v.is_a? Array object._attributes[k] = ActiveRestClient::ResultIterator.new v.each do |item| if item.is_a? Hash object._attributes[k] << new_object(item, overriden_name || k) else object._attributes[k] << item end end else if v.to_s[/\d{4}\-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(Z|[+-]\d{2}:\d{2})/] object._attributes[k] = DateTime.parse(v) else object._attributes[k] = v end end end object.clean! unless object_is_class? object end |
#object_is_class? ⇒ Boolean
19 20 21 |
# File 'lib/active_rest_client/request.rb', line 19 def object_is_class? !@object.respond_to?(:dirty?) end |
#original_object_class ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/active_rest_client/request.rb', line 31 def original_object_class if object_is_class? @object else @object.class end end |
#prepare_params ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/active_rest_client/request.rb', line 140 def prepare_params params = @params || @object._attributes rescue {} if params.is_a?(String) || params.is_a?(Fixnum) params = {id:params} end default_params = @method[:options][:defaults] || {} if @explicit_parameters params = @explicit_parameters end if http_method == :get @get_params = default_params.merge(params || {}) @post_params = nil else @post_params = default_params.merge(params || {}) @get_params = {} end end |
#prepare_request_body(params = nil) ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/active_rest_client/request.rb', line 183 def prepare_request_body(params = nil) if request_body_type == :form_encoded @body ||= (params || @post_params || {}).map {|k,v| "#{k}=#{CGI.escape(v.to_s)}"}.sort * "&" elsif request_body_type == :json @body ||= (params || @post_params || {}).to_json end end |
#prepare_url ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/active_rest_client/request.rb', line 160 def prepare_url if @forced_url && @forced_url.present? @url = @forced_url else @url = @method[:url].dup matches = @url.scan(/(:[a-z_-]+)/) @get_params ||= {} @post_params ||= {} matches.each do |token| token = token.first[1,999] target = @get_params.delete(token.to_sym) || @post_params.delete(token.to_sym) || @get_params.delete(token.to_s) || @post_params.delete(token.to_s) || "" @url.gsub!(":#{token}", target.to_s) end end end |
#proxy ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/active_rest_client/request.rb', line 71 def proxy if object_is_class? @object.proxy else @object.class.proxy end rescue nil end |
#request_body_type ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/active_rest_client/request.rb', line 47 def request_body_type if object_is_class? @object.request_body_type else @object.class.request_body_type end end |
#translator ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/active_rest_client/request.rb', line 63 def translator if object_is_class? @object.translator else @object.class.translator end end |
#verbose? ⇒ Boolean
55 56 57 58 59 60 61 |
# File 'lib/active_rest_client/request.rb', line 55 def verbose? if object_is_class? @object.verbose else @object.class.verbose end end |