Skip to content
This repository has been archived by the owner on Apr 27, 2022. It is now read-only.

Commit

Permalink
Merge branch 'release/v0.3.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
rwd committed Jan 13, 2016
2 parents 39a6fc9 + ecc135d commit a168829
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 57 deletions.
2 changes: 1 addition & 1 deletion europeana-blacklight.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.0.0'

spec.add_dependency 'activesupport', '>= 4.0', '< 5.0'
spec.add_dependency 'blacklight', '>= 5.12.0', '< 5.16.0'
spec.add_dependency 'blacklight', '>= 5.12.0', '< 6.0'
spec.add_dependency 'europeana-api', '~> 0.4.1'
spec.add_dependency 'iso-639', '~> 0.2.5'
spec.add_dependency 'kaminari', '~> 0.16'
Expand Down
45 changes: 2 additions & 43 deletions lib/europeana/blacklight/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,27 @@ module Blacklight
##
# A Europeana document
class Document
autoload :LangMaps, 'europeana/blacklight/document/lang_maps'
autoload :MoreLikeThis, 'europeana/blacklight/document/more_like_this'
autoload :Relations, 'europeana/blacklight/document/relations'

include ActiveModel::Conversion
include ::Blacklight::Document
include ::Blacklight::Document::ActiveModelShim
include LangMaps
include MoreLikeThis
include Relations

attr_writer :provider_id, :record_id

class << self
# @todo Are three-letter language codes valid in EDM?
# @todo Empty key acceptance is a workaround for malformed API data
# output; remove when fixed at source
def lang_map?(obj)
return false unless obj.is_a?(Hash)
obj.keys.map(&:to_s).all? { |key| known_lang_map_key?(key) }
end

def known_lang_map_key?(key)
key = key.dup.downcase
['def', '', 'sh'].include?(key) || (!ISO_639.find(key.split('-').first).nil?)
end

def localize_lang_map(lang_map)
if lang_map.is_a?(Array)
return lang_map.map { |l| localize_lang_map(l) }
end

return lang_map unless lang_map?(lang_map)

lang_map_value(lang_map, I18n.locale.to_s) ||
lang_map_value(lang_map, I18n.default_locale.to_s) ||
lang_map[:def] ||
lang_map.values
end

def lang_map_value(lang_map, locale)
iso_locale = ISO_639.find(locale)
return nil unless lang_map.key?(iso_locale.alpha2) || lang_map.key?(iso_locale.alpha3)

alpha2 = lang_map[iso_locale.alpha2]
alpha3 = lang_map[iso_locale.alpha3]
if alpha2 && alpha3
[alpha2, alpha3].flatten
elsif alpha2
alpha2
else
alpha3
end
end

def model_name
@_model_name ||= begin
ActiveModel::Name.new(self, nil, 'Document')
end
end
end

delegate :lang_map?, :localize_lang_map, to: :class

def initialize(source_doc = {}, response = nil)
fields, @relations = extract_relations(source_doc)
super(fields, response)
Expand Down
65 changes: 65 additions & 0 deletions lib/europeana/blacklight/document/lang_maps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module Europeana
module Blacklight
class Document
##
# Methods for working with "LangMap" data types in API JSON responses
# @see http://labs.europeana.eu/api/getting-started#datatypes
module LangMaps
extend ActiveSupport::Concern

# @see https://www.loc.gov/standards/iso639-2/php/code_changes.php
DEPRECATED_ISO_LANG_CODES = %w(in iw jaw ji jw mo mol scc scr sh)

# Special keys API may return in a LangMap, not ISO codes
# @todo Empty key acceptance is a workaround for malformed API data
# output; remove when fixed at source
NON_ISO_LANG_CODES = ['def', '']

class_methods do
# @todo Are three-letter language codes valid in EDM?
def lang_map?(obj)
return false unless obj.is_a?(Hash)
obj.keys.map(&:to_s).all? { |key| known_lang_map_key?(key) }
end

