class NexusAPI::NexusConnection
Constants
- VALID_RESPONSE_CODES
Attributes
continuation_token[RW]
Public Class Methods
new(username:, password:, hostname:, protocol:)
click to toggle source
# File lib/nexus_api/nexus_connection.rb, line 12 def initialize(username:, password:, hostname:, protocol:) @username = username @password = password @hostname = hostname @protocol = protocol end
Public Instance Methods
content_length(asset_url:)
click to toggle source
# File lib/nexus_api/nexus_connection.rb, line 66 def content_length(asset_url:) response = head(asset_url: asset_url) return -1 unless response.respond_to?(:headers) response.headers[:content_length] end
delete(endpoint:, headers: {'Content-Type' => 'application/json'}, api_version: 'v1')
click to toggle source
# File lib/nexus_api/nexus_connection.rb, line 50 def delete(endpoint:, headers: {'Content-Type' => 'application/json'}, api_version: 'v1') response = send_request( :delete, endpoint, headers: headers, api_version: api_version ) valid?(response) end
download(url:)
click to toggle source
# File lib/nexus_api/nexus_connection.rb, line 72 def download(url:) catch_connection_error do RestClient.get(URI.escape(url), authorization_header) end end
get(endpoint:, paginate: false, headers: {'Content-Type' => 'application/json'}, api_version: 'v1')
click to toggle source
# File lib/nexus_api/nexus_connection.rb, line 24 def get(endpoint:, paginate: false, headers: {'Content-Type' => 'application/json'}, api_version: 'v1') valid?(send_get(endpoint, paginate, headers, api_version)) end
get_response(endpoint:, paginate: false, headers: {'Content-Type' => 'application/json'}, api_version: 'v1')
click to toggle source
# File lib/nexus_api/nexus_connection.rb, line 19 def get_response(endpoint:, paginate: false, headers: {'Content-Type' => 'application/json'}, api_version: 'v1') response = send_get(endpoint, paginate, headers, api_version) response.nil? ? Hash.new : jsonize(response) end
head(asset_url:)
click to toggle source
# File lib/nexus_api/nexus_connection.rb, line 60 def head(asset_url:) catch_connection_error do RestClient.head(URI.escape(asset_url)) end end
paginate?()
click to toggle source
# File lib/nexus_api/nexus_connection.rb, line 78 def paginate? !@continuation_token.nil? end
post(endpoint:, parameters: '', headers: {'Content-Type' => 'application/json'}, api_version: 'v1')
click to toggle source
# File lib/nexus_api/nexus_connection.rb, line 28 def post(endpoint:, parameters: '', headers: {'Content-Type' => 'application/json'}, api_version: 'v1') response = send_request( :post, endpoint, parameters: parameters, headers: headers, api_version: api_version ) valid?(response) end
put(endpoint:, parameters: '', headers: {'Content-Type' => 'application/json'}, api_version: 'v1')
click to toggle source
# File lib/nexus_api/nexus_connection.rb, line 39 def put(endpoint:, parameters: '', headers: {'Content-Type' => 'application/json'}, api_version: 'v1') response = send_request( :put, endpoint, parameters: parameters, headers: headers, api_version: api_version ) valid?(response) end
Private Instance Methods
catch_connection_error() { || ... }
click to toggle source
# File lib/nexus_api/nexus_connection.rb, line 99 def catch_connection_error begin yield rescue SocketError => error return handle(error) rescue RestClient::Unauthorized => error return handle(error) rescue RestClient::Exceptions::ReadTimeout => error return handle(error) rescue RestClient::ExceptionWithResponse => error return handle(error.response) rescue StandardError => error return handle(error) end end
continuation_token_for(json)
click to toggle source
That's right, nexus has inconsistent null values for its api
# File lib/nexus_api/nexus_connection.rb, line 146 def continuation_token_for(json) return nil if json['continuationToken'].nil? return nil if json['continuationToken'] == 'nil' json['continuationToken'] end
handle(error)
click to toggle source
# File lib/nexus_api/nexus_connection.rb, line 90 def handle(error) puts "ERROR: Request failed" if error.is_a?(RestClient::Response) puts error.description else puts error.to_s end end
jsonize(response)
click to toggle source
# File lib/nexus_api/nexus_connection.rb, line 152 def jsonize(response) json = JSON.parse(response.body) if json.class == Hash @continuation_token = continuation_token_for(json) json = json["items"] if json["items"] end json rescue JSON::ParserError response.body end
send_get(endpoint, paginate, headers, api_version)
click to toggle source
# File lib/nexus_api/nexus_connection.rb, line 132 def send_get(endpoint, paginate, headers, api_version) url_marker = endpoint.include?('?') ? '&' : '?' # paginate answers is the user requesting pagination, paginate? answers does a continuation token exist # if an empty continuation token is included in the request we'll get an ArrayIndexOutOfBoundsException endpoint += "#{url_marker}continuationToken=#{@continuation_token}" if paginate && paginate? send_request( :get, endpoint, headers: headers, api_version: api_version ) end
send_request(connection_method, endpoint, parameters: '', headers: {}, api_version: 'v1')
click to toggle source
# File lib/nexus_api/nexus_connection.rb, line 119 def send_request(connection_method, endpoint, parameters: '', headers: {}, api_version: 'v1') parameters = parameters.to_json if headers['Content-Type'] == 'application/json' url = "#{@protocol}://#{@hostname}/service/rest/#{api_version}/#{endpoint}" catch_connection_error do RestClient::Request.execute( method: connection_method, url: URI.escape(url), payload: parameters, headers: authorization_header.merge(headers) ) end end
valid?(response)
click to toggle source
# File lib/nexus_api/nexus_connection.rb, line 85 def valid?(response) return false if response.nil? VALID_RESPONSE_CODES.include?(response.code) ? true : false end