class CocoapodsNexus::API::NexusConnection

Constants

VALID_RESPONSE_CODES

Attributes

continuation_token[RW]

Public Class Methods

new(hostname:) click to toggle source
# File lib/cocoapods-nexus/api/connection.rb, line 13
def initialize(hostname:)
  @hostname = hostname
end

Public Instance Methods

content_length(asset_url:) click to toggle source
# File lib/cocoapods-nexus/api/connection.rb, line 64
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/cocoapods-nexus/api/connection.rb, line 48
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/cocoapods-nexus/api/connection.rb, line 70
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/cocoapods-nexus/api/connection.rb, line 22
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/cocoapods-nexus/api/connection.rb, line 17
def get_response(endpoint:, paginate: false, headers: {'Content-Type' => 'application/json'}, api_version: 'v1')
  response = send_get(endpoint, paginate, headers, api_version)
  response.nil? ? {} : jsonize(response)
end
head(asset_url:) click to toggle source
# File lib/cocoapods-nexus/api/connection.rb, line 58
def head(asset_url:)
  catch_connection_error do
    RestClient.head(URI.escape(asset_url))
  end
end
paginate?() click to toggle source
# File lib/cocoapods-nexus/api/connection.rb, line 76
def paginate?
  !@continuation_token.nil?
end
post(endpoint:, parameters: '', headers: {'Content-Type' => 'application/json'}, api_version: 'v1') click to toggle source
# File lib/cocoapods-nexus/api/connection.rb, line 26
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/cocoapods-nexus/api/connection.rb, line 37
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

authorization_header() click to toggle source
# File lib/cocoapods-nexus/api/connection.rb, line 109
def authorization_header
  {}
end
catch_connection_error() { || ... } click to toggle source
# File lib/cocoapods-nexus/api/connection.rb, line 93
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/cocoapods-nexus/api/connection.rb, line 140
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/cocoapods-nexus/api/connection.rb, line 88
def handle(error)
  puts 'ERROR: Request failed'
  puts error
end
jsonize(response) click to toggle source
# File lib/cocoapods-nexus/api/connection.rb, line 146
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/cocoapods-nexus/api/connection.rb, line 126
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/cocoapods-nexus/api/connection.rb, line 113
def send_request(connection_method, endpoint, parameters: '', headers: {}, api_version: 'v1')
  parameters = parameters.to_json if headers['Content-Type'] == 'application/json'
  url = File.join(@hostname,"/nexus/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/cocoapods-nexus/api/connection.rb, line 83
def valid?(response)
  return false if response.nil?
  VALID_RESPONSE_CODES.include?(response.code) ? true : false
end