Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Likeno #103

Merged
merged 27 commits into from
Mar 30, 2016
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3a862a4
Migrate configuration loading and Cucumber Helpers to Likeno
danielkza Mar 21, 2016
9f3b86d
Update Likeno to version 0.0.1
Mar 21, 2016
8bc552d
Fix KalibroCucumberHelpers::cleaner behaviour
Mar 21, 2016
ed8cdae
Update likeno to v1.0.0
diegoamc Mar 23, 2016
159a316
[ci skip] Adapt Base to use Likeno
rafamanzo Mar 23, 2016
bb909a7
Update likeno to v1.0.1
diegoamc Mar 23, 2016
d150012
Bump likeno version
rafamanzo Mar 23, 2016
367c9f8
Fix cucumber steps relying on old error classes
rafamanzo Mar 24, 2016
91777fc
Use Likeno::Entity#all implementation on KalibroConfigurations
diegoamc Mar 28, 2016
eb46db3
Use Likeno::Entity#all implementation on ReadingGroups
diegoamc Mar 28, 2016
42bba5a
Use Likeno::Entity#all implementation on MetricCollectorDetails
diegoamc Mar 28, 2016
9558cac
Use Likeno::Entity#all implementation on Projects
diegoamc Mar 28, 2016
877a5bf
Use Likeno::Entity#all implementation on Repositories
diegoamc Mar 28, 2016
69d4ee0
Rescue Likeno errors on MetricCollectorDetails
diegoamc Mar 28, 2016
584fc8b
Use the default Likeno standard error class
diegoamc Mar 28, 2016
682d480
Use Likeno's implementation of RequestErrors
diegoamc Mar 28, 2016
f10e008
Use Likeno's implementation of RecordInvalid errors
diegoamc Mar 28, 2016
215c823
Rescue Likeno RecordNotFound errors on MetricConfiguration
diegoamc Mar 28, 2016
76aaa56
Rescue Likeno request errors on MetricConfiguration
diegoamc Mar 28, 2016
69e5153
Add deprecation warnings on error classes
diegoamc Mar 28, 2016
feefd01
Remove unnecessary mocks on Cleaner spec
diegoamc Mar 28, 2016
7e7ceba
Remove unused XML helpers
diegoamc Mar 28, 2016
b4ef614
Fix MetricConfiguration steps to correct handle errors
diegoamc Mar 28, 2016
49cccf1
Use Likeno errors instead of KalibroClient errors
diegoamc Mar 30, 2016
f1ed133
Use a more concise syntax to define kalibro_client's version
diegoamc Mar 30, 2016
fc95371
Release version v4.0.0.alpha1
diegoamc Mar 30, 2016
e0aacac
Fix excessive spaces in comment
danielkza Mar 30, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@is_error = false
begin
KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name!("Avalio")
rescue KalibroClient::Errors::RecordNotFound
rescue Likeno::Errors::RecordNotFound
@is_error = true
end
end
Expand Down
2 changes: 1 addition & 1 deletion features/step_definitions/module_result_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@is_error = false
begin
KalibroClient::Entities::Processor::ModuleResult.find(-1)
rescue KalibroClient::Errors::RecordNotFound
rescue Likeno::Errors::RecordNotFound
@is_error = true
end
end
Expand Down
3 changes: 0 additions & 3 deletions features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
# Kalibro hooks
require 'kalibro_client/kalibro_cucumber_helpers/hooks'

# Configuring the right hooks
KalibroClient::KalibroCucumberHelpers.configure_from_yml("#{__dir__}/kalibro_cucumber_helpers.yml")

# The gem itself
require 'kalibro_client'

Expand Down
1 change: 1 addition & 0 deletions kalibro_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ Gem::Specification.new do |spec|

spec.add_dependency "activesupport", ">= 2.2.1" #version in which underscore was introduced
spec.add_dependency "faraday_middleware", "~> 0.9"
spec.add_dependency "likeno", ">= 1.1", "< 2"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not "~> 1.1"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍷

end
43 changes: 4 additions & 39 deletions lib/kalibro_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,14 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

require 'yaml'
require 'logger'
require 'likeno'
require "kalibro_client/version"
require "kalibro_client/errors"
require "kalibro_client/entities"

module KalibroClient
@config = {
processor_address: "http://localhost:8082",
configurations_address: "http://localhost:8083"
}
# Apply default configuration to Likeno
Likeno.configure(processor_address: "http://localhost:8082",
configurations_address: "http://localhost:8083")

