Sha256: f0c24ae8fe59135bcab7da4b2a9eeb7cb2939cd27ca4434e2a30de10d745d7c7

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'rest_client'

module Airborne
  module RestClientRequester
    def make_request(method, url, options = {})
      headers = base_headers.merge(options[:headers] || {})
      res = if method == :post || method == :patch || method == :put
        begin
          request_body = options[:body].nil? ? '' : options[:body]
          request_body = request_body.to_json if is_json_request(headers)
          RestClient.send(method, get_url(url), request_body, headers){|response, request, result| response }
        rescue RestClient::Exception => e
          e.response
        end
      else
        begin
          RestClient.send(method, get_url(url), headers){|response, request, result| response }
        rescue RestClient::Exception => e
          e.response
        end
      end
      res
    end

    private

    def is_json_request(headers)
      header = headers.fetch(:content_type)
      header == :json || /application\/([a-zA-Z0-9\.\_\-]*\+?)json/ =~ header
    end

    def base_headers
      { content_type: :json }.merge(Airborne.configuration.headers || {})
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
airborne-0.3.2 lib/airborne/rest_client_requester.rb