lib/hawkular/hawkular_client.rb in hawkular-client-5.0.0.pre1 vs lib/hawkular/hawkular_client.rb in hawkular-client-5.0.0.pre2

- old
+ new

@@ -1,52 +1,44 @@ require 'hawkular/inventory/inventory_api' -require 'hawkular/metrics/metrics_client' require 'hawkular/alerts/alerts_api' require 'hawkular/tokens/tokens_api' require 'hawkular/operations/operations_api' +require 'hawkular/prometheus/prometheus_api' require 'hawkular/base_client' module Hawkular class Client - attr_reader :inventory, :metrics, :alerts, :operations, :tokens, :state - def initialize(hash) hash[:credentials] ||= {} hash[:options] ||= {} fail Hawkular::ArgumentError, 'no parameter ":entrypoint" given' if hash[:entrypoint].nil? @state = hash end + def respond_to_missing?(method_name, include_private = false) + delegate_client = client_for_method(method_name) + return super if delegate_client.nil? + + method = submethod_name_for(method_name) + return super unless delegate_client.respond_to?(method) + + true + end + def method_missing(name, *args, &block) - delegate_client = case name - when /^inventory_/ then inventory - when /^metrics_/ then metrics - when /^alerts_/ then alerts - when /^operations_/ then operations - when /^tokens_/ then tokens - else - fail Hawkular::ArgumentError, "unknown method prefix `#{name}`, allowed prefixes:"\ - '`inventory_`, `metrics_`,`alerts_`,`operations_`, `tokens_`' - end - method = name.to_s.sub(/^[^_]+_/, '') - delegate_client.__send__(method, *args, &block) + super unless respond_to?(name) + client_for_method(name).__send__(submethod_name_for(name), *args, &block) end def inventory @inventory ||= Inventory::Client.new("#{@state[:entrypoint]}/hawkular/inventory", @state[:credentials], @state[:options]) end - def metrics - @metrics ||= Metrics::Client.new("#{@state[:entrypoint]}/hawkular/metrics", - @state[:credentials], - @state[:options]) - end - def alerts @alerts ||= Alerts::Client.new("#{@state[:entrypoint]}/hawkular/alerts", @state[:credentials], @state[:options]) end @@ -62,15 +54,35 @@ @tokens ||= Token::Client.new(@state[:entrypoint], @state[:credentials], @state[:options]) end + def prometheus + @prometheus ||= Prometheus::Client.new(@state[:entrypoint], + @state[:credentials], + @state[:options]) + end + private # this is in a dedicated method, because constructor opens the websocket connection to make the handshake def init_operations_client Operations::Client.new(entrypoint: @state[:entrypoint], credentials: @state[:credentials], options: @state[:options]) + end + + def client_for_method(method_name) + case method_name + when /^inventory_/ then inventory + when /^alerts_/ then alerts + when /^operations_/ then operations + when /^tokens_/ then tokens + when /^prometheus_/ then prometheus + end + end + + def submethod_name_for(method_name) + method_name.to_s.sub(/^[^_]+_/, '') end end end