Class: Sasquatch::SearchResult

Inherits:
Array
  • Object
show all
Defined in:
lib/sasquatch/search_result.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSearchResult

Returns a new instance of SearchResult.



5
6
7
# File 'lib/sasquatch/search_result.rb', line 5

def initialize
  @results = []
end

Instance Attribute Details

#graphObject

Returns the value of attribute graph.



3
4
5
# File 'lib/sasquatch/search_result.rb', line 3

def graph
  @graph
end

#max_resultsObject (readonly)

Returns the value of attribute max_results.



3
4
5
# File 'lib/sasquatch/search_result.rb', line 3

def max_results
  @max_results
end

#search_contextObject

Returns the value of attribute search_context.



3
4
5
# File 'lib/sasquatch/search_result.rb', line 3

def search_context
  @search_context
end

#start_indexObject (readonly)

Returns the value of attribute start_index.



3
4
5
# File 'lib/sasquatch/search_result.rb', line 3

def start_index
  @start_index
end

#total_resultsObject (readonly)

Returns the value of attribute total_results.



3
4
5
# File 'lib/sasquatch/search_result.rb', line 3

def total_results
  @total_results
end

Class Method Details

.new_from_query(graph, options, store) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/sasquatch/search_result.rb', line 9

def self.new_from_query(graph, options, store)
  search_result = self.new
  search_result.graph = graph
  search_result.search_context={:store=>store, :options=>options}
  search_result.parse_graph
  search_result
end

Instance Method Details

#nextObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sasquatch/search_result.rb', line 75

def next
  if (@start_index + self.count) >= @total_results
    nil
  else
    o = {}
    o[:offset] = @start_index + @max_results
    o[:sort] = @search_context[:options][:sort]
    o[:max] = @max_results
    @search_context[:store].search(@search_context[:options][:query][:query],o)
  end
end

#parse_graphObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sasquatch/search_result.rb', line 25

def parse_graph
  return unless @graph
  @graph.query(:predicate=>RDF::URI.intern("http://a9.com/-/spec/opensearch/1.1/totalResults")).each do |stmt|
    @total_results = stmt.object.value.to_i
  end
  @total_results ||= 0
  @graph.query(:predicate=>RDF::URI.intern("http://a9.com/-/spec/opensearch/1.1/startIndex")).each do |stmt|
    @start_index = stmt.object.value.to_i
  end
  @start_index ||= 0 
  @graph.query(:predicate=>RDF::URI.intern("http://a9.com/-/spec/opensearch/1.1/itemsPerPage")).each do |stmt|
    @max_results = stmt.object.value.to_i
  end
  @max_results ||= 0    
  
  set_results       
end

#previousObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sasquatch/search_result.rb', line 63

def previous
  if @start_index == 0
    nil
  else
    o = {}
    o[:offset] = @start_index - @max_results
    o[:offset] = 0 if o[:offset] < 0
    o[:sort] = @search_context[:options][:sort]
    o[:max] = @max_results
    @search_context[:store].search(@search_context[:options][:query][:query],o)
  end
end

#set_resultsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sasquatch/search_result.rb', line 43

def set_results
  channel_uri = nil
  @graph.query(:predicate=>RDF.type, :object=>RDF::RSS.channel).each do |channel|
    channel_uri = channel.subject
  end
  return unless channel_uri
  seq_uri = nil
  @graph.query(:subject=>channel_uri, :predicate=>RDF::RSS.items).each do |items|
    seq_uri = items.object
  end
  return unless seq_uri
  @graph.query(:subject=>seq_uri).each do |li|
    if li.predicate.to_s =~ /http:\/\/www\.w3\.org\/1999\/02\/22-rdf-syntax-ns#_(\d)+$/
      (ns,idx) = li.predicate.to_s.split("#_")
      idx = (idx.to_i - 1)
      self[idx] = li.object
    end
  end
end