Class: BridgeBlueprint::RemoteData

Inherits:
Object
  • Object
show all
Defined in:
lib/bridge_blueprint/remote_data.rb

Constant Summary collapse

USERS_CSV_NAME =
'users.csv'
CUSTOM_FIELD_CSV_NAME =
'custom_fields.csv'
GRANTS_CSV_NAME =
'grants.csv'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, key, secret, models = nil) ⇒ RemoteData

Returns a new instance of RemoteData.



26
27
28
29
30
31
# 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

Instance Attribute Details

#clientObject

Returns the value of attribute client.



16
17
18
# File 'lib/bridge_blueprint/remote_data.rb', line 16

def client
  @client
end

Instance Method Details

#remote_urlObject



54
55
56
57
58
59
60
61
62
63
# 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_reportObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

#statusObject



48
49
50
51
52
# 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) ⇒ Object



65
66
67
68
69
70
71
72
# 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(URI.open(url), @file_path)
  end
end