class BridgeBlueprint::RemoteData

Constants

CUSTOM_FIELD_CSV_NAME
GRANTS_CSV_NAME
USERS_CSV_NAME

Attributes

client[RW]

Public Class Methods

new(base_url, key, secret, models = nil) click to toggle source
# File lib/bridge_blueprint/remote_data.rb, line 26
def initialize(base_url, key, secret, models = nil)
  @base_url = base_url
  @auth_header = 'Basic ' + Base64.strict_encode64("#{key}:#{secret}")
  @client = BridgeAPI::Client.new(prefix: base_url, api_key: key, api_secret: secret)
  @models = models
end

Public Instance Methods

remote_url() click to toggle source
# File lib/bridge_blueprint/remote_data.rb, line 54
def remote_url
  uri = URI.parse(@base_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.read_timeout = 300
  http.use_ssl = (uri.scheme == 'https')
  req = Net::HTTP::Get.new("#{@base_url}/api/admin/data_dumps/download")
  req.add_field('Authorization', @auth_header)
  res = http.request(req)
  res['location']
end
start_data_report() click to toggle source
# File lib/bridge_blueprint/remote_data.rb, line 33
def start_data_report
  raw_dumps = get_dumps
  dumps = raw_dumps.members
  unless dumps.empty?
    dump = dumps.first
    case dump['status']
    when 'pending'
      return
    when 'complete'
      return if Time.parse(dump['date']) > Time.now - 300
    end
  end
  @models.present? ? @client.create_data_dump({only: @models}) : @client.create_data_dump
end
status() click to toggle source
# File lib/bridge_blueprint/remote_data.rb, line 48
def status
  dumps = get_dumps
  return get_dumps.first['status'] unless dumps.members.empty?
  BridgeBlueprint::Constants::STATUS_NOT_FOUND
end
store_file(path) click to toggle source
# File lib/bridge_blueprint/remote_data.rb, line 65
def store_file(path)
  @file_path = path
  url = remote_url
  raise 'Missing location url from bridge data dump response' if url.blank?
  File.open(@file_path, 'w') do |_file|
    IO.copy_stream(open(url), @file_path)
  end
end

Private Instance Methods

get_dumps() click to toggle source
# File lib/bridge_blueprint/remote_data.rb, line 76
def get_dumps
  @client.get_data_dumps
end
get_headers() click to toggle source
# File lib/bridge_blueprint/remote_data.rb, line 80
def get_headers
  {
    'Authorization' => @auth_header,
    'Content-Type' => 'application/json',
    'Accept' => 'application/json'
  }
end