-
Notifications
You must be signed in to change notification settings - Fork 3
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
Likeno #103
Changes from 23 commits
3a862a4
9f3b86d
8bc552d
ed8cdae
159a316
bb909a7
d150012
367c9f8
91777fc
eb46db3
42bba5a
9558cac
877a5bf
69d4ee0
584fc8b
682d480
f10e008
215c823
76aaa56
69e5153
feefd01
7e7ceba
b4ef614
49cccf1
f1ed133
fc95371
e0aacac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like a good opportunity to make the new API just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we prefix the errors with |
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
"~> 1.1"
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🍷