Class: Apisync::Rails::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/apisync/rails/model.rb

Defined Under Namespace

Classes: MissingAttribute

Constant Summary collapse

REQUIRED_ATTRS =
{
  available: "This is required to enable/disable your item in our database.",
  content_language: "This is required to show your item to the correct audience.",
  ad_template_type: "This is required to generate the correct ads for this item."
}.freeze
WARNING_ATTRS =
{
  reference_id: "This is required to track your record, otherwise it will be created every time."
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Model

Returns a new instance of Model.



18
19
20
21
# File 'lib/apisync/rails/model.rb', line 18

def initialize(model)
  @model = model
  @attributes = {}
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



16
17
18
# File 'lib/apisync/rails/model.rb', line 16

def attributes
  @attributes
end

Instance Method Details

#attribute(attr_name, from: nil, value: nil) ⇒ Object



23
24
25
26
# File 'lib/apisync/rails/model.rb', line 23

def attribute(attr_name, from: nil, value: nil)
  @attributes.delete(attr_name)
  @attributes[attr_name] = attr_value(attr_name, from: from, value: value)
end

#custom_attribute(attr_name, from: nil, value: nil, name: nil) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/apisync/rails/model.rb', line 28

def custom_attribute(attr_name, from: nil, value: nil, name: nil)
  @attributes[:custom_attributes] ||= []
  @attributes[:custom_attributes] << {
    name: localized_name(name),
    identifier: attr_name.to_s,
    value: attr_value(attr_name, from: from, value: value)
  }
end

#log_warningsObject



52
53
54
55
56
57
58
# File 'lib/apisync/rails/model.rb', line 52

def log_warnings
  WARNING_ATTRS.each do |attr, message|
    if @attributes[attr].blank?
      ::Rails.logger.warn "Please specify #{attr}. #{message}"
    end
  end
end

#syncObject



37
38
39
40
41
42
# File 'lib/apisync/rails/model.rb', line 37

def sync
  set_reference_id
  validate!
  log_warnings
  Apisync::Rails::Http.post(@attributes)
end

#validate!Object



44
45
46
47
48
49
50
# File 'lib/apisync/rails/model.rb', line 44

def validate!
  REQUIRED_ATTRS.each do |attr, message|
    if @attributes[attr].blank?
      raise MissingAttribute, "Please specify #{attr}. #{message}"
    end
  end
end