Skip to content

Commit

Permalink
Remove offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
xeviknal committed Dec 11, 2017
1 parent 4a677cc commit 6ac022c
Show file tree
Hide file tree
Showing 21 changed files with 176 additions and 175 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ end

RuboCop::RakeTask.new

task default: [:rubocop, :spec]
task default: %i[rubocop spec]
27 changes: 12 additions & 15 deletions hawkularclient.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'hawkular/version'
Expand All @@ -7,35 +6,33 @@ Gem::Specification.new do |gem|
gem.name = 'hawkular-client'
gem.version = Hawkular::VERSION
gem.authors = ['Libor Zoubek', 'Heiko W. Rupp', 'Jirka Kremser', 'Federico Simoncelli']
gem.email = %w([email protected] [email protected] [email protected])
gem.email = %w[[email protected] [email protected] [email protected]]
gem.homepage = 'https://github.com/hawkular/hawkular-client-ruby'
gem.summary = 'A Ruby client for Hawkular'
gem.license = 'Apache-2.0'
gem.required_ruby_version = '>= 2.2.0'
gem.description = <<-EOS
A Ruby client for Hawkular
EOS
gem.description = 'A Ruby client for Hawkular'

gem.files = `git ls-files -z lib LICENSE README.rdoc`.split("\x0")
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ['lib']

gem.add_runtime_dependency('addressable')
gem.add_runtime_dependency('rest-client', '~> 2.0.0')
gem.add_runtime_dependency('websocket-client-simple', '~> 0.3.0')
gem.add_runtime_dependency('addressable')
gem.add_development_dependency('shoulda')
gem.add_development_dependency('rspec-rails', '~> 3.1')
gem.add_development_dependency('actionpack', '~> 4')
gem.add_development_dependency('actionpack', '~> 4')
gem.add_development_dependency('coveralls')
gem.add_development_dependency('pry-byebug')
gem.add_development_dependency('rack', '~> 1.6.4')
gem.add_development_dependency('rake', '< 11')
gem.add_development_dependency('rspec-rails', '~> 3.1')
gem.add_development_dependency('rubocop', '= 0.51.0')
gem.add_development_dependency('shoulda')
gem.add_development_dependency('simple-websocket-vcr', '= 0.1.0')
gem.add_development_dependency('yard')
gem.add_development_dependency('webmock', '~> 3.0')
gem.add_development_dependency('vcr')
gem.add_development_dependency('rubocop', '= 0.51.0')
gem.add_development_dependency('coveralls')
gem.add_development_dependency('rack', '~> 1.6.4')
gem.add_development_dependency('pry-byebug')
gem.add_development_dependency('webmock', '~> 3.0')
gem.add_development_dependency('yard')

gem.rdoc_options << '--title' << gem.name <<
'--main' << 'README.rdoc' << '--line-numbers' << '--inline-source'
Expand Down
24 changes: 11 additions & 13 deletions lib/hawkular/alerts/alerts_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,7 @@ def delete_group_trigger(trigger_id)
# Obtains action definition/plugin from the server.
# @param [String] action_plugin Id of the action plugin to fetch. If nil, all the plugins are fetched
def get_action_definition(action_plugin = nil)
if action_plugin.nil?
plugins = http_get('plugins')
else
plugins = [action_plugin]
end
plugins = action_plugin.nil? ? http_get('plugins') : [action_plugin]
ret = {}
plugins.each do |p|
ret[p] = http_get("/plugins/#{p}")
Expand Down Expand Up @@ -232,7 +228,8 @@ def delete_action(plugin, action_id)
# Obtain the alerts for the Trigger with the passed id
# @param [String] trigger_id Id of the trigger that has fired the alerts
# @return [Array<Alert>] List of alerts for the trigger. Can be empty
def get_alerts_for_trigger(trigger_id) # TODO: add additional filters
def get_alerts_for_trigger(trigger_id)
# TODO: add additional filters
return [] unless trigger_id

