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:



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

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

  @cql = Cql.new(params[:query])
  @version = params.has_key?(:version) ? params[:version] : '1.2'
  @start = params.has_key?(:startRecord) ? params[:startRecord].to_i : 1
  @maximum = params.has_key?(:maximumRecords) ? params[:maximumRecords].to_i : 200
  @maximum = 1000 if 1000 < @maximum
  @packing = params.has_key?(:recordPacking) ? params[:recordPacking] : 'string'
  @schema = params.has_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.



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

def ascending
  @ascending
end

#cqlObject (readonly)

Returns the value of attribute cql.



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

def cql
  @cql
end

#extra_response_dataObject (readonly)

Returns the value of attribute extra_response_data.



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

def extra_response_data
  @extra_response_data
end

#manifestationsObject (readonly)

Returns the value of attribute manifestations.



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

def manifestations
  @manifestations
end

#maximumObject (readonly)

Returns the value of attribute maximum.



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

def maximum
  @maximum
end

#next_record_positionObject (readonly)

Returns the value of attribute next_record_position.



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

def next_record_position
  @next_record_position
end

#number_of_recordsObject (readonly)

Returns the value of attribute number_of_records.



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

def number_of_records
  @number_of_records
end

#packingObject (readonly)

Returns the value of attribute packing.



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

def packing
  @packing
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#schemaObject (readonly)

Returns the value of attribute schema.



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

def schema
  @schema
end

#startObject (readonly)

Returns the value of attribute start.



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

def start
  @start
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#get_extra_response_dataObject



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

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

#get_number_of_recordsObject



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

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

#searchObject



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

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



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

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