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
22
23
# File 'lib/apisync/rails/model.rb', line 18

def initialize(model)
  @model = model
  @attributes = {}
  @payload = {}
  @should_sync = true
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



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

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

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



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

def custom_attribute(attr_name, from: nil, value: nil, identifier: nil, label:)
  @attributes[:custom_attributes] ||= []
  @attributes[:custom_attributes] << {
    attr_name: attr_name,
    from: from,
    value: value,
    identifier: identifier,
    label: label
  }
end

#log_warnings(payload) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/apisync/rails/model.rb', line 79

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

#syncObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/apisync/rails/model.rb', line 45

def sync
  if sync?
    payload = generate_payload
    payload = set_reference_id(payload)
    validate!(payload)
    log_warnings(payload)

    Apisync::Rails::Extensions.setup

    if defined?(::Sidekiq)
      Apisync::Rails::SyncModelJob::Sidekiq.perform_async(
        @model.class.name,
        @model.id,
        payload
      )
    else
      Apisync::Rails::Http.post(
        payload,
        request_concurrency: :synchronous
      )
    end
  end
end

#sync_if(method_name) ⇒ Object



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

def sync_if(method_name)
  @should_sync = @model.send(method_name.to_sym)
end

#validate!(payload) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/apisync/rails/model.rb', line 69

def validate!(payload)
  return unless sync?

  REQUIRED_ATTRS.each do |attr, message|
    if payload[attr].blank?
      raise MissingAttribute, "Please specify '#{attr}'. #{message}"
    end
  end
end