@valid_config_keys = @config.keys

@logger = Logger.new(STDOUT)

# Configure through hash
def KalibroClient.configure(opts = {})
opts.each {|k,v| @config[k.to_sym] = v if @valid_config_keys.include? k.to_sym}
end

# Configure through yaml file
def KalibroClient.configure_with(path_to_yaml_file)
begin
config = YAML::load(IO.read(path_to_yaml_file))
rescue Errno::ENOENT
logger.warn("YAML configuration file couldn't be found. Using defaults."); return
rescue Psych::SyntaxError
logger.warn("YAML configuration file contains invalid syntax. Using defaults."); return
end

configure(config)
end

def KalibroClient.config
@config
end

def KalibroClient.logger
@logger
end

def KalibroClient.logger=(logger)
@logger = logger
end
end
218 changes: 11 additions & 207 deletions lib/kalibro_client/entities/base.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is part of KalibroClient
# This file is part of KalibroClient
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extra space seems accidental.

# Copyright (C) 2013 it's respectives authors (please see the AUTHORS file)
#
# This program is free software: you can redistribute it and/or modify
Expand All @@ -14,218 +14,22 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

require 'faraday_middleware'
require 'kalibro_client/helpers/hash_converters'
require 'kalibro_client/helpers/request_methods'
require 'likeno'

module KalibroClient
module Entities
class Base
attr_accessor :kalibro_errors, :persisted

def initialize(attributes={}, persisted=false)
attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) }
@kalibro_errors = []
@persisted = persisted
end

def to_hash(options={})
hash = Hash.new
excepts = options[:except].nil? ? [] : options[:except]
excepts << "kalibro_errors"
excepts << "persisted"
fields.each do |field|
hash = field_to_hash(field).merge(hash) if !excepts.include?(field)
end
hash
end

def self.request(action, params = {}, method = :post, prefix="")
response = client.send(method) do |request|
url = "/#{endpoint}/#{action}".gsub(":id", params[:id].to_s)
url = "/#{prefix}#{url}" unless prefix.empty?
request.url url
request.body = params unless method == :get || params.empty?
request.options.timeout = 300
request.options.open_timeout = 300
end

if response.success?
response.body
# FIXME This condition was added to preserve the deprecated error codes that are returned by kalibro processor
elsif response.status == 404 || (response.body.key?('errors') && /NotFound/ === response.body['errors'])
raise KalibroClient::Errors::RecordNotFound.new(response: response)
else
raise KalibroClient::Errors::RequestError.new(response: response)
end
end


def self.to_object value
value.kind_of?(Hash) ? new(value, true) : value
end

def self.to_objects_array value
array = value.kind_of?(Array) ? value : [value]
array.each.map { |element| to_object(element) }
end

def save
if persisted?
self.update
else
without_request_error? do
response = self.class.request(save_action, save_params, :post, save_prefix)

self.id = response[instance_class_name]["id"]
self.created_at = response[instance_class_name]["created_at"] unless response[instance_class_name]["created_at"].nil?
self.updated_at = response[instance_class_name]["updated_at"] unless response[instance_class_name]["updated_at"].nil?
@persisted = true
end
end
end

def save!
return true if save
raise KalibroClient::Errors::RecordInvalid.new(self)
class Base < Likeno::Entity
# We do not track coverage here as it is deprecated and will be removed soon
# :nocov:
def kalibro_errors
STDERR.puts "DEPRECATED: use 'likeno_errors' instead of 'kalibro_errors'."
self.likeno_errors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a good opportunity to make the new API just errors. There's no need to expose likeno externally, it can be just an implementation detail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we prefix the errors with kalibro and likeno i order to avoid conflicts with Rails. But here is a issue to investigate this deeper: #106

end
# :nocov:

def self.create(attributes={})
new_model = new attributes
new_model.save
new_model
def self.module_name
'KalibroClient'
end

def update(attributes={})
attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) }
without_request_error? do
self.class.request(update_action, update_params, :put, update_prefix)
end
end

def ==(another)
unless self.class == another.class
return false
end

self.variable_names.each do |name|
next if name == "created_at" or name == "updated_at" or name == "persisted"
unless self.send("#{name}") == another.send("#{name}") then
return false
end
end

return true
end

