Class: KalibroClient::Entities::Base
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
exists_action, find_action, id_params
#convert_to_hash, #date_with_milliseconds, #field_to_hash
#get_xml, #xml_instance_class_name
#destroy_action, #destroy_params, #destroy_prefix, #save_action, #save_params, #save_prefix, #update_params, #update_prefix
Constructor Details
#initialize(attributes = {}, persisted = false) ⇒ Base
Returns a new instance of Base.
26
27
28
29
30
|
# File 'lib/kalibro_client/entities/base.rb', line 26
def initialize(attributes={}, persisted=false)
attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) }
@kalibro_errors = []
@persisted = persisted
end
|
Instance Attribute Details
#kalibro_errors ⇒ Object
Returns the value of attribute kalibro_errors.
24
25
26
|
# File 'lib/kalibro_client/entities/base.rb', line 24
def kalibro_errors
@kalibro_errors
end
|
#persisted ⇒ Object
Also known as:
persisted?
Returns the value of attribute persisted.
24
25
26
|
# File 'lib/kalibro_client/entities/base.rb', line 24
def persisted
@persisted
end
|
Class Method Details
.create(attributes = {}) ⇒ Object
93
94
95
96
97
|
# File 'lib/kalibro_client/entities/base.rb', line 93
def self.create(attributes={})
new_model = new attributes
new_model.save
new_model
end
|
.create_array_from_hash(response) ⇒ Object
141
142
143
144
145
|
# File 'lib/kalibro_client/entities/base.rb', line 141
def self.create_array_from_hash (response)
response = [] if response.nil?
response = [response] if response.is_a?(Hash)
response
end
|
.create_objects_array_from_hash(response) ⇒ Object
137
138
139
|
# File 'lib/kalibro_client/entities/base.rb', line 137
def self.create_objects_array_from_hash (response)
create_array_from_hash(response[entity_name.pluralize]).map { |hash| new(hash, true) }
end
|
.exists?(id) ⇒ Boolean
121
122
123
|
# File 'lib/kalibro_client/entities/base.rb', line 121
def self.exists?(id)
request(exists_action, id_params(id), :get)['exists']
end
|
.find(id) ⇒ Object
125
126
127
128
|
# File 'lib/kalibro_client/entities/base.rb', line 125
def self.find(id)
response = request(find_action, id_params(id), :get)
new(response[entity_name], true)
end
|
.request(action, params = {}, method = :post, prefix = "") ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/kalibro_client/entities/base.rb', line 43
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
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
|
.to_object(value) ⇒ Object
64
65
66
|
# File 'lib/kalibro_client/entities/base.rb', line 64
def self.to_object value
value.kind_of?(Hash) ? new(value, true) : value
end
|
.to_objects_array(value) ⇒ Object
68
69
70
71
|
# File 'lib/kalibro_client/entities/base.rb', line 68
def self.to_objects_array value
array = value.kind_of?(Array) ? value : [value]
array.each.map { |element| to_object(element) }
end
|
Instance Method Details
#==(another) ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/kalibro_client/entities/base.rb', line 106
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
|
#destroy ⇒ Object
130
131
132
133
134
135
|
# File 'lib/kalibro_client/entities/base.rb', line 130
def destroy
without_request_error? do
response = self.class.request(destroy_action, destroy_params, :delete, destroy_prefix)
@persisted = false
end
end
|
#save ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/kalibro_client/entities/base.rb', line 73
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
|
#to_hash(options = {}) ⇒ Object
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/kalibro_client/entities/base.rb', line 32
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
|
#update(attributes = {}) ⇒ Object
99
100
101
102
103
104
|
# File 'lib/kalibro_client/entities/base.rb', line 99
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
|