Class: EagleSearch::Response
- Inherits:
-
Object
- Object
- EagleSearch::Response
- Defined in:
- lib/eagle_search/response.rb
Instance Method Summary collapse
- #current_page ⇒ Object
- #each ⇒ Object
- #hits ⇒ Object
-
#initialize(klass, response, options) ⇒ Response
constructor
A new instance of Response.
- #limit_value ⇒ Object
- #records ⇒ Object
- #total_hits ⇒ Object
- #total_pages ⇒ Object
Constructor Details
#initialize(klass, response, options) ⇒ Response
Returns a new instance of Response.
3 4 5 6 7 |
# File 'lib/eagle_search/response.rb', line 3 def initialize(klass, response, ) @klass = klass @response = response = end |
Instance Method Details
#current_page ⇒ Object
32 33 34 |
# File 'lib/eagle_search/response.rb', line 32 def current_page [:page] || 1 end |
#each ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/eagle_search/response.rb', line 16 def each if block_given? records.each { |e| yield(e) } else records.to_enum end end |
#hits ⇒ Object
28 29 30 |
# File 'lib/eagle_search/response.rb', line 28 def hits @response["hits"]["hits"] end |
#limit_value ⇒ Object
40 41 42 |
# File 'lib/eagle_search/response.rb', line 40 def limit_value [:per_page] || 25 end |
#records ⇒ Object
9 10 11 12 13 14 |
# File 'lib/eagle_search/response.rb', line 9 def records ids = hits.map { |hit| hit["_id"] } #avoids n+1 @klass.includes([:includes]) if [:includes] @klass.where(@klass.primary_key => ids) end |
#total_hits ⇒ Object
24 25 26 |
# File 'lib/eagle_search/response.rb', line 24 def total_hits @response["hits"]["total"] end |
#total_pages ⇒ Object
36 37 38 |
# File 'lib/eagle_search/response.rb', line 36 def total_pages (total_hits / limit_value.to_f).ceil end |