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.



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

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::RequestError => e
  end
end

Instance Attribute Details

#facetsObject (readonly)

Returns the value of attribute facets.



13
14
15
# File 'lib/ur/search.rb', line 13

def facets
  @facets
end

#productsObject (readonly)

Returns the value of attribute products.



13
14
15
# File 'lib/ur/search.rb', line 13

def products
  @products
end

#solrObject (readonly)

Returns the value of attribute solr.



13
14
15
# File 'lib/ur/search.rb', line 13

def solr
  @solr
end

Class Method Details

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



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

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)

  programs = Yajl::HttpStream.get(url)
  
  if (programs['response']['docs'].count > 0) 
    Product.find(programs['response']['docs'].map { |d| d['id'] })
  else
    []
  end
end

Instance Method Details

#current_pageObject

Pagination



74
75
76
# File 'lib/ur/search.rb', line 74

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

#next_pageObject



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

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

#num_foundObject



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

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

#ok?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/ur/search.rb', line 36

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

#per_pageObject



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

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

#previous_pageObject



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

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

#total_pagesObject



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

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