Class: HealthCheckToolbox::Services::RestServices

Inherits:
BaseHealthCheck show all
Defined in:
lib/health_check_toolbox/services/rest_services.rb

Constant Summary collapse

SERVICE =
'_service'

Constants inherited from BaseHealthCheck

BaseHealthCheck::STATUS_ERROR, BaseHealthCheck::STATUS_OK

Instance Attribute Summary collapse

Attributes inherited from BaseHealthCheck

#args, #block

Instance Method Summary collapse

Methods inherited from BaseHealthCheck

#build_response

Constructor Details

#initialize(args, block) ⇒ RestServices

Returns a new instance of RestServices.



14
15
16
17
18
# File 'lib/health_check_toolbox/services/rest_services.rb', line 14

def initialize(args, block)
  super(args, block)
  @services = []
  instance_eval(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

rubocop:disable Style/MissingRespondToMissing



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/health_check_toolbox/services/rest_services.rb', line 21

def method_missing(method, *args)
  method_name = method.to_s

  if method_name.include?(SERVICE)

    class_name = method_name.camelize

    HealthCheckToolbox::Services.const_set(class_name, class_service)

    services.push(
      "HealthCheckToolbox::Services::#{class_name}".constantize.new(args)
    )
  else
    super
  end
end

Instance Attribute Details

#servicesObject

Returns the value of attribute services.



10
11
12
# File 'lib/health_check_toolbox/services/rest_services.rb', line 10

def services
  @services
end

Instance Method Details

#class_serviceObject

rubocop:disable Metrics/MethodLength



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
67
68
69
70
# File 'lib/health_check_toolbox/services/rest_services.rb', line 40

def class_service
  Class.new(Object) do
    attr_reader :url

    def initialize(url)
      @url = url.first
    end

    def try_connect
      build_response(
        begin
          response = RestClient.get(url, headers)
          response.code == 200
        rescue StandardError
          false
        end
      )
    end

    private

    def headers
      { 'Authorization' => Wso2Toolbox::TokenManager.generate_token,
        'Content-Type' => 'application/json' }
    end

    def build_response(condition)
      condition ? STATUS_OK : STATUS_ERROR
    end
  end
end