Class: Flexirest::Request
- Inherits:
-
Object
- Object
- Flexirest::Request
- Includes:
- ActiveSupport::Inflector, AttributeParsing, JsonAPIProxy
- Defined in:
- lib/flexirest/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.
-
#retrying ⇒ Object
Returns the value of attribute retrying.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #api_auth_access_id ⇒ Object
- #api_auth_options ⇒ Object
- #api_auth_secret_key ⇒ Object
- #append_get_parameters ⇒ Object
- #base_url ⇒ Object
- #call(explicit_parameters = nil) ⇒ Object
- #class_name ⇒ Object
- #delete? ⇒ Boolean
- #do_request(etag) ⇒ Object
- #get? ⇒ Boolean
- #hal_response? ⇒ Boolean
- #handle_cached_response(cached) ⇒ Object
- #handle_hal_links_embedded(object, attributes) ⇒ Object
- #handle_response(response, cached = nil) ⇒ 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
- #password ⇒ Object
- #post? ⇒ Boolean
- #prepare_params ⇒ Object
- #prepare_request_body(params = nil) ⇒ Object
- #prepare_url ⇒ Object
- #proxy ⇒ Object
- #put? ⇒ Boolean
- #request_body_type ⇒ Object
- #translator ⇒ Object
- #username ⇒ Object
- #using_api_auth? ⇒ Boolean
- #verbose? ⇒ Boolean
Constructor Details
#initialize(method, object, params = {}) ⇒ Request
Returns a new instance of Request.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/flexirest/request.rb', line 14 def initialize(method, object, params = {}) @method = method @method[:options] ||= {} @method[:options][:lazy] ||= [] @method[:options][:array] ||= [] @method[:options][:has_one] ||= {} @overridden_name = @method[:options][:overridden_name] @object = object @response_delegate = Flexirest::RequestDelegator.new(nil) @params = params @headers = HeadersList.new (@method[:options][:headers] || {}).each do |k,v| @headers[k] = v end @forced_url = nil end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
12 13 14 |
# File 'lib/flexirest/request.rb', line 12 def body @body end |
#forced_url ⇒ Object
Returns the value of attribute forced_url.
12 13 14 |
# File 'lib/flexirest/request.rb', line 12 def forced_url @forced_url end |
#get_params ⇒ Object
Returns the value of attribute get_params.
12 13 14 |
# File 'lib/flexirest/request.rb', line 12 def get_params @get_params end |
#headers ⇒ Object
Returns the value of attribute headers.
12 13 14 |
# File 'lib/flexirest/request.rb', line 12 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
12 13 14 |
# File 'lib/flexirest/request.rb', line 12 def method @method end |
#object ⇒ Object
Returns the value of attribute object.
12 13 14 |
# File 'lib/flexirest/request.rb', line 12 def object @object end |
#original_url ⇒ Object
Returns the value of attribute original_url.
12 13 14 |
# File 'lib/flexirest/request.rb', line 12 def original_url @original_url end |
#path ⇒ Object
Returns the value of attribute path.
12 13 14 |
# File 'lib/flexirest/request.rb', line 12 def path @path end |
#post_params ⇒ Object
Returns the value of attribute post_params.
12 13 14 |
# File 'lib/flexirest/request.rb', line 12 def post_params @post_params end |
#retrying ⇒ Object
Returns the value of attribute retrying.
12 13 14 |
# File 'lib/flexirest/request.rb', line 12 def @retrying end |
#url ⇒ Object
Returns the value of attribute url.
12 13 14 |
# File 'lib/flexirest/request.rb', line 12 def url @url end |
Instance Method Details
#api_auth_access_id ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/flexirest/request.rb', line 71 def api_auth_access_id if object_is_class? @object.api_auth_access_id else @object.class.api_auth_access_id end end |
#api_auth_options ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/flexirest/request.rb', line 87 def if object_is_class? @object. else @object.class. end end |
#api_auth_secret_key ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/flexirest/request.rb', line 79 def api_auth_secret_key if object_is_class? @object.api_auth_secret_key else @object.class.api_auth_secret_key end end |
#append_get_parameters ⇒ Object
376 377 378 379 380 381 382 383 384 |
# File 'lib/flexirest/request.rb', line 376 def append_get_parameters if @get_params.any? if @method[:options][:params_encoder] == :flat @url += "?" + URI.encode_www_form(@get_params) else @url += "?" + @get_params.to_query end end end |
#base_url ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/flexirest/request.rb', line 51 def base_url if object_is_class? url = @object.base_url else url = @object.class.base_url end if url.is_a?(Array) url = url.sample end url end |
#call(explicit_parameters = nil) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 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 261 262 263 264 265 266 |
# File 'lib/flexirest/request.rb', line 177 def call(explicit_parameters=nil) @instrumentation_name = "#{class_name}##{@method[:name]}" result = nil cached = nil ActiveSupport::Notifications.instrument("request_call.flexirest", :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 Flexirest::Logger.debug " \033[1;4;32m#{Flexirest.name}\033[0m #{@instrumentation_name} - Faked response found" content_type = @method[:options][:fake_content_type] || "application/json" return handle_response(OpenStruct.new(status:200, body:fake, response_headers:{"X-ARC-Faked-Response" => "true", "Content-Type" => content_type})) end if object_is_class? callback_result = @object.send(:_callback_request, :before, @method[:name], self) else callback_result = @object.class.send(:_callback_request, :before, @method[:name], self) end if callback_result == false return false end append_get_parameters prepare_request_body self.original_url = self.url cached = original_object_class.read_cached_response(self) if cached && !cached.is_a?(String) if cached.expires && cached.expires > Time.now Flexirest::Logger.debug " \033[1;4;32m#{Flexirest.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 Flexirest::Logger.debug " \033[1;4;32m#{Flexirest.name}\033[0m #{@instrumentation_name} - Etag cached copy found with etag #{cached.etag}" etag = cached.etag end end response = ( if proxy && proxy.is_a?(Class) proxy.handle(self) do |request| request.do_request(etag) end else do_request(etag) end ) # This block is called immediately when this request is not inside a parallel request block. # Otherwise this callback is called after the parallel request block ends. response.on_complete do |response_env| if verbose? Flexirest::Logger.debug " Response" Flexirest::Logger.debug " << Status : #{response_env.status}" response_env.response_headers.each do |k,v| Flexirest::Logger.debug " << #{k} : #{v}" end Flexirest::Logger.debug " << Body:\n#{response_env.body}" end if object_is_class? && @object.record_response? @object.record_response(self.url, response_env) end begin if object_is_class? callback_result = @object.send(:_callback_request, :after, @method[:name], response_env) else callback_result = @object.class.send(:_callback_request, :after, @method[:name], response_env) end rescue Flexirest::CallbackRetryRequestException if self. != true self. = true return call() end end result = handle_response(response_env, cached) @response_delegate.__setobj__(result) original_object_class.write_cached_response(self, response_env, result) end # If this was not a parallel request just return the original result return result if response.finished? # Otherwise return the delegate which will get set later once the call back is completed return @response_delegate end end |
#class_name ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/flexirest/request.rb', line 35 def class_name if object_is_class? @object.name else @object.class.name end end |
#delete? ⇒ Boolean
173 174 175 |
# File 'lib/flexirest/request.rb', line 173 def delete? http_method == :delete end |
#do_request(etag) ⇒ Object
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
# File 'lib/flexirest/request.rb', line 436 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 = Flexirest::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 base_url.gsub!(%r{//(.)}, "//#{username}:#{password}@\\1") if username && !base_url[%r{//[^/]*:[^/]*@}] connection = Flexirest::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 else base_url = parts[0] end base_url.gsub!(%r{//(.)}, "//#{username}:#{password}@\\1") if username && !base_url[%r{//[^/]*:[^/]*@}] connection = Flexirest::ConnectionManager.get_connection(base_url) end if @method[:options][:direct] Flexirest::Logger.info " \033[1;4;32m#{Flexirest.name}\033[0m #{@instrumentation_name} - Requesting #{@url}" else Flexirest::Logger.info " \033[1;4;32m#{Flexirest.name}\033[0m #{@instrumentation_name} - Requesting #{connection.base_url}#{@url}" end if verbose? Flexirest::Logger.debug "Flexirest Verbose Log:" Flexirest::Logger.debug " Request" Flexirest::Logger.debug " >> #{http_method.upcase} #{@url} HTTP/1.1" http_headers.each do |k,v| Flexirest::Logger.debug " >> #{k} : #{v}" end Flexirest::Logger.debug " >> Body:\n#{@body}" end = {:headers => http_headers} if using_api_auth? [:api_auth] = { :api_auth_access_id => api_auth_access_id, :api_auth_secret_key => api_auth_secret_key, :api_auth_options => } end if @method[:options][:timeout] [:timeout] = @method[:options][:timeout] end case http_method when :get response = connection.get(@url, ) when :put response = connection.put(@url, @body, ) when :post response = connection.post(@url, @body, ) when :patch response = connection.patch(@url, @body, ) when :delete response = connection.delete(@url, @body, ) else raise InvalidRequestException.new("Invalid method #{http_method}") end response end |
#get? ⇒ Boolean
161 162 163 |
# File 'lib/flexirest/request.rb', line 161 def get? http_method == :get end |
#hal_response? ⇒ Boolean
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 |
# File 'lib/flexirest/request.rb', line 654 def hal_response? _, content_type = @response.response_headers.detect{|k,v| k.downcase == "content-type"} faked_response = @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
520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/flexirest/request.rb', line 520 def handle_cached_response(cached) if cached.result.is_a? Flexirest::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
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 |
# File 'lib/flexirest/request.rb', line 670 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] ||= Flexirest::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] << Flexirest::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] = Flexirest::LazyAssociationLoader.new(key, value, self) end end end attributes.delete("_links") attributes.delete("_embedded") end attributes end |
#handle_response(response, cached = nil) ⇒ Object
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
# File 'lib/flexirest/request.rb', line 533 def handle_response(response, cached = nil) @response = response status = @response.status || 200 if @response.body.blank? @response.response_headers['Content-Type'] = "application/json" @response.body = "{}" end if cached && response.status == 304 Flexirest::Logger.debug " \033[1;4;32m#{Flexirest.name}\033[0m #{@instrumentation_name}" + ' - Etag copy is the same as the server' return handle_cached_response(cached) end if (200..399).include?(status) if @method[:options][:plain] return @response = Flexirest::PlainResponse.from_response(@response) elsif is_json_response? || is_xml_response? if @response.respond_to?(:proxied) && @response.proxied Flexirest::Logger.debug " \033[1;4;32m#{Flexirest.name}\033[0m #{@instrumentation_name} - Response was proxied, unable to determine size" else Flexirest::Logger.debug " \033[1;4;32m#{Flexirest.name}\033[0m #{@instrumentation_name} - Response received #{@response.body.size} bytes" end result = generate_new_object(ignore_root: @method[:options][:ignore_root], ignore_xml_root: @method[:options][:ignore_xml_root]) # TODO: Cleanup when ignore_xml_root is removed else raise ResponseParseException.new(status:status, body:@response.body, headers: @response.headers) end else if is_json_response? || is_xml_response? error_response = generate_new_object(mutable: false, ignore_xml_root: @method[:options][:ignore_xml_root]) else error_response = @response.body end if status == 400 raise HTTPBadRequestClientException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method) elsif status == 401 raise HTTPUnauthorisedClientException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method) elsif status == 403 raise HTTPForbiddenClientException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method) elsif status == 404 raise HTTPNotFoundClientException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method) elsif status == 405 raise HTTPMethodNotAllowedClientException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method) elsif status == 406 raise HTTPNotAcceptableClientException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method) elsif status == 408 raise HTTPTimeoutClientException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method) elsif status == 409 raise HTTPConflictClientException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method) elsif (400..499).include? status raise HTTPClientException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method) elsif (500..599).include? status raise HTTPServerException.new(status:status, result:error_response, raw_response: @response.body, url:@url, method: http_method) elsif status == 0 raise TimeoutException.new("Timed out getting #{response.url}") end end result end |
#http_method ⇒ Object
157 158 159 |
# File 'lib/flexirest/request.rb', line 157 def http_method @method[:method] end |
#new_object(attributes, name = nil) ⇒ Object
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 |
# File 'lib/flexirest/request.rb', line 594 def new_object(attributes, name = nil) @method[:options][:has_many] ||= {} name = name.to_sym rescue nil if @method[:options][:has_many][name] overridden_name = name object = @method[:options][:has_many][name].new elsif @method[:options][:has_one][name] overridden_name = name object = @method[:options][:has_one][name].new else object = create_object_instance end if hal_response? && name.nil? attributes = (object, attributes) end attributes.each do |k,v| if @method[:options][:rubify_names] k = rubify_name(k) else k = k.to_sym end overridden_name = select_name(k, overridden_name) if @method[:options][:lazy].include?(k) object._attributes[k] = Flexirest::LazyAssociationLoader.new(overridden_name, v, self, overridden_name:(overridden_name)) elsif v.is_a? Hash object._attributes[k] = new_object(v, overridden_name ) elsif v.is_a? Array if @method[:options][:array].include?(k) object._attributes[k] = Array.new else object._attributes[k] = Flexirest::ResultIterator.new end v.each do |item| if item.is_a? Hash object._attributes[k] << new_object(item, overridden_name) else object._attributes[k] << item end end else parse_fields = [ @method[:options][:parse_fields], @object._date_fields ].compact.reduce([], :|) parse_fields = nil if parse_fields.empty? if (parse_fields && parse_fields.include?(k)) object._attributes[k] = parse_attribute_value(v) elsif parse_fields object._attributes[k] = v elsif Flexirest::Base.disable_automatic_date_parsing object._attributes[k] = v else object._attributes[k] = parse_attribute_value(v) end end end object.clean! unless object_is_class? object end |
#object_is_class? ⇒ Boolean
31 32 33 |
# File 'lib/flexirest/request.rb', line 31 def object_is_class? !@object.respond_to?(:dirty?) end |
#original_object_class ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/flexirest/request.rb', line 43 def original_object_class if object_is_class? @object else @object.class end end |
#password ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/flexirest/request.rb', line 107 def password ret = nil if object_is_class? ret = @object.password ret = ret.call if ret.respond_to?(:call) else ret = @object.class.password ret = ret.call(@object) if ret.respond_to?(:call) end ret end |
#post? ⇒ Boolean
165 166 167 |
# File 'lib/flexirest/request.rb', line 165 def post? http_method == :post end |
#prepare_params ⇒ Object
268 269 270 271 272 273 274 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 315 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 |
# File 'lib/flexirest/request.rb', line 268 def prepare_params if http_method == :post || http_method == :put || http_method == :patch params = (@object._attributes rescue {}).merge(@params || {}) rescue {} else params = @params || @object._attributes rescue {} end if params.is_a?(String) || params.is_a?(Integer) params = {id:params} end # Format includes parameter for jsonapi if proxy == :json_api JsonAPIProxy::Request::Params.translate(params, @object._include_associations) @object._reset_include_associations! end if @method[:options][:defaults].respond_to?(:call) default_params = @method[:options][:defaults].call(params) else default_params = @method[:options][:defaults] || {} end if @explicit_parameters params = @explicit_parameters end if http_method == :get @get_params = default_params.merge(params || {}) @post_params = nil elsif http_method == :delete && @method[:options][:send_delete_body] @post_params = default_params.merge(params || {}) @get_params = {} elsif params.is_a? String @post_params = params @get_params = {} else @post_params = (default_params || {}).merge(params || {}) @get_params = {} end # Evaluate :only_changed if @method[:options][:only_changed] if http_method == :post or http_method == :put or http_method == :patch # we only ever mess with @post_params in here, because @get_params will/should never match our method criteria if @method[:options][:only_changed].is_a? Hash # only include the listed attributes marked 'true' when they are changed; attributed marked false are always included newPostHash = {} @method[:options][:only_changed].each_pair do |changed_attr_k,changed_attr_v| if changed_attr_v == false or @object.changes.has_key? changed_attr_k.to_sym newPostHash[changed_attr_k.to_sym] = @object[changed_attr_k.to_sym] end end @post_params = newPostHash elsif @method[:options][:only_changed].is_a? Array # only send these listed attributes, and only if they are changed newPostHash = {} @method[:options][:only_changed].each do |changed_attr| if @object.changes.has_key? changed_attr.to_sym newPostHash[changed_attr.to_sym] = @object[changed_attr.to_sym] end end @post_params = newPostHash else # only send attributes if they are changed, drop the rest newPostHash = {} @object.changed.each do |k| newPostHash[k] = @object[k] end @post_params = newPostHash end end end if @method[:options][:requires] requires = @method[:options][:requires].dup merged_params = @get_params.merge(@post_params || {}) missing = [] requires.each do |key| if merged_params[key.to_sym].blank? && ![true, false].include?(merged_params[key.to_sym]) missing << key end end if missing.any? raise Flexirest::MissingParametersException.new("The following parameters weren't specifed: #{missing.join(", ")}") end end end |
#prepare_request_body(params = nil) ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/flexirest/request.rb', line 386 def prepare_request_body(params = nil) if proxy == :json_api if http_method == :get || (http_method == :delete && !@method[:options][:send_delete_body]) @body = "" else headers["Content-Type"] ||= "application/vnd.api+json" @body = JsonAPIProxy::Request::Params.create( params || @post_params || {}, object_is_class? ? @object.new : @object ).to_json end headers["Accept"] ||= "application/vnd.api+json" JsonAPIProxy::Headers.save(headers) elsif http_method == :get || (http_method == :delete && !@method[:options][:send_delete_body]) if request_body_type == :form_encoded headers["Content-Type"] ||= "application/x-www-form-urlencoded" elsif request_body_type == :json headers["Content-Type"] ||= "application/json; charset=utf-8" end @body = "" elsif request_body_type == :form_encoded @body ||= if params.is_a?(String) params elsif @post_params.is_a?(String) @post_params else p = (params || @post_params || {}) if @method[:options][:wrap_root].present? p = {@method[:options][:wrap_root] => p} end p.to_query end headers["Content-Type"] ||= "application/x-www-form-urlencoded" elsif request_body_type == :json @body ||= if params.is_a?(String) params elsif @post_params.is_a?(String) @post_params else if @method[:options][:wrap_root].present? {@method[:options][:wrap_root] => (params || @post_params || {})}.to_json else (params || @post_params || {}).to_json end end headers["Content-Type"] ||= "application/json; charset=utf-8" end end |
#prepare_url ⇒ Object
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/flexirest/request.rb', line 355 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] # pull URL path variables out of @get_params/@post_params 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) || "" unless object_is_class? # it's possible the URL path variable may not be part of the request, in that case, try to resolve it from the object attributes target = @object._attributes[token.to_sym] || "" if target == "" end @url.gsub!(":#{token}", URI.escape(target.to_s).gsub("/", "%2F").gsub("+", "%2B")) end end end |
#proxy ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/flexirest/request.rb', line 147 def proxy if object_is_class? @object.proxy else @object.class.proxy end rescue nil end |
#put? ⇒ Boolean
169 170 171 |
# File 'lib/flexirest/request.rb', line 169 def put? http_method == :put end |
#request_body_type ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/flexirest/request.rb', line 119 def request_body_type if @method[:options][:request_body_type] @method[:options][:request_body_type] elsif @object.nil? nil elsif object_is_class? @object.request_body_type else @object.class.request_body_type end end |
#translator ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/flexirest/request.rb', line 139 def translator if object_is_class? @object.translator else @object.class.translator end end |
#username ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/flexirest/request.rb', line 95 def username ret = nil if object_is_class? ret = @object.username ret = ret.call if ret.respond_to?(:call) else ret = @object.class.username ret = ret.call(@object) if ret.respond_to?(:call) end ret end |
#using_api_auth? ⇒ Boolean
63 64 65 66 67 68 69 |
# File 'lib/flexirest/request.rb', line 63 def using_api_auth? if object_is_class? @object.using_api_auth? else @object.class.using_api_auth? end end |
#verbose? ⇒ Boolean
131 132 133 134 135 136 137 |
# File 'lib/flexirest/request.rb', line 131 def verbose? if object_is_class? @object.verbose else @object.class.verbose end end |