def known_lang_map_key?(key)
key = key.dup.downcase
DEPRECATED_ISO_LANG_CODES.include?(key) ||
NON_ISO_LANG_CODES.include?(key) ||
(!ISO_639.find(key.split('-').first).nil?)
end

def localize_lang_map(lang_map)
if lang_map.is_a?(Array)
return lang_map.map { |l| localize_lang_map(l) }
end

return lang_map unless lang_map?(lang_map)

lang_map_value(lang_map, I18n.locale.to_s) ||
lang_map_value(lang_map, I18n.default_locale.to_s) ||
lang_map[:def] ||
lang_map.values
end

def lang_map_value(lang_map, locale)
iso_locale = ISO_639.find(locale)
return nil unless lang_map.key?(iso_locale.alpha2) || lang_map.key?(iso_locale.alpha3)

alpha2 = lang_map[iso_locale.alpha2]
alpha3 = lang_map[iso_locale.alpha3]
if alpha2 && alpha3
[alpha2, alpha3].flatten
elsif alpha2
alpha2
else
alpha3
end
end
end

delegate :lang_map?, :localize_lang_map, to: :class
end
end
end
end
8 changes: 6 additions & 2 deletions lib/europeana/blacklight/document/more_like_this.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def more_like_this_logic

def more_like_this_field_terms(*fields)
fields.flatten.map do |field|
fetch(field, []).compact
fetch(field, []).compact[0..9]
end.flatten
end

Expand All @@ -42,7 +42,11 @@ def more_like_this_field_queries(param = nil)

def more_like_this_param_query(param, terms, boost)
return nil unless terms.present?
or_terms = terms.map do |v|

string_terms = terms.select { |t| t.is_a?(String) }
return nil unless string_terms.present?

or_terms = string_terms.map do |v|
'"' + Europeana::API::Search.escape(v) + '"'
end.join(' OR ')
"#{param}: (#{or_terms})^#{boost}"
Expand Down
24 changes: 17 additions & 7 deletions lib/europeana/blacklight/search_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,16 @@ def add_facet_qf_to_api(api_parameters)
salient_facets.each_pair do |facet_field, value_list|
Array(value_list).reject(&:blank?).each do |value|
api_parameters[:qf] ||= []
if Europeana::API::Search::Fields::MEDIA.include?(facet_field)
api_parameters[:qf] << "#{facet_field}:#{value}"
else
api_parameters[:qf] << "#{facet_field}:\"#{value}\""
end
api_parameters[:qf] << "#{facet_field}:" + quote_facet_value(facet_field, value)
end
end
end

def quote_facet_value(facet_field, value)
return value if Europeana::API::Search::Fields::MEDIA.include?(facet_field)
'"' + value.gsub('"', '\"') + '"'
end

##
# Filter results by a query facet
def add_query_facet_to_api(_api_parameters)
Expand Down Expand Up @@ -173,8 +174,17 @@ def add_sorting_to_api(_api_parameters)
##
# Europeana API start param counts from 1
def start(start = nil)
super_start = super
super_start == self ? super_start : super_start + 1
if start
params_will_change!
@start = start.to_i
self
else
@start ||= (page - 1) * (rows || 10) + 1

val = @start || 1
val = 1 if @start < 1
val
end
end

protected
Expand Down
8 changes: 5 additions & 3 deletions lib/europeana/blacklight/search_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ module Blacklight
##
# Local overrides for {Blacklight::SearchHelper}
module SearchHelper
# index arg counts from 0; API start param counts from 1
def previous_and_next_document_params(index, window = 1)
start = index + 1
api_params = {}

if index > 1
api_params[:start] = index - window # get one before
if start > 1
api_params[:start] = start - window # get one before
api_params[:rows] = 2 * window + 1 # and one after
else
api_params[:start] = 1 # there is no previous doc
api_params[:start] = start # there is no previous doc
api_params[:rows] = 2 * window # but there should be one after
end

Expand Down
2 changes: 1 addition & 1 deletion lib/europeana/blacklight/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Europeana
module Blacklight
VERSION = '0.3.2'
VERSION = '0.3.3'
end
end

0 comments on commit a168829

Please sign in to comment.