Class: UR::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/ur/search.rb

Overview

Search for products and populate from the metadata cache

Constant Summary collapse

SEARCH_SERVICE_URL =
'http://services.ur.se/search'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(solr_params) ⇒ Search

Returns a new instance of Search.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ur/search.rb', line 18

def initialize(solr_params)
  # Prepare the facets
  @products = []
  @facets = {}
  @solr   = nil

  begin
    solr = RSolr.connect :url => SEARCH_SERVICE_URL
    response = solr.find solr_params

    # Populate the products from the Metadata Cache
    @products = (response.ok? && response.docs.size > 0) ?
      Product.find(response.docs.map { |d| d.id }) : []

    # Expose the Solr response
    @solr = response
    @solr.facets.map { |f| @facets[f.name] = f.items } if @solr.facets.size > 0
  rescue RSolr::Error => e
  end
end

Instance Attribute Details

#facetsObject (readonly)

Returns the value of attribute facets.



16
17
18
# File 'lib/ur/search.rb', line 16

def facets
  @facets
end

#productsObject (readonly)

Returns the value of attribute products.



16
17
18
# File 'lib/ur/search.rb', line 16

def products
  @products
end

#solrObject (readonly)

Returns the value of attribute solr.



16
17
18
# File 'lib/ur/search.rb', line 16

def solr
  @solr
end

Class Method Details

.current_programs(per_page = 10, offset = 0, format = false, agerange = false) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ur/search.rb', line 51

def self.current_programs(per_page = 10, offset = 0, format = false, agerange = false)
  url = "#{SEARCH_SERVICE_URL}/select?qt=current-products" +
        "&rows=#{per_page}&start=#{offset}" +
        "&publicstreaming=NOW" +
        "&fq=(search_product_type:programtv" +
        "%20OR%20search_product_type:programradio)"

  if ['audio','video'].include? format
    url += "%20AND%20format:#{format}"
  end

  url += agerange_filter(agerange)

  response = RestClient.get(url)
  json = Yajl::Parser.parse(response)

  if (json['response']['docs'].count > 0)
    Product.find(json['response']['docs'].map { |d| d['id'] })
  else
    []
  end
end

Instance Method Details

#current_pageObject

Pagination



78
79
80
# File 'lib/ur/search.rb', line 78

def current_page
  (!@solr.docs.nil?) ? @solr.docs.current_page : 0
end

#next_pageObject



90
91
92
# File 'lib/ur/search.rb', line 90

def next_page
  (current_page < total_pages) ? @solr.docs.next_page : false
end

#num_foundObject



47
48
49
# File 'lib/ur/search.rb', line 47

def num_found
  (ok?) ? @solr.response['numFound'].to_i : 0
end

#ok?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/ur/search.rb', line 39

def ok?
  (!@solr.nil? && @solr && @solr.ok?)
end

#per_pageObject



43
44
45
# File 'lib/ur/search.rb', line 43

def per_page
  (ok?) ? @solr.params['rows'].to_i : 0
end

#previous_pageObject



86
87
88
# File 'lib/ur/search.rb', line 86

def previous_page
  (current_page > 1) ? @solr.docs.previous_page : false
end

#total_pagesObject



82
83
84
# File 'lib/ur/search.rb', line 82

def total_pages
  (!@solr.docs.nil?) ? @solr.docs.total_pages : 0
end