Class: Netdot::RestClient
- Inherits:
-
Object
- Object
- Netdot::RestClient
- Defined in:
- lib/netdot/restclient.rb,
lib/netdot/restclient/version.rb
Overview
RestClient
Constant Summary collapse
- VERSION =
'2.0.3'
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#format ⇒ Object
Returns the value of attribute format.
-
#ua ⇒ Object
Returns the value of attribute ua.
-
#xs ⇒ Object
Returns the value of attribute xs.
Instance Method Summary collapse
-
#build_url(resource) ⇒ Object
Builds a URL for the specified resource.
-
#delete(resource) ⇒ Truth
Deletes a resource.
-
#extheader ⇒ Object
Builds the Extra headers.
-
#get(resource) ⇒ Hash
Gets a resource.
-
#initialize(argv = {}) ⇒ RestClient
constructor
Constructor and login method.
-
#post(resource, data) ⇒ Hash
Updates or creates a resource.
- #version ⇒ Object
Constructor Details
#initialize(argv = {}) ⇒ RestClient
Constructor and login method
24 25 26 27 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/netdot/restclient.rb', line 24 def initialize(argv = {}) [:server, :username, :password].each do |k| fail ArgumentError, "Missing required argument '#{k}'" unless argv[k] end argv.each { |k, v| instance_variable_set("@#{k}", v) } @timeout ||= 10 @retries ||= 3 @format ||= 'xml' @cookie_file ||= './cookie.dat' defined?(@ssl_verify) || @ssl_verify = true if (@format == 'xml') begin require 'xmlsimple' rescue LoadError raise LoadError, "Cannot load XML library. Try running 'gem install xml-simple'" end xs = XmlSimple.new('ForceArray' => true, 'KeyAttr' => 'id') @xs = xs else fail ArgumentError, 'Only XML formatting supported at this time' end ua = HTTPClient.new(agent_name: "Netdot::RestClient/#{version}") ua.("#{@cookie_file}") # SSL stuff if @ssl_verify if @ssl_ca_dir # We are told to add a certs path # We'll want to clear the default cert store first ua.ssl_config.clear_cert_store ua.ssl_config.set_trust_ca(@ssl_ca_dir) elsif @ssl_ca_file ua.ssl_config.set_trust_ca(@ssl_ca_file) end else ua.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE end # If version given, set it ua.ssl_config.ssl_version = @ssl_version if @ssl_version login_url = @server + '/NetdotLogin' resp = nil @retries.times do resp = ua.post login_url, 'destination' => 'index.html', 'credential_0' => @username, 'credential_1' => @password, 'permanent_session' => 1 if (resp.status == 302) ua. @ua = ua @base_url = @server + '/rest' break else $stderr.puts "Warning: Connection attempt to #{@server} failed" end end return if (resp.status == 302) fail "Could not log into #{@server}. Status Code: '#{resp.status}'" end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
9 10 11 |
# File 'lib/netdot/restclient.rb', line 9 def base_url @base_url end |
#format ⇒ Object
Returns the value of attribute format.
9 10 11 |
# File 'lib/netdot/restclient.rb', line 9 def format @format end |
#ua ⇒ Object
Returns the value of attribute ua.
9 10 11 |
# File 'lib/netdot/restclient.rb', line 9 def ua @ua end |
#xs ⇒ Object
Returns the value of attribute xs.
9 10 11 |
# File 'lib/netdot/restclient.rb', line 9 def xs @xs end |
Instance Method Details
#build_url(resource) ⇒ Object
Builds a URL for the specified resource.
102 103 104 |
# File 'lib/netdot/restclient.rb', line 102 def build_url(resource) base_url + '/' + resource end |
#delete(resource) ⇒ Truth
Deletes a resource.
138 139 140 141 142 143 144 145 146 |
# File 'lib/netdot/restclient.rb', line 138 def delete(resource) url = build_url(resource) resp = ua.delete(url, nil, extheader) if (resp.status == 200) return true else fail "Could not delete #{url}: #{resp.status}" end end |
#extheader ⇒ Object
Builds the Extra headers.
96 97 98 |
# File 'lib/netdot/restclient.rb', line 96 def extheader { 'Accept' => 'text/' + format + '; version=1.0' } end |
#get(resource) ⇒ Hash
Gets a resource.
109 110 111 112 113 114 115 116 117 |
# File 'lib/netdot/restclient.rb', line 109 def get(resource) url = build_url(resource) resp = ua.get(url, nil, extheader) if (resp.status == 200) xs.xml_in(resp.content) else fail "Could not get #{url}: #{resp.status}" end end |
#post(resource, data) ⇒ Hash
Updates or creates a resource.
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/netdot/restclient.rb', line 124 def post(resource, data) url = build_url(resource) fail ArgumentError, 'Data must be hash' unless data.is_a?(Hash) resp = ua.post(url, data, extheader) if (resp.status == 200) xs.xml_in(resp.content) else fail "Could not post to #{url}: #{resp.status}" end end |
#version ⇒ Object
7 8 9 |
# File 'lib/netdot/restclient/version.rb', line 7 def version VERSION end |