Class: Appsignal::AuthCheck Private

Inherits:
Object show all
Defined in:
lib/appsignal/auth_check.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class used to perform a Push API validation / authentication check against the AppSignal Push API.

Examples:

config = Appsignal::Config.new(Dir.pwd, "production")
auth_check = Appsignal::AuthCheck.new(config)
# Valid push_api_key
auth_check.perform # => "200"
# Invalid push_api_key
auth_check.perform # => "401"

Constant Summary collapse

ACTION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Path used on the AppSignal Push API https://push.appsignal.com/1/auth

"auth".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, logger = nil) ⇒ AuthCheck

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of AuthCheck.



25
26
27
28
29
30
31
# File 'lib/appsignal/auth_check.rb', line 25

def initialize(config, logger = nil)
  @config = config
  if logger # rubocop:disable Style/GuardClause
    warn "Deprecated: `logger` argument will be removed in the next " \
      "major version."
  end
end

Instance Attribute Details

#configAppsignal::Config (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns config to use in the authentication request.

Returns:



18
19
20
21
22
23
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
# File 'lib/appsignal/auth_check.rb', line 18

class AuthCheck
  # Path used on the AppSignal Push API
  # https://push.appsignal.com/1/auth
  ACTION = "auth".freeze

  attr_reader :config, :logger

  def initialize(config, logger = nil)
    @config = config
    if logger # rubocop:disable Style/GuardClause
      warn "Deprecated: `logger` argument will be removed in the next " \
        "major version."
    end
  end

  # Perform push api validation request and return response status code.
  #
  # @return [String] response status code.
  # @raise [StandardError] see {Appsignal::Transmitter#transmit}.
  def perform
    Appsignal::Transmitter.new(ACTION, config).transmit({}).code
  end

  # Perform push api validation request and return a descriptive response
  # tuple.
  #
  # @return [Array<String/nil, String>] response tuple.
  #   - First value is the response status code.
  #   - Second value is a description of the response and the exception error
  #     message if an exception occured.
  def perform_with_result
    status = perform
    result =
      case status
      when "200"
        "AppSignal has confirmed authorization!"
      when "401"
        "API key not valid with AppSignal..."
      else
        "Could not confirm authorization: " \
          "#{status.nil? ? "nil" : status}"
      end
    [status, result]
  rescue => e
    result = "Something went wrong while trying to "\
             "authenticate with AppSignal: #{e}"
    [nil, result]
  end
end

#loggerObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
# File 'lib/appsignal/auth_check.rb', line 23

def logger
  @logger
end

Instance Method Details

#performString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Perform push api validation request and return response status code.

Returns:

  • (String)

    response status code.

Raises:



37
38
39
# File 'lib/appsignal/auth_check.rb', line 37

def perform
  Appsignal::Transmitter.new(ACTION, config).transmit({}).code
end

#perform_with_resultArray<String/nil, String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Perform push api validation request and return a descriptive response tuple.

Returns:

  • (Array<String/nil, String>)

    response tuple.

    • First value is the response status code.
    • Second value is a description of the response and the exception error message if an exception occured.


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/appsignal/auth_check.rb', line 48

def perform_with_result
  status = perform
  result =
    case status
    when "200"
      "AppSignal has confirmed authorization!"
    when "401"
      "API key not valid with AppSignal..."
    else
      "Could not confirm authorization: " \
        "#{status.nil? ? "nil" : status}"
    end
  [status, result]
rescue => e
  result = "Something went wrong while trying to "\
           "authenticate with AppSignal: #{e}"
  [nil, result]
end