url = '/?triggerIds=' + trigger_id
Expand All @@ -254,7 +251,7 @@ def list_alerts(criteria = {})
# @param [Array] tenants optional list of tenants. The elements of the array can be any object
# convertible to a string
# @return [Array<Alert>] List of alerts in the system. Can be empty
def alerts(criteria: {}, tenants:nil)
def alerts(criteria: {}, tenants: nil)
query = generate_query_params(criteria)
uri = tenants ? '/admin/alerts/' : '/'
ret = http_get(uri + query, multi_tenants_header(tenants))
Expand Down Expand Up @@ -389,7 +386,7 @@ class Trigger
attr_accessor :auto_resolve, :auto_resolve_alerts, :tags, :type
attr_accessor :tenant, :description, :group, :severity, :event_type, :event_category, :member_of, :data_id_map
attr_reader :conditions, :dampenings
attr_accessor :enabled, :actions, :firing_match, :auto_resolve_match
attr_accessor :enabled, :firing_match, :auto_resolve_match

def initialize(trigger_hash)
return if trigger_hash.nil?
Expand Down Expand Up @@ -427,9 +424,10 @@ def to_h
ret = x.to_s.split('_').collect(&:capitalize).join
ret[0, 1].downcase + ret[1..-1]
end
fields = [:id, :name, :enabled, :severity, :auto_resolve, :auto_resolve_alerts, :event_type, :event_category,
:description, :auto_enable, :auto_disable, :context, :type, :tags, :member_of, :data_id_map,
:firing_match, :auto_resolve_match]
fields = %i[id name enabled severity auto_resolve auto_resolve_alerts
event_type event_category description auto_enable auto_disable
context type tags member_of data_id_map firing_match
auto_resolve_match]

fields.each do |field|
camelized_field = to_camel.call(field)
Expand Down Expand Up @@ -604,8 +602,8 @@ def status_by(status)
end

# for some API back compatibility
alias_method :ackBy, :ack_by
alias_method :resolvedBy, :resolved_by
alias ackBy ack_by
alias resolvedBy resolved_by
end

# Representation of one event.
Expand Down
6 changes: 3 additions & 3 deletions lib/hawkular/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def rest_client(suburl)
opts[:user] = @credentials[:username]
opts[:password] = @credentials[:password]
# strip @endpoint in case suburl is absolute
suburl = suburl[@entrypoint.length, suburl.length] if suburl.match(/^http/)
suburl = suburl[@entrypoint.length, suburl.length] if suburl =~ /^http/
RestClient::Resource.new(@entrypoint, opts)[suburl]
end

Expand Down Expand Up @@ -131,12 +131,12 @@ def base_64_credentials(credentials = {})
# @param params [Hash] key-values pairs
# @return [String] complete query string to append to a base url, '' if no valid params
def generate_query_params(params = {})
params = params.select { |_k, v| !(v.nil? || ((v.instance_of? Array) && v.empty?)) }
params = params.reject { |_k, v| v.nil? || ((v.instance_of? Array) && v.empty?) }
return '' if params.empty?

params.inject('?') do |ret, (k, v)|
ret += '&' unless ret == '?'
part = (v.instance_of? Array) ? "#{k}=#{v.join(',')}" : "#{k}=#{v}"
part = v.instance_of?(Array) ? "#{k}=#{v.join(',')}" : "#{k}=#{v}"
ret + hawk_escape(part)
end
end
Expand Down
9 changes: 5 additions & 4 deletions lib/hawkular/hawkular_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

module Hawkular
class Client
attr_reader :inventory, :alerts, :operations, :tokens, :state, :prometheus

def initialize(hash)
hash[:credentials] ||= {}
hash[:options] ||= {}
Expand All @@ -18,6 +16,10 @@ def initialize(hash)
@state = hash
end

def respond_to_missing?(method_name, include_private = false)
method_name.to_s.start_with?('inventory_', 'alerts_', 'operations_', 'tokens_', 'prometheus_') || super
end

