Class: KalibroGatekeeperClient::Entities::Model

Inherits:
Object
  • Object
show all
Extended by:
RequestMethods::ClassMethods
Includes:
HashConverters, RequestMethods
Defined in:
lib/kalibro_gatekeeper_client/entities/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RequestMethods::ClassMethods

exists_action, find_action, id_params

Methods included from HashConverters

#convert_to_hash, #date_with_milliseconds, #field_to_hash

Methods included from XMLConverters

#get_xml, #xml_instance_class_name

Methods included from RequestMethods

#destroy_action, #destroy_params, #save_action, #save_params

Constructor Details

#initialize(attributes = {}) ⇒ Model

Returns a new instance of Model.



26
27
28
29
# File 'lib/kalibro_gatekeeper_client/entities/model.rb', line 26

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

Instance Attribute Details

#kalibro_errorsObject

Returns the value of attribute kalibro_errors.



24
25
26
# File 'lib/kalibro_gatekeeper_client/entities/model.rb', line 24

def kalibro_errors
  @kalibro_errors
end

Class Method Details

.create(attributes = {}) ⇒ Object



78
79
80
81
82
# File 'lib/kalibro_gatekeeper_client/entities/model.rb', line 78

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

.create_array_from_hash(response) ⇒ Object



121
122
123
124
125
# File 'lib/kalibro_gatekeeper_client/entities/model.rb', line 121

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



117
118
119
# File 'lib/kalibro_gatekeeper_client/entities/model.rb', line 117

def self.create_objects_array_from_hash (response)
  create_array_from_hash(response).map { |hash| new hash }
end

.exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/kalibro_gatekeeper_client/entities/model.rb', line 97

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

.find(id) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/kalibro_gatekeeper_client/entities/model.rb', line 101

def self.find(id)
  if(exists?(id))
    new request(find_action, id_params(id))
  else
    raise KalibroGatekeeperClient::Errors::RecordNotFound
  end
end

.request(action, params = {}, method = :post) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/kalibro_gatekeeper_client/entities/model.rb', line 41

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

  response.body
end

.to_object(value) ⇒ Object



52
53
54
# File 'lib/kalibro_gatekeeper_client/entities/model.rb', line 52

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

.to_objects_array(value) ⇒ Object



56
57
58
59
# File 'lib/kalibro_gatekeeper_client/entities/model.rb', line 56

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



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/kalibro_gatekeeper_client/entities/model.rb', line 84

def ==(another)
  unless self.class == another.class then
    return false
  end
  self.variable_names.each {
    |name|
    unless self.send("#{name}") == another.send("#{name}") then
      return false
    end
  }
  true
end

#destroyObject



109
110
111
112
113
114
115
# File 'lib/kalibro_gatekeeper_client/entities/model.rb', line 109

def destroy
  begin
    self.class.request(destroy_action, destroy_params)
  rescue Exception => exception
    add_error exception
  end
end

#saveObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kalibro_gatekeeper_client/entities/model.rb', line 61

def save
  begin
    response = self.class.request(save_action, save_params)
    self.id = response["id"]
    self.kalibro_errors = response["kalibro_errors"] unless response["kalibro_errors"].nil?

    self.kalibro_errors.empty? ? true : false
  rescue Exception => exception
    add_error exception
    false
  end
end

#save!Object



74
75
76
# File 'lib/kalibro_gatekeeper_client/entities/model.rb', line 74

def save!
  save
end

#to_hash(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/kalibro_gatekeeper_client/entities/model.rb', line 31

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