Class: NexusAPI::NexusConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/nexus_api/nexus_connection.rb

Constant Summary collapse

VALID_RESPONSE_CODES =
[200, 201, 204].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username:, password:, hostname:) ⇒ NexusConnection

Returns a new instance of NexusConnection.



12
13
14
15
16
# File 'lib/nexus_api/nexus_connection.rb', line 12

def initialize(username:, password:, hostname:)
  @username = username
  @password = password
  @hostname = hostname
end

Instance Attribute Details

#continuation_tokenObject

Returns the value of attribute continuation_token.



10
11
12
# File 'lib/nexus_api/nexus_connection.rb', line 10

def continuation_token
  @continuation_token
end

Instance Method Details

#content_length(asset_url:) ⇒ Object



65
66
67
68
69
# File 'lib/nexus_api/nexus_connection.rb', line 65

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') ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/nexus_api/nexus_connection.rb', line 49

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:) ⇒ Object



71
72
73
74
75
# File 'lib/nexus_api/nexus_connection.rb', line 71

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') ⇒ Object



23
24
25
# File 'lib/nexus_api/nexus_connection.rb', line 23

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') ⇒ Object



18
19
20
21
# File 'lib/nexus_api/nexus_connection.rb', line 18

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:) ⇒ Object



59
60
61
62
63
# File 'lib/nexus_api/nexus_connection.rb', line 59

def head(asset_url:)
  catch_connection_error do
    RestClient.head(URI.escape(asset_url))
  end
end

#paginate?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/nexus_api/nexus_connection.rb', line 77

def paginate?
  !@continuation_token.nil?
end

#post(endpoint:, parameters: '', headers: {'Content-Type' => 'application/json'}, api_version: 'v1') ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/nexus_api/nexus_connection.rb', line 27

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') ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/nexus_api/nexus_connection.rb', line 38

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