def self.exists?(id)
request(exists_action, id_params(id), :get)['exists']
end

def self.find(id)
response = request(find_action, id_params(id), :get)
new(response[entity_name], true)
end

def destroy
without_request_error? do
response = self.class.request(destroy_action, destroy_params, :delete, destroy_prefix)
@persisted = false
end
end

def self.create_objects_array_from_hash (response)
create_array_from_hash(response[entity_name.pluralize]).map { |hash| new(hash, true) }
end

def self.create_array_from_hash (response)
response = [] if response.nil?
response = [response] if response.is_a?(Hash)
response
end

alias_method :persisted?, :persisted

protected

def instance_variable_names
instance_variables.map { |var| var.to_s }
end

def fields
instance_variable_names.each.collect { |variable| variable.to_s.sub(/@/, '') }
end

def variable_names
instance_variable_names.each.collect { |variable| variable.to_s.sub(/@/, '') }
end

def self.address
raise NotImplementedError
end

# TODO: probably the connection could be a class static variable.
def self.client
Faraday.new(:url => KalibroClient.config[address]) do |conn|
conn.request :json
conn.response :json, :content_type => /\bjson$/
conn.adapter Faraday.default_adapter # make requests with Net::HTTP
end
end

def self.is_valid?(field)
field.to_s[0] != '@' and field != :attributes! and (field =~ /attributes!/).nil? and (field.to_s =~ /xsi/).nil? and (field.to_s =~ /errors/).nil?
end

# TODO rename to instance_entity_name
def instance_class_name
self.class.entity_name
end

include RequestMethods
extend RequestMethods::ClassMethods

def add_error(exception)
@kalibro_errors << exception
end

def self.endpoint
entity_name.pluralize
end

def self.entity_name
# This loop is a generic way to make this work even when the children class has a different name
entity_class = self
until entity_class.name.include?("KalibroClient::Entities::") do
entity_class = entity_class.superclass
end

return entity_class.name.split("::").last.underscore.downcase
end

def without_request_error?(&block)
begin
block.call
true
rescue KalibroClient::Errors::RecordNotFound => error
raise error
rescue KalibroClient::Errors::RequestError => error
raise error if error.response.status.between?(500, 599)

response_errors = error.response.body['errors']
if response_errors.is_a?(Array)
response_errors.each { |error_msg| add_error(error_msg) }
elsif !response_errors.nil?
add_error response_errors
else
add_error error
end

false
end
end

include HashConverters
end
end
end
6 changes: 3 additions & 3 deletions lib/kalibro_client/entities/configurations/base.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require 'kalibro_client/helpers/date_attributes'
require 'likeno/helpers/date_attributes'

module KalibroClient
module Entities
module Configurations
class Base < KalibroClient::Entities::Base
include DateAttributes
include Likeno::DateAttributes

def self.address
:configurations_address
Likeno.config[:configurations_address]
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ def id=(value)
@id = value.to_i
end

def self.all
create_objects_array_from_hash(request('', {}, :get))
end

def metric_configurations
KalibroClient::Entities::Configurations::MetricConfiguration.create_objects_array_from_hash(self.class.request(':id/metric_configurations', {id: id}, :get))
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def weight=(value)
end

def update_attributes(attributes={})
attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) }
attributes.each { |field, value| send("#{field}=", value) if self.class.valid?(field) }
save
end

Expand All @@ -73,15 +73,15 @@ def self.find(id)
metric_configuration = request(':id', {id: id}, :get)
return new(metric_configuration['metric_configuration'], true)
#FIXME Temporary until KalibroProcessor returns proper http statuses
rescue KalibroClient::Errors::RequestError
rescue Likeno::Errors::RequestError
raise KalibroClient::Errors::RecordNotFound
end
end

def self.exists?(id)
begin
return true unless find(id).nil?
rescue KalibroClient::Errors::RecordNotFound
rescue Likeno::Errors::RecordNotFound
return false
end
end
Expand Down
6 changes: 1 addition & 5 deletions lib/kalibro_client/entities/configurations/reading_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Expand All @@ -25,10 +25,6 @@ def id=(value)
@id = value.to_i
end

def self.all
create_objects_array_from_hash(request('', {}, :get))
end

def readings
KalibroClient::Entities::Configurations::Reading.create_objects_array_from_hash(self.class.request(':id/readings', {id: @id}, :get))
end
Expand Down
Loading