Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/dpr.rb

Instance Method Summary collapse

Instance Method Details

#http_delete(params: {}, headers: {}, timeout: 10) ⇒ Object



16
17
18
# File 'lib/dpr.rb', line 16

def http_delete params: {}, headers: {}, timeout: 10
  to_resp method: :delete, headers: headers, params: params, timeout: timeout
end

#http_get(params: {}, headers: {}, timeout: 10) ⇒ Object



8
9
10
# File 'lib/dpr.rb', line 8

def http_get params: {}, headers: {}, timeout: 10
  to_resp method: :get, headers: headers, params: params, timeout: timeout
end

#http_patch(params: {}, headers: {}, timeout: 10) ⇒ Object



24
25
26
# File 'lib/dpr.rb', line 24

def http_patch params: {}, headers: {}, timeout: 10
  to_resp method: :patch, headers: headers, params: params, timeout: timeout
end

#http_post(params: {}, headers: {}, timeout: 10) ⇒ Object



12
13
14
# File 'lib/dpr.rb', line 12

def http_post params: {}, headers: {}, timeout: 10
  to_resp method: :post, headers: headers, params: params, timeout: timeout
end

#http_put(params: {}, headers: {}, timeout: 10) ⇒ Object



20
21
22
# File 'lib/dpr.rb', line 20

def http_put params: {}, headers: {}, timeout: 10
  to_resp method: :put, headers: headers, params: params, timeout: timeout
end

#to_resp(method: :get, headers: {}, params: {}, timeout: 10) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dpr.rb', line 28

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, :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
    # response = RestClient::Request.execute(method: http_method, url: self, headers: headers, parameters: params)
    return JSON.parse(http_response.body), http_response.code
  else
    return {}, -1
  end
end