Class: RestAgent

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

Overview

Copyright © 2015, BROCADE COMMUNICATIONS SYSTEMS, INC

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this

list of conditions and the following disclaimer.

  1. Redistributions in binary form must reproduce the above copyright notice,

this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  1. Neither the name of the copyright holder nor the names of its contributors

may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_uri, headers: {}, username: nil, password: nil, open_timeout: nil) ⇒ RestAgent

Returns a new instance of RestAgent.



41
42
43
44
45
46
47
48
# File 'lib/utils/rest_agent.rb', line 41

def initialize(service_uri, headers: {}, username: nil, password: nil,
    open_timeout: nil)
  @service_uri = URI(service_uri)
  @headers = {'Content-Type' => 'application/json', 'Accept' => 'application/json'}.merge(headers)
  @username = username
  @password = password
  @open_timeout = open_timeout
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



37
38
39
# File 'lib/utils/rest_agent.rb', line 37

def headers
  @headers
end

#service_uriObject (readonly)

Returns the value of attribute service_uri.



36
37
38
# File 'lib/utils/rest_agent.rb', line 36

def service_uri
  @service_uri
end

Instance Method Details

#delete_request(uri_endpoint) ⇒ Object



78
79
80
81
82
# File 'lib/utils/rest_agent.rb', line 78

def delete_request(uri_endpoint)
  uri = @service_uri + URI(uri_endpoint)
  req = Net::HTTP::Delete.new(uri, @headers)
  return send_request(uri, req)
end

#get_request(uri_endpoint, query_params = {}) ⇒ Object



50
51
52
53
54
55
# File 'lib/utils/rest_agent.rb', line 50

def get_request(uri_endpoint, query_params = {})
  uri = @service_uri + URI(uri_endpoint)
  uri.query = URI.encode_www_form(query_params) unless query_params.empty?
  req = Net::HTTP::Get.new(uri, @headers)
  return send_request(uri, req)
end

#post_request(uri_endpoint, post_body, headers: {}) ⇒ Object



57
58
59
60
61
62
# File 'lib/utils/rest_agent.rb', line 57

def post_request(uri_endpoint, post_body, headers: {})
  uri = @service_uri + URI(uri_endpoint)
  req = Net::HTTP::Post.new(uri, @headers.merge(headers))
  req.body = post_body.is_a?(String) ? post_body : post_body.to_json
  return send_request(uri, req)
end

#put_request(uri_endpoint, put_body, headers: {}) ⇒ Object

def patch_request(uri_endpoint, patch_body, headers: {})

  uri = @service_uri + URI(uri_endpoint)
  req = Net::HTTP::Patch.new(uri, @headers.merge(headers))
  req.body = patch_body.to_json
  return send_request(uri, req)
end


71
72
73
74
75
76
# File 'lib/utils/rest_agent.rb', line 71

def put_request(uri_endpoint, put_body, headers: {})
  uri = @service_uri + URI(uri_endpoint)
  req = Net::HTTP::Put.new(uri, @headers.merge(headers))
  req.body = put_body.to_json
  return send_request(uri, req)
end