module Vaultify class VaultifyController < ApplicationController require 'csv' require 'net/http' require 'base64' include EdtfHelper def initialize super @fields = Vaultify::Engine.config['fields'] end def upload end def translate @mvs = Vaultify::Engine.config['mvs'] csv = params['csv'].path @csv = ::CSV.parse(File.read(csv), headers: true, encoding: 'utf-8').map(&:to_hash) session[:csv] = ::CSV.parse(File.read(csv), headers: true, encoding: 'utf-8').map(&:to_hash) @total = @csv.length @csv.each_with_index do |row, index| next if row == nil @fields.each do |field_row| field = field_row.first api = field_row.last next if (row[field].to_s.empty? || api.to_s.empty?) adjusted_array = [] row[field].split(@mvs).each do |line| adjusted_array << send("#{api}First", line) rescue nil end @csv[index]["#{field}-adjusted"] = adjusted_array.join(@mvs) end end end def export @csv = session[:csv].deep_dup @puramses = params @fields.each do |field_row| next unless params.key? field_row.first.to_sym params.permit![field_row.first.to_sym].each do |csv_row, values| adj = [] values.each do |_, value| adj << value end @csv[csv_row.to_i][field_row.first] = adj.join('|') end end csv_output = [] csv_output << @csv.first.keys @csv.each do |row| csv_output << row.values end respond_to do |format| format.csv {render plain: csv_output.inject([]) {|csv, row| csv << CSV.generate_line(row)}.join, content_type: 'text/csv'} end #csv.each_with_index |row, index| #@fields.each do |field_row| # params[field_row.first.to_sym][index] # csv # end #end end def fast res = fastApi params['query'] respond_to do |format| format.json {render plain: res} format.text {render plain: res} end end def aat res = aatApi params['cleanQuery'] respond_to do |format| format.xml {render plain: res} format.text {render plain: res} format.json {render plain: res.to_json} end end def edtf date = convert_date params[:query] respond_to do |format| format.text {render plain: date} end end private def fastFirst query, queryIndex = 'suggestall', queryReturn = '%2Cidroot%2Cauth' res = JSON.parse(fastApi query, queryIndex, queryReturn) res['response']['docs'][0]['auth'] + 'url:' + res['response']['docs'][0]['idroot'] end def aatFirst query res = aatApi query.gsub(' AAT', '') #res = Hash.from_xml(res.gsub("\r\n", '').downcase).to_json.force_encoding('UTF-8')['response']['docs'][0]['auth'] res[0]['preferred_term'] + 'url:' + res[0]['subject_id'] end def isoFirst query # make list with matches, return the one with most letters matched # query.split('').each do |char| { if row.alpha3.includes? char { count++ } } #top = 0 #top_index = 0 #query_arr = query.split('') #isoVar.each_with_index do |row, index| isoVar.each do |row| return row[:English] + 'url:' + [:alpha2] if query == row[:"alpha3-b"] || query == row[:alpha2] || query == row[:English] =begin count = 0 query_arr.each do |ch| count += 1 if row[:"alpha3-b"].include? ch end if count > top top = count top_index = index end =end end #isoVar[top_index][:alpha2] nil end def dcmiFirst query dcmiVar.each do |row| return row[:text] + 'url:' + row[:url] if query == row[:text] end nil end def edtfFirst query date = convert_date query return date + 'url:' + date if date nil end def fastApi query, queryIndex = 'suggestall', queryReturn = '%2Cidroot%2Cauth' query = query.gsub(/[^a-zA-Z\d\s]/, '') url = 'http://fast.oclc.org/searchfast/fastsuggest?' + "&query=#{query}&queryIndex=#{queryIndex}&queryReturn=#{queryIndex + queryReturn}&suggest=autoSubject&rows=20" Net::HTTP.get_response(URI.parse(url)).body end def aatApi query url = URI('http://vocabsservices.getty.edu/AATService.asmx/AATGetTermMatch?term=' + query + '&logop=¬es=') req = Net::HTTP::Get.new(url) res = Net::HTTP.start(url.hostname, url.port) do |http| http.request(req) end Hash.from_xml(res.body.gsub("\r\n", '').downcase)['vocabulary']['subject'] end def dcmiVar [ {"text": 'Collection', "url": 'Collection'}, {"text": 'Dataset', "url": 'Dataset'}, {"text": 'Event', "url": 'Event'}, {"text": 'Image', "url": 'Image'}, {"text": 'InteractiveResource', "url": 'InteractiveResource'}, {"text": 'MovingImage', "url": 'MovingImage'}, {"text": 'PhysicalObject', "url": 'PhysicalObject'}, {"text": 'Service', "url": 'Service'}, {"text": 'Software', "url": 'Software'}, {"text": 'Sound', "url": 'Sound'}, {"text": 'StillImage', "url": 'StillImage'}, {"text": 'Text', "url": 'Text'} ] end def isoVar [ {"alpha3-b": "aar", "alpha2": "aa", "English": "Afar"}, {"alpha3-b": "abk", "alpha2": "ab", "English": "Abkhazian"}, {"alpha3-b": "afr", "alpha2": "af", "English": "Afrikaans"}, {"alpha3-b": "aka", "alpha2": "ak", "English": "Akan"}, {"alpha3-b": "alb", "alpha2": "sq", "English": "Albanian"}, {"alpha3-b": "amh", "alpha2": "am", "English": "Amharic"}, {"alpha3-b": "ara", "alpha2": "ar", "English": "Arabic"}, {"alpha3-b": "arg", "alpha2": "an", "English": "Aragonese"}, {"alpha3-b": "arm", "alpha2": "hy", "English": "Armenian"}, {"alpha3-b": "asm", "alpha2": "as", "English": "Assamese"}, {"alpha3-b": "ava", "alpha2": "av", "English": "Avaric"}, {"alpha3-b": "ave", "alpha2": "ae", "English": "Avestan"}, {"alpha3-b": "aym", "alpha2": "ay", "English": "Aymara"}, {"alpha3-b": "aze", "alpha2": "az", "English": "Azerbaijani"}, {"alpha3-b": "bak", "alpha2": "ba", "English": "Bashkir"}, {"alpha3-b": "bam", "alpha2": "bm", "English": "Bambara"}, {"alpha3-b": "baq", "alpha2": "eu", "English": "Basque"}, {"alpha3-b": "bel", "alpha2": "be", "English": "Belarusian"}, {"alpha3-b": "ben", "alpha2": "bn", "English": "Bengali"}, {"alpha3-b": "bih", "alpha2": "bh", "English": "Bihari languages"}, {"alpha3-b": "bis", "alpha2": "bi", "English": "Bislama"}, {"alpha3-b": "bos", "alpha2": "bs", "English": "Bosnian"}, {"alpha3-b": "bre", "alpha2": "br", "English": "Breton"}, {"alpha3-b": "bul", "alpha2": "bg", "English": "Bulgarian"}, {"alpha3-b": "bur", "alpha2": "my", "English": "Burmese"}, {"alpha3-b": "cat", "alpha2": "ca", "English": "Catalan; Valencian"}, {"alpha3-b": "cha", "alpha2": "ch", "English": "Chamorro"}, {"alpha3-b": "che", "alpha2": "ce", "English": "Chechen"}, {"alpha3-b": "chi", "alpha2": "zh", "English": "Chinese"}, {"alpha3-b": "chu", "alpha2": "cu", "English": "Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian; Old Church Slavonic"}, {"alpha3-b": "chv", "alpha2": "cv", "English": "Chuvash"}, {"alpha3-b": "cor", "alpha2": "kw", "English": "Cornish"}, {"alpha3-b": "cos", "alpha2": "co", "English": "Corsican"}, {"alpha3-b": "cre", "alpha2": "cr", "English": "Cree"}, {"alpha3-b": "cze", "alpha2": "cs", "English": "Czech"}, {"alpha3-b": "dan", "alpha2": "da", "English": "Danish"}, {"alpha3-b": "div", "alpha2": "dv", "English": "Divehi; Dhivehi; Maldivian"}, {"alpha3-b": "dut", "alpha2": "nl", "English": "Dutch; Flemish"}, {"alpha3-b": "dzo", "alpha2": "dz", "English": "Dzongkha"}, {"alpha3-b": "eng", "alpha2": "en", "English": "English"}, {"alpha3-b": "epo", "alpha2": "eo", "English": "Esperanto"}, {"alpha3-b": "est", "alpha2": "et", "English": "Estonian"}, {"alpha3-b": "ewe", "alpha2": "ee", "English": "Ewe"}, {"alpha3-b": "fao", "alpha2": "fo", "English": "Faroese"}, {"alpha3-b": "fij", "alpha2": "fj", "English": "Fijian"}, {"alpha3-b": "fin", "alpha2": "fi", "English": "Finnish"}, {"alpha3-b": "fre", "alpha2": "fr", "English": "French"}, {"alpha3-b": "fry", "alpha2": "fy", "English": "Western Frisian"}, {"alpha3-b": "ful", "alpha2": "ff", "English": "Fulah"}, {"alpha3-b": "geo", "alpha2": "ka", "English": "Georgian"}, {"alpha3-b": "ger", "alpha2": "de", "English": "German"}, {"alpha3-b": "gla", "alpha2": "gd", "English": "Gaelic; Scottish Gaelic"}, {"alpha3-b": "gle", "alpha2": "ga", "English": "Irish"}, {"alpha3-b": "glg", "alpha2": "gl", "English": "Galician"}, {"alpha3-b": "glv", "alpha2": "gv", "English": "Manx"}, {"alpha3-b": "gre", "alpha2": "el", "English": "Greek, Modern (1453-)"}, {"alpha3-b": "grn", "alpha2": "gn", "English": "Guarani"}, {"alpha3-b": "guj", "alpha2": "gu", "English": "Gujarati"}, {"alpha3-b": "hat", "alpha2": "ht", "English": "Haitian; Haitian Creole"}, {"alpha3-b": "hau", "alpha2": "ha", "English": "Hausa"}, {"alpha3-b": "heb", "alpha2": "he", "English": "Hebrew"}, {"alpha3-b": "her", "alpha2": "hz", "English": "Herero"}, {"alpha3-b": "hin", "alpha2": "hi", "English": "Hindi"}, {"alpha3-b": "hmo", "alpha2": "ho", "English": "Hiri Motu"}, {"alpha3-b": "hrv", "alpha2": "hr", "English": "Croatian"}, {"alpha3-b": "hun", "alpha2": "hu", "English": "Hungarian"}, {"alpha3-b": "ibo", "alpha2": "ig", "English": "Igbo"}, {"alpha3-b": "ice", "alpha2": "is", "English": "Icelandic"}, {"alpha3-b": "ido", "alpha2": "io", "English": "Ido"}, {"alpha3-b": "iii", "alpha2": "ii", "English": "Sichuan Yi; Nuosu"}, {"alpha3-b": "iku", "alpha2": "iu", "English": "Inuktitut"}, {"alpha3-b": "ile", "alpha2": "ie", "English": "Interlingue; Occidental"}, {"alpha3-b": "ina", "alpha2": "ia", "English": "Interlingua (International Auxiliary Language Association)"}, {"alpha3-b": "ind", "alpha2": "id", "English": "Indonesian"}, {"alpha3-b": "ipk", "alpha2": "ik", "English": "Inupiaq"}, {"alpha3-b": "ita", "alpha2": "it", "English": "Italian"}, {"alpha3-b": "jav", "alpha2": "jv", "English": "Javanese"}, {"alpha3-b": "jpn", "alpha2": "ja", "English": "Japanese"}, {"alpha3-b": "kal", "alpha2": "kl", "English": "Kalaallisut; Greenlandic"}, {"alpha3-b": "kan", "alpha2": "kn", "English": "Kannada"}, {"alpha3-b": "kas", "alpha2": "ks", "English": "Kashmiri"}, {"alpha3-b": "kau", "alpha2": "kr", "English": "Kanuri"}, {"alpha3-b": "kaz", "alpha2": "kk", "English": "Kazakh"}, {"alpha3-b": "khm", "alpha2": "km", "English": "Central Khmer"}, {"alpha3-b": "kik", "alpha2": "ki", "English": "Kikuyu; Gikuyu"}, {"alpha3-b": "kin", "alpha2": "rw", "English": "Kinyarwanda"}, {"alpha3-b": "kir", "alpha2": "ky", "English": "Kirghiz; Kyrgyz"}, {"alpha3-b": "kom", "alpha2": "kv", "English": "Komi"}, {"alpha3-b": "kon", "alpha2": "kg", "English": "Kongo"}, {"alpha3-b": "kor", "alpha2": "ko", "English": "Korean"}, {"alpha3-b": "kua", "alpha2": "kj", "English": "Kuanyama; Kwanyama"}, {"alpha3-b": "kur", "alpha2": "ku", "English": "Kurdish"}, {"alpha3-b": "lao", "alpha2": "lo", "English": "Lao"}, {"alpha3-b": "lat", "alpha2": "la", "English": "Latin"}, {"alpha3-b": "lav", "alpha2": "lv", "English": "Latvian"}, {"alpha3-b": "lim", "alpha2": "li", "English": "Limburgan; Limburger; Limburgish"}, {"alpha3-b": "lin", "alpha2": "ln", "English": "Lingala"}, {"alpha3-b": "lit", "alpha2": "lt", "English": "Lithuanian"}, {"alpha3-b": "ltz", "alpha2": "lb", "English": "Luxembourgish; Letzeburgesch"}, {"alpha3-b": "lub", "alpha2": "lu", "English": "Luba-Katanga"}, {"alpha3-b": "lug", "alpha2": "lg", "English": "Ganda"}, {"alpha3-b": "mac", "alpha2": "mk", "English": "Macedonian"}, {"alpha3-b": "mah", "alpha2": "mh", "English": "Marshallese"}, {"alpha3-b": "mal", "alpha2": "ml", "English": "Malayalam"}, {"alpha3-b": "mao", "alpha2": "mi", "English": "Maori"}, {"alpha3-b": "mar", "alpha2": "mr", "English": "Marathi"}, {"alpha3-b": "may", "alpha2": "ms", "English": "Malay"}, {"alpha3-b": "mlg", "alpha2": "mg", "English": "Malagasy"}, {"alpha3-b": "mlt", "alpha2": "mt", "English": "Maltese"}, {"alpha3-b": "mon", "alpha2": "mn", "English": "Mongolian"}, {"alpha3-b": "nau", "alpha2": "na", "English": "Nauru"}, {"alpha3-b": "nav", "alpha2": "nv", "English": "Navajo; Navaho"}, {"alpha3-b": "nbl", "alpha2": "nr", "English": "Ndebele, South; South Ndebele"}, {"alpha3-b": "nde", "alpha2": "nd", "English": "Ndebele, North; North Ndebele"}, {"alpha3-b": "ndo", "alpha2": "ng", "English": "Ndonga"}, {"alpha3-b": "nep", "alpha2": "ne", "English": "Nepali"}, {"alpha3-b": "nno", "alpha2": "nn", "English": "Norwegian Nynorsk; Nynorsk, Norwegian"}, {"alpha3-b": "nob", "alpha2": "nb", "English": "Bokmål, Norwegian; Norwegian Bokmål"}, {"alpha3-b": "nor", "alpha2": "no", "English": "Norwegian"}, {"alpha3-b": "nya", "alpha2": "ny", "English": "Chichewa; Chewa; Nyanja"}, {"alpha3-b": "oci", "alpha2": "oc", "English": "Occitan (post 1500); Provençal"}, {"alpha3-b": "oji", "alpha2": "oj", "English": "Ojibwa"}, {"alpha3-b": "ori", "alpha2": "or", "English": "Oriya"}, {"alpha3-b": "orm", "alpha2": "om", "English": "Oromo"}, {"alpha3-b": "oss", "alpha2": "os", "English": "Ossetian; Ossetic"}, {"alpha3-b": "pan", "alpha2": "pa", "English": "Panjabi; Punjabi"}, {"alpha3-b": "per", "alpha2": "fa", "English": "Persian"}, {"alpha3-b": "pli", "alpha2": "pi", "English": "Pali"}, {"alpha3-b": "pol", "alpha2": "pl", "English": "Polish"}, {"alpha3-b": "por", "alpha2": "pt", "English": "Portuguese"}, {"alpha3-b": "pus", "alpha2": "ps", "English": "Pushto; Pashto"}, {"alpha3-b": "que", "alpha2": "qu", "English": "Quechua"}, {"alpha3-b": "roh", "alpha2": "rm", "English": "Romansh"}, {"alpha3-b": "rum", "alpha2": "ro", "English": "Romanian; Moldavian; Moldovan"}, {"alpha3-b": "run", "alpha2": "rn", "English": "Rundi"}, {"alpha3-b": "rus", "alpha2": "ru", "English": "Russian"}, {"alpha3-b": "sag", "alpha2": "sg", "English": "Sango"}, {"alpha3-b": "san", "alpha2": "sa", "English": "Sanskrit"}, {"alpha3-b": "sin", "alpha2": "si", "English": "Sinhala; Sinhalese"}, {"alpha3-b": "slo", "alpha2": "sk", "English": "Slovak"}, {"alpha3-b": "slv", "alpha2": "sl", "English": "Slovenian"}, {"alpha3-b": "sme", "alpha2": "se", "English": "Northern Sami"}, {"alpha3-b": "smo", "alpha2": "sm", "English": "Samoan"}, {"alpha3-b": "sna", "alpha2": "sn", "English": "Shona"}, {"alpha3-b": "snd", "alpha2": "sd", "English": "Sindhi"}, {"alpha3-b": "som", "alpha2": "so", "English": "Somali"}, {"alpha3-b": "sot", "alpha2": "st", "English": "Sotho, Southern"}, {"alpha3-b": "spa", "alpha2": "es", "English": "Spanish; Castilian"}, {"alpha3-b": "srd", "alpha2": "sc", "English": "Sardinian"}, {"alpha3-b": "srp", "alpha2": "sr", "English": "Serbian"}, {"alpha3-b": "ssw", "alpha2": "ss", "English": "Swati"}, {"alpha3-b": "sun", "alpha2": "su", "English": "Sundanese"}, {"alpha3-b": "swa", "alpha2": "sw", "English": "Swahili"}, {"alpha3-b": "swe", "alpha2": "sv", "English": "Swedish"}, {"alpha3-b": "tah", "alpha2": "ty", "English": "Tahitian"}, {"alpha3-b": "tam", "alpha2": "ta", "English": "Tamil"}, {"alpha3-b": "tat", "alpha2": "tt", "English": "Tatar"}, {"alpha3-b": "tel", "alpha2": "te", "English": "Telugu"}, {"alpha3-b": "tgk", "alpha2": "tg", "English": "Tajik"}, {"alpha3-b": "tgl", "alpha2": "tl", "English": "Tagalog"}, {"alpha3-b": "tha", "alpha2": "th", "English": "Thai"}, {"alpha3-b": "tib", "alpha2": "bo", "English": "Tibetan"}, {"alpha3-b": "tir", "alpha2": "ti", "English": "Tigrinya"}, {"alpha3-b": "ton", "alpha2": "to", "English": "Tonga (Tonga Islands)"}, {"alpha3-b": "tsn", "alpha2": "tn", "English": "Tswana"}, {"alpha3-b": "tso", "alpha2": "ts", "English": "Tsonga"}, {"alpha3-b": "tuk", "alpha2": "tk", "English": "Turkmen"}, {"alpha3-b": "tur", "alpha2": "tr", "English": "Turkish"}, {"alpha3-b": "twi", "alpha2": "tw", "English": "Twi"}, {"alpha3-b": "uig", "alpha2": "ug", "English": "Uighur; Uyghur"}, {"alpha3-b": "ukr", "alpha2": "uk", "English": "Ukrainian"}, {"alpha3-b": "urd", "alpha2": "ur", "English": "Urdu"}, {"alpha3-b": "uzb", "alpha2": "uz", "English": "Uzbek"}, {"alpha3-b": "ven", "alpha2": "ve", "English": "Venda"}, {"alpha3-b": "vie", "alpha2": "vi", "English": "Vietnamese"}, {"alpha3-b": "vol", "alpha2": "vo", "English": "Volapük"}, {"alpha3-b": "wel", "alpha2": "cy", "English": "Welsh"}, {"alpha3-b": "wln", "alpha2": "wa", "English": "Walloon"}, {"alpha3-b": "wol", "alpha2": "wo", "English": "Wolof"}, {"alpha3-b": "xho", "alpha2": "xh", "English": "Xhosa"}, {"alpha3-b": "yid", "alpha2": "yi", "English": "Yiddish"}, {"alpha3-b": "yor", "alpha2": "yo", "English": "Yoruba"}, {"alpha3-b": "zha", "alpha2": "za", "English": "Zhuang; Chuang"}, {"alpha3-b": "zul", "alpha2": "zu", "English": "Zulu"} ] end end end