Class: SyncableModels::Importer::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/syncable_models/importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Import

Returns a new instance of Import.



10
11
12
13
14
15
# File 'lib/syncable_models/importer.rb', line 10

def initialize(name)
  @name = name.to_s
  @models = {}
  @interval = 5.minutes
  @timeout = 10.seconds
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



7
8
9
# File 'lib/syncable_models/importer.rb', line 7

def api_key
  @api_key
end

#api_urlObject

Returns the value of attribute api_url.



7
8
9
# File 'lib/syncable_models/importer.rb', line 7

def api_url
  @api_url
end

#destinationObject

Returns the value of attribute destination.



7
8
9
# File 'lib/syncable_models/importer.rb', line 7

def destination
  @destination
end

#intervalObject

Returns the value of attribute interval.



7
8
9
# File 'lib/syncable_models/importer.rb', line 7

def interval
  @interval
end

#modelsObject (readonly)

Returns the value of attribute models.



8
9
10
# File 'lib/syncable_models/importer.rb', line 8

def models
  @models
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/syncable_models/importer.rb', line 7

def name
  @name
end

#timeoutObject

Returns the value of attribute timeout.



7
8
9
# File 'lib/syncable_models/importer.rb', line 7

def timeout
  @timeout
end

Instance Method Details

#fetch_request(params) ⇒ Object



70
71
72
73
# File 'lib/syncable_models/importer.rb', line 70

def fetch_request(params)
  conn = Faraday.new(url: api_url + params[:fetch_path])
  conn.get '', params_with_api_key(destination: destination)
end

#import(model_names = []) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/syncable_models/importer.rb', line 40

def import(model_names=[])
  model_names = [model_names] unless model_names.is_a?(Array)
  model_names.map!(&:to_s)
  selected_models = model_names.any? ?
    @models.select{ |k, v| k.in? model_names } :
    @models

  selected_models.each do |model_name, params|
    puts "[SyncableModels::Importer] Importing #{model_name.underscore.pluralize}..."

    next if model_names.any? && !model_name.in?(model_names)

    response = fetch_request(params)

    if response.success?
      response = JSON.parse(response.body)
      if response["status"].to_i == 401
        puts "[SyncableModels::Importer] Wrong api key!"
      end

      synced_ids = []

      synced_ids += sync_update model_name, params, response['for_sync']
      synced_ids += sync_destroy model_name, params, response['for_destroy']

      sync_request(params, synced_ids) if synced_ids.any?
    end
  end
end

#import_model(class_name, args = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/syncable_models/importer.rb', line 17

def import_model(class_name, args = {})
  class_name = class_name.to_s if class_name.is_a?(Class)
  fetch_path = args[:fetch_path] || class_name.underscore.pluralize
  sync_path = args[:sync_path] || "sync_" + class_name.underscore.pluralize
  api_id_key = args[:api_id_key] || :uuid
  external_id_column = args[:external_id_column] || :external_id
  @models[class_name] = {
    fetch_path: fetch_path,
    sync_path: sync_path,
    api_id_key: api_id_key,
    external_id_column: external_id_column
  }
end

#params_with_api_key(params) ⇒ Object



35
36
37
38
# File 'lib/syncable_models/importer.rb', line 35

def params_with_api_key(params)
  params.merge!(key: api_key) if api_key
  params
end

#sync_destroy(model_name, params, ids) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/syncable_models/importer.rb', line 97

def sync_destroy(model_name, params, ids)
  synced_ids = []

  if ids
    klass = model_name.constantize

    ids.each do |id|
      result = klass.where(params[:external_id_column].to_s => id).first.try(:destroy)
      puts "[SyncableModels::Importer] Destroying #{model_name} (external_id=#{id}): #{ result ? 'OK' : 'FAIL' }"
      synced_ids << id if result
    end
  end

  synced_ids
end

#sync_request(params, ids) ⇒ Object



75
76
77
78
# File 'lib/syncable_models/importer.rb', line 75

def sync_request(params, ids)
  conn = Faraday.new(url: api_url + params[:sync_path])
  conn.get '', params_with_api_key(destination: destination, ids: ids)
end

#sync_update(model_name, params, objects) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/syncable_models/importer.rb', line 80

def sync_update(model_name, params, objects)
  synced_ids = []

  if objects
    klass = model_name.constantize

    objects.each do |o|
      id = o[params[:api_id_key].to_s]
      result = klass.from_import_hash(o)
      puts "[SyncableModels::Importer] Importing #{model_name} (external_id=#{id}): #{ result ? 'OK' : 'FAIL' }"
      synced_ids << id if result
    end
  end

  synced_ids
end