Class: ToSpotlight::SpotlightTransferJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
app/jobs/to_spotlight/spotlight_transfer_job.rb

Instance Method Summary collapse

Instance Method Details

#base_urlObject



69
70
71
# File 'app/jobs/to_spotlight/spotlight_transfer_job.rb', line 69

def base_url
  @base_url ||= ::.where(tenant: Apartment::Tenant.current).first.cname
end

#create_cell(w, field) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'app/jobs/to_spotlight/spotlight_transfer_job.rb', line 77

def create_cell w, field
  if w.respond_to?((field + '_label').to_sym)
    w.send(field + '_label').join(';')
  elsif w.respond_to?(field.to_sym)
    if w.send(field).is_a?(Array)
      w.send(field).join(';')
    else
      w.send(field)
    end
  end
end

#empty_fs_line(fs_id, commas) ⇒ Object



73
74
75
# File 'app/jobs/to_spotlight/spotlight_transfer_job.rb', line 73

def empty_fs_line fs_id, commas
  "http://#{base_url}/downloads/#{fs_id}?locale=en" + (',' * commas)
end

#get_location(list) ⇒ Object



93
94
95
96
97
98
99
# File 'app/jobs/to_spotlight/spotlight_transfer_job.rb', line 93

def get_location list
  output = []
  list.each do |loc|
    output += loc.rdf_label || []
  end
  output.join('; ')
end

#get_resource(list) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'app/jobs/to_spotlight/spotlight_transfer_job.rb', line 101

def get_resource list
  output = []
  list.each do |url|
    output << if url.include? 'http'
                Hyrax::ResourceTypesService.label(url)
              else
                url
              end
  end
  output.join('; ')
end

#get_rights(list) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/jobs/to_spotlight/spotlight_transfer_job.rb', line 113

def get_rights list
  output = []
  list.each do |url|
    output << if url.include? 'http'
                ::VaultRightsStatementsService.label(url)
              else
                url
              end
  end
  #output = list.map { |url| url.include? "http" ? IaffRightsStatementService.label(url) : url }
  output.join('; ')
end

#perform(works, mappings, transfer) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/jobs/to_spotlight/spotlight_transfer_job.rb', line 5

def perform(works, mappings, transfer)
  base_url = ::.where(tenant: Apartment::Tenant.current).first.cname
  csv_headers = %w[url full_title_tesim] + mappings.keys + %w[children]
  csv_array = [csv_headers.join(',')]
  #items = []
  works.each do |wk|
    #if comp
    #  items << wk.id if wk.file_set_ids.length>1
    #  next unless wk.file_set_ids.length>1
    #else
    #  items << wk.id unless wk.file_set_ids.length>1
    #  next if wk.file_set_ids.length>1
    #end
    #index += 1
    comp = false #wk.file_set_ids.length > 1
    children = []
    if comp
      index = wk.ordered_file_set_ids.length * -1
      wk.ordered_file_set_ids.each do |f_id|
        csv_array << empty_fs_line(f_id, csv_headers.length - 1)
        children << index
        index += 1
      end
    end

    doc = ::SolrDocument.find wk.id

    line_hash = {}
    line_hash['url'] = "http://#{base_url}/concern/#{underscore_name(wk)}/#{wk.id}/manifest.json" #url
    line_hash['full_title_tesim'] = (doc.title.length > 1 ? doc.title.join('; ') : doc.title.first) #full_title_tesim

    mappings.each do |skey, field|
      line_hash[skey] = create_cell doc, field
    end

    line_hash['children'] = children.join('|')

    csv_array << line_hash.values_at(*csv_headers).map { |cell| cell = '' if cell.nil?; "\"#{cell.gsub("\"","\"\"")}\"" }.join(',')

  end

  dir = Rails.root.join('public', 'uploads', 'csvs')
  Dir.mkdir(dir) unless Dir.exist?(dir)
  time = DateTime.now.strftime('%s')
  filename = "spotlight_export-#{time}.csv"
  File.open(dir.join(filename), 'wb') do |file|
    file.write(csv_array.join("\n"))
  end
  res = post_response URI("#{transfer.spotlight_url}/from_hyrax/receive"),
                      csv_url:    "https://#{base_url}/uploads/csvs/#{filename}",
                      transfer:   transfer.attributes.slice(:user, :exhibit), #only allow user+exhibit
                      token:      'secret_token'

end

#post_response(uri, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
# File 'app/jobs/to_spotlight/spotlight_transfer_job.rb', line 60

def post_response uri, options={}
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json')
  request.body = options.to_json
  http.request(request)
end

#underscore_name(work) ⇒ Object



89
90
91
# File 'app/jobs/to_spotlight/spotlight_transfer_job.rb', line 89

def underscore_name work
  work.class.to_s.underscore + 's'
end