Class: Cinii::SearchResult::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cinii/search_result/base.rb

Direct Known Subclasses

Article, Book, Dissertation

Instance Method Summary collapse

Constructor Details

#initialize(faraday_response) ⇒ Base

Returns a new instance of Base.

Raises:



8
9
10
11
12
# File 'lib/cinii/search_result/base.rb', line 8

def initialize(faraday_response)
  @raw_body = faraday_response.body

  raise APIResponseError, 'レスポンスに@graph属性が含まれていません' unless body['@graph']
end

Instance Method Details

#current_pageObject

現在のページ数



54
55
56
# File 'lib/cinii/search_result/base.rb', line 54

def current_page
  (start_index.to_f / items_per_page.to_f).ceil
end

#dateObject

検索日時



34
35
36
# File 'lib/cinii/search_result/base.rb', line 34

def date
  @date ||= DateTime.parse(graph['dc:date'])
end

#descriptionObject

タイトルと同じ



24
25
26
# File 'lib/cinii/search_result/base.rb', line 24

def description
  @description ||= graph['description']
end

#first_page?Boolean

最初のページ判定

Returns:

  • (Boolean)


64
65
66
# File 'lib/cinii/search_result/base.rb', line 64

def first_page?
  start_index.between?(1, items_per_page)
end

#itemsObject



14
15
16
# File 'lib/cinii/search_result/base.rb', line 14

def items
  raise 'This method must be overridden.'
end

#items_per_pageObject

検索結果件数



49
50
51
# File 'lib/cinii/search_result/base.rb', line 49

def items_per_page
  @items_per_page ||= graph['opensearch:itemsPerPage'].to_i
end

#last_page?Boolean

最終ページ判定

Returns:

  • (Boolean)


69
70
71
# File 'lib/cinii/search_result/base.rb', line 69

def last_page?
  start_index.between?(last_page_start_index, total_items)
end

#last_page_start_indexObject

最終ページの開始番号



85
86
87
# File 'lib/cinii/search_result/base.rb', line 85

def last_page_start_index
  total_items - items_per_page
end

リクエストURI



29
30
31
# File 'lib/cinii/search_result/base.rb', line 29

def link
  @link ||= graph['link']['@id']
end

#next_page_start_indexObject

次ページの開始番号



74
75
76
# File 'lib/cinii/search_result/base.rb', line 74

def next_page_start_index
  start_index + items_per_page
end

#prev_page_start_indexObject

前ページの開始番号



79
80
81
82
# File 'lib/cinii/search_result/base.rb', line 79

def prev_page_start_index
  index = start_index - items_per_page
  index >= 1 ? index : 1
end

#start_indexObject

検索結果の開始番号



44
45
46
# File 'lib/cinii/search_result/base.rb', line 44

def start_index
  @start_index ||= graph['opensearch:startIndex'].to_i
end

#titleObject

タイトル



19
20
21
# File 'lib/cinii/search_result/base.rb', line 19

def title
  @title ||= graph['title']
end

#total_itemsObject

検索結果総数



39
40
41
# File 'lib/cinii/search_result/base.rb', line 39

def total_items
  @total_items ||= graph['opensearch:totalResults'].to_i
end

#total_pagesObject

全てのページ数



59
60
61
# File 'lib/cinii/search_result/base.rb', line 59

def total_pages
  (total_items.to_f / items_per_page.to_f).ceil
end