Module: MongoHTTPSync::Importer

Included in:
MongoImporter, MongoidImporter
Defined in:
lib/mongo_http_sync/importer.rb

Instance Method Summary collapse

Instance Method Details

#import(url, key: :id, entity: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mongo_http_sync/importer.rb', line 12

def import(url, key: :id, entity: nil)
  last_updated_doc = find_last_updated_doc
  newest_update = last_updated_doc['updated_at'] unless last_updated_doc.nil?
  unless newest_update.nil?
    url = append_query_param(url, 'updated_since', newest_update.utc.strftime('%Y-%m-%d %H:%M:%S.%L UTC'))
  end
  Parser.parse(HTTP.get(url).body) do |json|
    json[:_id] = json.delete(key)
    json[:updated_at] = Time.parse(json[:updated_at])
    json = entity.represent(json).as_json if entity.present?
    upsert(json)
  end
end

#initialize(output) ⇒ Object



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

def initialize(output)
  @output = output
end