def method_missing(name, *args, &block)
delegate_client = case name
when /^inventory_/ then inventory
Expand All @@ -26,8 +28,7 @@ def method_missing(name, *args, &block)
when /^tokens_/ then tokens
when /^prometheus_/ then prometheus
else
fail Hawkular::ArgumentError, "unknown method prefix `#{name}`, allowed prefixes:"\
'`inventory_`, `alerts_`, `operations_`, `tokens_`, `prometheus_`'
super
end
method = name.to_s.sub(/^[^_]+_/, '')
delegate_client.__send__(method, *args, &block)
Expand Down
10 changes: 3 additions & 7 deletions lib/hawkular/inventory/entities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Operation

def initialize(op_hash)
@name = op_hash['name']
@params = (op_hash.key? 'parameters') ? op_hash['parameters'] : {}
@params = op_hash.key?('parameters') ? op_hash['parameters'] : {}
end
end

Expand All @@ -61,7 +61,7 @@ def initialize(hash)
end

def ==(other)
self.equal?(other) || other.class == self.class && other.id == @id
equal?(other) || other.class == self.class && other.id == @id
end

# Returns a hash representation of the resource type
Expand All @@ -86,10 +86,6 @@ class Resource
attr_reader :properties
# @return [Hash<String,String>] Config map of this resource
attr_reader :config
# @return [List<Metric>] Metrics associated to this resource
attr_reader :metrics
# @return [List<Resource>] List of children (present when the whole tree is loaded, else nil)
attr_reader :children

def initialize(hash)
@id = hash['id']
Expand Down Expand Up @@ -126,7 +122,7 @@ def metrics_by_family(family)
end

def ==(other)
self.equal?(other) || other.class == self.class && other.id == @id
equal?(other) || other.class == self.class && other.id == @id
end

# Returns a hash representation of the resource
Expand Down
41 changes: 24 additions & 17 deletions lib/hawkular/metrics/metric_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def tags_param(tags)
# @param starts [Integer] optional timestamp (default now - 8h)
# @param ends [Integer] optional timestamp (default now)
# @return [Array[Hash]] datapoints
def data_by_tags(tags, buckets: nil, bucketDuration:nil, start:nil, ends: nil)
def data_by_tags(tags, buckets: nil, bucketDuration: nil, # rubocop:disable Naming/VariableName
start: nil, ends: nil)
data = {
tags: tags_param(tags), buckets: buckets, bucketDuration: bucketDuration, start: start, end: ends
}
Expand Down Expand Up @@ -56,7 +57,7 @@ def push_data(gauges: [], counters: [], availabilities: [], strings: [])
strings.each { |g| default_timestamp g[:data] }
data = { gauges: gauges, counters: counters, availabilities: availabilities, strings: strings }
path = '/metrics/'
@legacy_api ? path << 'data' : path << 'raw'
path << (@legacy_api ? 'data' : 'raw')
http_post(path, data)
end

