Class: KalibroClient::Entities::Base

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

Direct Known Subclasses

Configurations::Base, Processor::Base

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, #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_errorsObject

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

#persistedObject 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



162
163
164
165
166
# File 'lib/kalibro_client/entities/base.rb', line 162

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



158
159
160
# File 'lib/kalibro_client/entities/base.rb', line 158

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

Returns:

  • (Boolean)


125
126
127
# File 'lib/kalibro_client/entities/base.rb', line 125

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

.find(id) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/kalibro_client/entities/base.rb', line 129

def self.find(id)
  if(exists?(id))
    response = request(find_action, id_params(id), :get)
    new(response[entity_name], true)
  else
    raise KalibroClient::Errors::RecordNotFound
  end
end

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



43
44
45
46
47
48
49
50
51
52
53
54
# 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 params.empty?
    request.options.timeout = 300
    request.options.open_timeout = 300
  end

  response.body
end

.to_object(value) ⇒ Object



56
57
58
# File 'lib/kalibro_client/entities/base.rb', line 56

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

.to_objects_array(value) ⇒ Object



60
61
62
63
# File 'lib/kalibro_client/entities/base.rb', line 60

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/kalibro_client/entities/base.rb', line 110

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

#destroyObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/kalibro_client/entities/base.rb', line 138

def destroy
  begin
    response = self.class.request(destroy_action, destroy_params, :delete, destroy_prefix)

    unless response['errors'].nil?
      response['errors'].each { |error| add_error(error) }
    end

    if self.kalibro_errors.empty?
      @persisted = false
      true
    else
      false
    end
  rescue Exception => exception
    add_error exception
    false
  end
end

#saveObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/kalibro_client/entities/base.rb', line 65

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

      if response["errors"].nil?
        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
        true
      else
        self.kalibro_errors = response["errors"]
        false
      end
    rescue Exception => exception
      add_error exception
      false
    end
  end
end

#save!Object



89
90
91
# File 'lib/kalibro_client/entities/base.rb', line 89

def save!
  save
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
105
106
107
108
# 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) }
  response = self.class.request(update_action, update_params, :put, update_prefix)
  unless response["errors"].nil?
    response["errors"].each { |error| add_error(error) }
    false
  else
    true
  end
end