class String

Public Instance Methods

http_delete(params: {}) click to toggle source
# File lib/dpr.rb, line 17
def http_delete params: {}, headers: {}, timeout: 10
  to_resp method: :delete, headers: headers, params: params, timeout: timeout
end
http_get(params: {}) click to toggle source
# File lib/dpr.rb, line 9
def http_get params: {}, headers: {}, timeout: 10
  to_resp method: :get, headers: headers, params: params, timeout: timeout
end
http_patch(params: {}) click to toggle source
# File lib/dpr.rb, line 25
def http_patch params: {}, headers: {}, timeout: 10
  to_resp method: :patch, headers: headers, params: params, timeout: timeout
end
http_post(params: {}) click to toggle source
# File lib/dpr.rb, line 13
def http_post params: {}, headers: {}, timeout: 10
  to_resp method: :post, headers: headers, params: params, timeout: timeout
end
http_put(params: {}) click to toggle source
# File lib/dpr.rb, line 21
def http_put params: {}, headers: {}, timeout: 10
  to_resp method: :put, headers: headers, params: params, timeout: timeout
end
to_resp(method: :get, headers: {}) click to toggle source
# File lib/dpr.rb, line 29
def to_resp method: :get, headers: {}, params: {}, timeout: 10
  if self =~ URI.regexp
    begin
      case method
        when :get
          url = self
          if params.is_a?(Hash) && params.length > 0
            if url.include? "?"
              url += "&"
            else
              url += "?"
            end
            uri = Addressable::URI.new
            uri.query_values = params
            url += uri.query
            url = url.gsub(/\s+/, '%20')
          end
          http_response = RestClient::Request.execute(:method => :get, :url => url, :headers => headers, :timeout => timeout)
        when :post
          http_response = RestClient::Request.execute(:method => :post, :url => self, :payload => params.to_json, :headers => headers, :timeout => timeout)
        when :put
          http_response = RestClient::Request.execute(:method => :put, :url => self, :payload => params, :headers => headers, :timeout => timeout)
        when :delete
          http_response = RestClient::Request.execute(:method => :delete, :url => self, :payload => params, :headers => headers, :timeout => timeout)
        when :patch
          http_response = RestClient::Request.execute(:method => :patch, :url => self, :payload => params, :headers => headers, :timeout => timeout)
      end
     rescue RestClient::RequestTimeout
       raise 'Request Timeout'
     rescue RestClient::Exception => e
       http_response = e.response
    end

    begin
      return JSON.parse(http_response.body), http_response.code
    rescue JSON::ParserError
      return {response: http_response.body}, http_response.code
    end
  else
    return {}, -1
  end
end