Expand Down Expand Up @@ -84,7 +85,7 @@ def query_stats(gauge_ids: [], counter_ids: [], avail_ids: [], rates: false, sta
path = '/metrics/stats/query'
metrics = { gauge: gauge_ids, counter: counter_ids, availability: avail_ids }
data = { metrics: metrics, start: starts, end: ends, bucketDuration: bucket_duration }
data['types'] = %w(gauge gauge_rate counter counter_rate availability) if rates
data['types'] = %w[gauge gauge_rate counter counter_rate availability] if rates
http_post(path, data)
end

Expand All @@ -93,9 +94,11 @@ def query_stats(gauge_ids: [], counter_ids: [], avail_ids: [], rates: false, sta
def tags
tags = []
http_get('/metrics/').map do |g|
next if g['tags'].nil?

g['tags'].map do |k, v|
tags << { k => v }
end unless g['tags'].nil?
end
end
tags.uniq!
end
Expand Down Expand Up @@ -171,7 +174,7 @@ def update_tags(metric_definition)
def push_data(id, data)
data = [data] unless data.is_a?(Array)
uri = "/#{@resource}/#{ERB::Util.url_encode(id)}/"
@legacy_api ? uri << 'data' : uri << 'raw'
uri << (@legacy_api ? 'data' : 'raw')
@client.default_timestamp data
@client.http_post(uri, data)
end
Expand All @@ -187,8 +190,8 @@ def push_data(id, data)
# @param order [String] optional Data point sort order, based on timestamp (ASC, DESC)
# @return [Array[Hash]] datapoints
# @see #push_data #push_data for datapoint detail
def get_data(id, starts: nil, ends: nil, bucketDuration: nil, buckets: nil, percentiles: nil, limit: nil,
order: nil)
def get_data(id, starts: nil, ends: nil, bucketDuration: nil, # rubocop:disable Naming/VariableName
buckets: nil, percentiles: nil, limit: nil, order: nil)
params = { start: starts, end: ends, bucketDuration: bucketDuration, buckets: buckets,
percentiles: percentiles, limit: limit, order: order }
get_data_helper(id, params)
Expand All @@ -214,28 +217,32 @@ def raw_data(ids, starts: nil, ends: nil, limit: nil, order: nil)
# @param buckets [Integer] optional number of buckets
# @return [Array[Hash]] datapoints
# @see #push_data #push_data for datapoint detail
def get_data_by_tags(tags, starts: nil, ends: nil, bucketDuration: nil, buckets:nil)
def get_data_by_tags(tags, starts: nil, ends: nil, bucketDuration: nil, # rubocop:disable Naming/VariableName
buckets: nil)
params = { tags: @client.tags_param(tags), start: starts,
end: ends, bucketDuration: bucketDuration, buckets: buckets }
path = "/#{@resource}/"
@legacy_api ? path << 'data/?' : path << 'stats/?'
path << (@legacy_api ? 'data/?' : 'stats/?')
resp = @client.http_get(path + encode_params(params))
resp.is_a?(Array) ? resp : [] # API returns no content (empty Hash) instead of empty array
end

def encode_params(params)
URI.encode_www_form(params.select { |_k, v| !v.nil? })
URI.encode_www_form(params.reject { |_k, v| v.nil? })
end

private

def get_data_helper(id, params)
path = "/#{@resource}/#{ERB::Util.url_encode(id)}/"
if @legacy_api
path << 'data/?'
else
(params[:bucketDuration].nil? && params[:buckets].nil?) ? path << 'raw/?' : path << 'stats/?'
end
path << if @legacy_api
'data/?'
elsif params[:bucketDuration].nil? && params[:buckets].nil?
'raw/?'
else
'stats/?'
end

path << encode_params(params)
resp = @client.http_get(path)
resp.is_a?(Array) ? resp : [] # API returns no content (empty Hash) instead of empty array
Expand Down Expand Up @@ -329,8 +336,8 @@ def initialize(client)
# @param order [String] optional Data point sort order, based on timestamp (ASC, DESC)
# @return [Array[Hash]] datapoints
# @see #push_data #push_data for datapoint detail
def get_data(id, starts: nil, ends: nil, bucketDuration: nil, buckets: nil, distinct: nil, limit: nil,
order: nil)
def get_data(id, starts: nil, ends: nil, bucketDuration: nil, # rubocop:disable Naming/VariableName
buckets: nil, distinct: nil, limit: nil, order: nil)
params = { start: starts, end: ends, bucketDuration: bucketDuration, buckets: buckets,
distinct: distinct, limit: limit, order: order }
get_data_helper(id, params)
Expand Down
2 changes: 1 addition & 1 deletion lib/hawkular/metrics/metrics_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def check_version
version = version_status_hash['Implementation-Version']
major, minor = version.scan(/\d+/).map(&:to_i)
fail Hawkular::Exception, fail_version_msg if major.nil? || minor.nil?
@legacy_api = (major == 0 && minor < 16)
@legacy_api = (major.zero? && minor < 16)
end

# Construct a new Hawkular Metrics client class.
Expand Down
Loading

0 comments on commit 6ac022c

Please sign in to comment.