Class: ZAWS::Sumoapi::SumoClient

Inherits:
Object
  • Object
show all
Defined in:
lib/zaws/external/sumoapi/sumo_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(creds) ⇒ SumoClient

Returns a new instance of SumoClient.

Parameters:

  • creds (SumoClient::Creds)


11
12
13
# File 'lib/zaws/external/sumoapi/sumo_client.rb', line 11

def initialize(creds)
  @creds = creds
end

Instance Method Details

#delete(path, options = {}) ⇒ Object



39
40
41
42
# File 'lib/zaws/external/sumoapi/sumo_client.rb', line 39

def delete(path, options = {})
  options[:idempotent] = true
  json_request('DELETE', path, options)
end

#get(path, query = {}, options = {}) ⇒ Object

Make a GET request expecting a JSON response.



20
21
22
23
24
25
# File 'lib/zaws/external/sumoapi/sumo_client.rb', line 20

def get(path, query = {}, options = {})
  # Handle nil or empty Array
  options[:query] = query.to_h if query
  options[:idempotent] = true
  json_request('GET', path, options)
end

#json_request(method, path, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/zaws/external/sumoapi/sumo_client.rb', line 44

def json_request(method, path, options = {})
  if options[:body] && !options[:body].instance_of?(String)
    options[:body] = options[:body].to_json
  end
  options[:headers] ||= {}
  options[:headers]['Content-Type'] = 'application/json'
  response = request(method, path, options)
  JSON.parse(response.body) if (response.body.length > 0 && response.headers['content-type'].match(/json/))
end

#post(path, body, options = {}) ⇒ Object

Make a POST request expecting a JSON response.



28
29
30
31
# File 'lib/zaws/external/sumoapi/sumo_client.rb', line 28

def post(path, body, options = {})
  options[:body] = body
  json_request('POST', path, options)
end

#put(path, body, options = {}) ⇒ Object

Make a PUT request expecting a JSON response.



34
35
36
37
# File 'lib/zaws/external/sumoapi/sumo_client.rb', line 34

def put(path, body, options = {})
  options[:body] = body
  json_request('PUT', path, options)
end

#request(method, path, options = {}) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/zaws/external/sumoapi/sumo_client.rb', line 54

def request(method, path, options = {})
  connection = Excon.new(@creds.url, :user => "#{@creds.access_id}", :password => "#{@creds.access_key}")
  options[:expects] ||= [200]
  options[:method] = method
  options[:path] = path
  connection.request(options)
end

#urlObject



15
16
17
# File 'lib/zaws/external/sumoapi/sumo_client.rb', line 15

def url
  @creds.url
end