Class: Mei::Loc
Instance Attribute Summary
Attributes included from WebServiceBase
Class Method Summary collapse
- .app_root ⇒ Object
- .env ⇒ Object
- .ldf_config ⇒ Object
- .ldf_config_path ⇒ Object
- .pop_graph(value) ⇒ Object
- .qskos(value) ⇒ Object
Instance Method Summary collapse
- #build_query_url(q) ⇒ Object
- #get_skos_concepts(subject) ⇒ Object
-
#initialize(subauthority, solr_field) ⇒ Loc
constructor
A new instance of Loc.
-
#loc_response_to_qa(data, counter) ⇒ Object
Simple conversion from LoC-based struct to QA hash.
-
#parse_authority_response ⇒ Object
Reformats the data received from the LOC service.
- #response_to_struct(response) ⇒ Object
- #search(q) ⇒ Object
- #solr_clean(term) ⇒ Object
Methods included from WebServiceBase
#get_json, #get_xml, #request_options
Constructor Details
#initialize(subauthority, solr_field) ⇒ Loc
Returns a new instance of Loc.
44 45 46 47 48 |
# File 'lib/mei/loc.rb', line 44 def initialize(, solr_field) = @solr_field = solr_field #RestClient.enable Rack::Cache end |
Class Method Details
.app_root ⇒ Object
17 18 19 20 21 22 |
# File 'lib/mei/loc.rb', line 17 def self.app_root return @app_root if @app_root @app_root = Rails.root if defined?(Rails) and defined?(Rails.root) @app_root ||= APP_ROOT if defined?(APP_ROOT) @app_root ||= '.' end |
.env ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/mei/loc.rb', line 24 def self.env return @env if @env #The following commented line always returns "test" in a rails c production console. Unsure of how to fix this yet... #@env = ENV["RAILS_ENV"] = "test" if ENV @env ||= Rails.env if defined?(Rails) and defined?(Rails.root) @env ||= 'development' end |
.ldf_config ⇒ Object
12 13 14 15 |
# File 'lib/mei/loc.rb', line 12 def self.ldf_config @ldf_config ||= YAML::load(File.open(ldf_config_path))[env] .with_indifferent_access end |
.ldf_config_path ⇒ Object
32 33 34 |
# File 'lib/mei/loc.rb', line 32 def self.ldf_config_path File.join(app_root, 'config', 'mei.yml') end |
.pop_graph(value) ⇒ Object
8 9 10 |
# File 'lib/mei/loc.rb', line 8 def self.pop_graph(value) RDF::Graph.load("#{Mei::Loc.ldf_config[:ldf_server]}#{value}.ttl", format: :ttl) end |
.qskos(value) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/mei/loc.rb', line 36 def self.qskos value if value.match(/^sh\d+/) return ::RDF::URI.new("http://id.loc.gov/authorities/subjects/#{value}") else return ::RDF::URI.new("http://www.w3.org/2004/02/skos/core##{value}") end end |
Instance Method Details
#build_query_url(q) ⇒ Object
56 57 58 59 60 |
# File 'lib/mei/loc.rb', line 56 def build_query_url q escaped_query = URI.escape(q) = Qa::Authorities::Loc.() + URI.escape() return "http://id.loc.gov/search/?q=#{escaped_query}&q=#{authority_fragment}&format=json" end |
#get_skos_concepts(subject) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/mei/loc.rb', line 136 def get_skos_concepts subject broader_list = [] narrower_list = [] variant_list = [] #xml_response = Nokogiri::XML(response).remove_namespaces! repo = pop_graph(subject) repo.query(:subject=>::RDF::URI.new(subject), :predicate=>Mei::Loc.qskos('broader')).each_statement do |result_statement| if !result_statement.object.literal? and result_statement.object.uri? broader_label = nil broader_uri = result_statement.object.to_s #if Mei::Loc.repo.query(:subject=>::RDF::URI.new(broader_uri), :predicate=>Mei::Loc.qskos('narrower'), :object=>::RDF::URI.new(subject)).count > 0 valid = false broader_repo = pop_graph(broader_uri) broader_repo.query(:subject=>::RDF::URI.new(broader_uri)).each_statement do |broader_statement| if broader_statement.predicate.to_s == Mei::Loc.qskos('prefLabel') broader_label ||= broader_statement.object.value if broader_statement.object.literal? end if broader_statement.predicate.to_s == Mei::Loc.qskos('member') valid = true if broader_statement.object.to_s == 'http://id.loc.gov/authorities/subjects/collection_LCSHAuthorizedHeadings' end end broader_label ||= broader_uri broader_list << {:uri_link=>broader_uri, :label=>broader_label} if valid #end end end puts 'pre-a' repo.query(:subject=>::RDF::URI.new(subject), :predicate=>Mei::Loc.qskos('narrower')).each_statement do |result_statement| puts 'a' if !result_statement.object.literal? and result_statement.object.uri? narrower_label = nil narrower_uri = result_statement.object.to_s valid = false narrower_repo = pop_graph(narrower_uri) puts 'b' puts narrower_uri narrower_repo.query(:subject=>::RDF::URI.new(narrower_uri)).each_statement do |narrower_statement| if narrower_statement.predicate.to_s == Mei::Loc.qskos('prefLabel') narrower_label ||= narrower_statement.object.value if narrower_statement.object.literal? end if narrower_statement.predicate.to_s == Mei::Loc.qskos('member') valid = true if narrower_statement.object.to_s == 'http://id.loc.gov/authorities/subjects/collection_LCSHAuthorizedHeadings' end end narrower_label ||= narrower_uri narrower_list << {:uri_link=>narrower_uri, :label=>narrower_label} if valid end end repo.query(:subject=>::RDF::URI.new(subject), :predicate=>Mei::Loc.qskos('altLabel')).each_statement do |result_statement| variant_list << result_statement.object.value if result_statement.object.literal? end return broader_list, narrower_list, variant_list end |
#loc_response_to_qa(data, counter) ⇒ Object
Simple conversion from LoC-based struct to QA hash
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mei/loc.rb', line 82 def loc_response_to_qa(data, counter) json_link = data.links.select { |link| link.first == 'application/json' } if json_link.present? json_link = json_link[0][1] broader, narrower, variants = get_skos_concepts(json_link.gsub('.json','')) end #count = ActiveFedora::Base.find_with_conditions("subject_tesim:#{data.id.gsub('info:lc', 'http://id.loc.gov').gsub(':','\:')}", rows: '100', fl: 'id' ).length #FIXME count = ActiveFedora::Base.search_with_conditions("#{@solr_field}:#{solr_clean(data.id.gsub('info:lc', 'http://id.loc.gov'))}", rows: '100', fl: 'id' ).length #count = 0 if count >= 99 count = "99+" else count = count.to_s end { "uri_link" => data.id.gsub('info:lc', 'http://id.loc.gov') || data.title, "label" => data.title, "broader" => broader, "narrower" => narrower, "variants" => variants, "count" => count } end |
#parse_authority_response ⇒ Object
Reformats the data received from the LOC service
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mei/loc.rb', line 63 def threaded_responses = [] #end_response = Array.new(20) end_response = [] position_counter = 0 @raw_response.select {|response| response[0] == "atom:entry"}.map do |response| threaded_responses << Thread.new(position_counter) { |local_pos| end_response[local_pos] = loc_response_to_qa(response_to_struct(response), position_counter) } position_counter+=1 #sleep(0.05) #loc_response_to_qa(response_to_struct(response)) end threaded_responses.each { |thr| thr.join } end_response end |
#response_to_struct(response) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/mei/loc.rb', line 117 def response_to_struct response result = response.each_with_object({}) do |result_parts, result| next unless result_parts[0] key = result_parts[0].sub('atom:', '').sub('dcterms:', '') info = result_parts[1] val = result_parts[2] case key when 'title', 'id', 'name', 'updated', 'created' result[key] = val when 'link' result["links"] ||= [] result["links"] << [info["type"], info["href"]] end end OpenStruct.new(result) end |
#search(q) ⇒ Object
50 51 52 53 |
# File 'lib/mei/loc.rb', line 50 def search q @raw_response = get_json(build_query_url(q)) end |
#solr_clean(term) ⇒ Object
113 114 115 |
# File 'lib/mei/loc.rb', line 113 def solr_clean(term) return term.gsub('\\', '\\\\').gsub(':', '\\:').gsub(' ', '\ ') end |