Module: SendSonar::Client

Defined in:
lib/send_sonar/client.rb

Class Method Summary collapse

Class Method Details

.delete(url, payload, headers = {}, &block) ⇒ Object



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

def self.delete(url, payload, headers={}, &block)
  execute_with_exceptions { RestClient::Request.execute(:method => :delete, :url => url, :payload => payload, :headers => headers, &block) }
end

.execute_with_exceptionsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/send_sonar/client.rb', line 16

def self.execute_with_exceptions
  yield

  rescue Errno::ECONNREFUSED => e
    raise ConnectionRefused.new(e)

  rescue RestClient::RequestTimeout => e
    raise RequestTimeout.new(e)

  rescue RestClient::Exception => e
    if e.http_code == 400
      raise BadRequest.new(e)
    else
      response = e.response && JSON.parse(e.response) || {}
      error = response["error"]
      if exception_class = Exceptions::EXCEPTIONS_MAP[error]
        raise exception_class.new(e)
      else
        raise "SONAR ERROR: #{e.response} - #{e.message}"
      end
    end
end

.get(url, headers = {}, &block) ⇒ Object



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

def self.get(url, headers={}, &block)
  execute_with_exceptions { RestClient::Request.execute(:method => :get, :url => url, :headers => headers, &block) }
end

.post(url, payload, headers = {}, &block) ⇒ Object



4
5
6
# File 'lib/send_sonar/client.rb', line 4

def self.post(url, payload, headers={}, &block)
  execute_with_exceptions { RestClient::Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers, &block) }
end