Class: KPM::NexusFacade::NexusApiCallsV2

Inherits:
Object
  • Object
show all
Defined in:
lib/kpm/nexus_helper/nexus_api_calls_v2.rb

Overview

This is an extract and slim down of functions needed from nexus_cli to maintain the response expected by the base_artifact.

Constant Summary collapse

PULL_ARTIFACT_ENDPOINT =
'/service/local/artifact/maven/redirect'
GET_ARTIFACT_INFO_ENDPOINT =
'/service/local/artifact/maven/resolve'
SEARCH_FOR_ARTIFACT_ENDPOINT =
'/service/local/lucene/search'
READ_TIMEOUT_DEFAULT =
60
OPEN_TIMEOUT_DEFAULT =
60
ERROR_MESSAGE_404 =
'The artifact you requested information for could not be found. Please ensure it exists inside the Nexus.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, ssl_verify, logger) ⇒ NexusApiCallsV2

Returns a new instance of NexusApiCallsV2.



44
45
46
47
48
# File 'lib/kpm/nexus_helper/nexus_api_calls_v2.rb', line 44

def initialize(configuration, ssl_verify, logger)
  @configuration = configuration
  @ssl_verify = ssl_verify
  @logger = logger
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



40
41
42
# File 'lib/kpm/nexus_helper/nexus_api_calls_v2.rb', line 40

def configuration
  @configuration
end

#loggerObject

Returns the value of attribute logger.



42
43
44
# File 'lib/kpm/nexus_helper/nexus_api_calls_v2.rb', line 42

def logger
  @logger
end

#ssl_verifyObject (readonly)

Returns the value of attribute ssl_verify.



41
42
43
# File 'lib/kpm/nexus_helper/nexus_api_calls_v2.rb', line 41

def ssl_verify
  @ssl_verify
end

#versionObject (readonly)

Returns the value of attribute version.



39
40
41
# File 'lib/kpm/nexus_helper/nexus_api_calls_v2.rb', line 39

def version
  @version
end

Instance Method Details

#get_artifact_info(coordinates) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kpm/nexus_helper/nexus_api_calls_v2.rb', line 65

def get_artifact_info(coordinates)
  logger.debug "Entered - Get artifact info, coordinates: #{coordinates}"
  response = get_response(coordinates, GET_ARTIFACT_INFO_ENDPOINT, nil)

  case response.code
  when '200'
    logger.debug "response body: #{response.body}"
    return response.body
  when '404'
    raise StandardError, ERROR_MESSAGE_404
  else
    raise UnexpectedStatusCodeException, response.code
  end
end

#pull_artifact(coordinates, destination) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/kpm/nexus_helper/nexus_api_calls_v2.rb', line 80

def pull_artifact(coordinates, destination)
  logger.debug "Entered - Pull artifact, coordinates: #{coordinates}"
  file_name = get_file_name(coordinates)
  destination = File.join(File.expand_path(destination || '.'), file_name)
  logger.debug "destination: #{destination}"
  response = get_response(coordinates, PULL_ARTIFACT_ENDPOINT, nil)

  case response.code
  when '301', '307'
    location = response['Location'].gsub!(configuration[:url], '')
    logger.debug 'fetching artifact'
    file_response = get_response(nil, location, nil)

    File.open(destination, 'wb') do |io|
      io.write(file_response.body)
    end
  when 404
    raise StandardError, ERROR_MESSAGE_404
  else
    raise UnexpectedStatusCodeException, response.code
  end
  {
    file_name: file_name,
    file_path: File.expand_path(destination),
    version: version,
    size: File.size(File.expand_path(destination))
  }
end

#search_for_artifacts(coordinates) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kpm/nexus_helper/nexus_api_calls_v2.rb', line 50

def search_for_artifacts(coordinates)
  logger.debug "Entered - Search for artifact, coordinates: #{coordinates}"
  response = get_response(coordinates, SEARCH_FOR_ARTIFACT_ENDPOINT, %i[g a])

  case response.code
  when '200'
    logger.debug "response body: #{response.body}"
    return response.body
  when '404'
    raise StandardError, ERROR_MESSAGE_404
  else
    raise UnexpectedStatusCodeException, response.code
  end
end