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



17
18
19
# 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: {}, headers: {}, timeout: 10) ⇒ Object



9
10
11
# 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: {}, headers: {}, timeout: 10) ⇒ Object



25
26
27
# 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: {}, headers: {}, timeout: 10) ⇒ Object



13
14
15
# 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: {}, headers: {}, timeout: 10) ⇒ Object



21
22
23
# 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: {}, params: {}, timeout: 10) ⇒ Object



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
66
67
68
69
70
# 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