module MongoHTTPSync::Importer

Public Class Methods

new(output) click to toggle source
# File lib/mongo_http_sync/importer.rb, line 8
def initialize(output)
  @output = output
end

Public Instance Methods

import(url, key: :id, entity: nil) click to toggle source
# 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

Protected Instance Methods

append_query_param(url, key, value) click to toggle source
# File lib/mongo_http_sync/importer.rb, line 28
def append_query_param(url, key, value)
  uri =  URI.parse(url)
  new_query_ar = URI.decode_www_form(String(uri.query)) << [key, value]
  uri.query = URI.encode_www_form(new_query_ar)
  url = uri.to_s
end