Class: Sru

Inherits:
Object
  • Object
show all
Defined in:
lib/enju_biblio/sru.rb

Constant Summary collapse

ASC_KEYS =
%w(title creator)
DESC_KEYS =
%w(created_at updated_at date_of_publication)
SORT_KEYS =
ASC_KEYS + DESC_KEYS
MULTI_KEY_MAP =
{'title' => 'sort_title'}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Sru

Returns a new instance of Sru.

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/enju_biblio/sru.rb', line 10

def initialize(params)
  raise QueryArgumentError, 'sru :query is required item.' unless params.key?(:query)

  @cql = Cql.new(params[:query])
  @version = params.key?(:version) ? params[:version] : '1.2'
  @start = params.key?(:startRecord) ? params[:startRecord].to_i : 1
  @maximum = params.key?(:maximumRecords) ? params[:maximumRecords].to_i : 200
  @maximum = 1000 if 1000 < @maximum
  @packing = params.key?(:recordPacking) ? params[:recordPacking] : 'string'
  @schema = params.key?(:recordSchema) ? params[:recordSchema] : 'dc'
  @sort_key = params[:sortKeys]

  @manifestations = []
  @extra_response_data = {}
end

Instance Attribute Details

#ascendingObject (readonly)

Returns the value of attribute ascending.



26
27
28
# File 'lib/enju_biblio/sru.rb', line 26

def ascending
  @ascending
end

#cqlObject (readonly)

Returns the value of attribute cql.



26
27
28
# File 'lib/enju_biblio/sru.rb', line 26

def cql
  @cql
end

#extra_response_dataObject (readonly)

Returns the value of attribute extra_response_data.



27
28
29
# File 'lib/enju_biblio/sru.rb', line 27

def extra_response_data
  @extra_response_data
end

#manifestationsObject (readonly)

Returns the value of attribute manifestations.



27
28
29
# File 'lib/enju_biblio/sru.rb', line 27

def manifestations
  @manifestations
end

#maximumObject (readonly)

Returns the value of attribute maximum.



26
27
28
# File 'lib/enju_biblio/sru.rb', line 26

def maximum
  @maximum
end

#next_record_positionObject (readonly)

Returns the value of attribute next_record_position.



27
28
29
# File 'lib/enju_biblio/sru.rb', line 27

def next_record_position
  @next_record_position
end

#number_of_recordsObject (readonly)

Returns the value of attribute number_of_records.



27
28
29
# File 'lib/enju_biblio/sru.rb', line 27

def number_of_records
  @number_of_records
end

#packingObject (readonly)

Returns the value of attribute packing.



26
27
28
# File 'lib/enju_biblio/sru.rb', line 26

def packing
  @packing
end

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/enju_biblio/sru.rb', line 26

def path
  @path
end

#schemaObject (readonly)

Returns the value of attribute schema.



26
27
28
# File 'lib/enju_biblio/sru.rb', line 26

def schema
  @schema
end

#startObject (readonly)

Returns the value of attribute start.



26
27
28
# File 'lib/enju_biblio/sru.rb', line 26

def start
  @start
end

#versionObject (readonly)

Returns the value of attribute version.



26
27
28
# File 'lib/enju_biblio/sru.rb', line 26

def version
  @version
end

Instance Method Details

#get_extra_response_dataObject



68
69
70
71
72
73
# File 'lib/enju_biblio/sru.rb', line 68

def get_extra_response_data
  # TODO: NDL で必要な項目が決定し、更に enju にそのフィールドが設けられた後で正式な実装を行なう。
  if @search.respond_to?(:erd)
    @schema == 'dc' ? @search.erd : {}
  end
end

#get_number_of_recordsObject



75
76
77
78
# File 'lib/enju_biblio/sru.rb', line 75

def get_number_of_records
  # TODO: sunspot での取得方法が分かり次第、正式な実装を行なう。
  @schema == 'dc' ? [1405, 1406] : [40,11]
end

#searchObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/enju_biblio/sru.rb', line 54

def search
  sunspot_query = @cql.to_sunspot
  search = Sunspot.new_search(Manifestation)
  search.build do
    fulltext sunspot_query
    paginate page: 1, per_page: 10000
  end
  @manifestations = search.execute!.results
  @extra_response_data = get_extra_response_data
  @number_of_records, @next_record_position = get_number_of_records

  @manifestations
end

#sort_byObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/enju_biblio/sru.rb', line 29

def sort_by
  sort = {sort_by: 'created_at', order: 'desc'}
  unless '1.1' == @version
    @path, @ascending = @cql.sort_by.split('/')
  else
    @path, @ascending = @sort_key.split(',') if @sort_key
  end
  # TODO ソート基準が入手しやすさの場合の処理
  if SORT_KEYS.include?(@path)
    if MULTI_KEY_MAP.keys.include?(@path)
      sort[:sort_by] = MULTI_KEY_MAP[@path]
    else
      sort[:sort_by] = @path
    end
    sort[:order] = 'asc' if ASC_KEYS.include?(@path)
    case @ascending
    when /\A(1|ascending)\Z/
      sort[:order] = 'asc'
    when /\A(0|descending)\Z/
      sort[:order] = 'desc'
    end
  end
  sort
end