Class: Kasabi::Search::Facet::Results

Inherits:
Object
  • Object
show all
Defined in:
lib/kasabi/api/facet.rb

Overview

The results of a facetted search

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, fields, facets = Hash.new) ⇒ Results

Returns a new instance of Results.



37
38
39
40
41
# File 'lib/kasabi/api/facet.rb', line 37

def initialize(query, fields, facets=Hash.new)
  @query = query
  @fields = fields
  @facets = facets
end

Instance Attribute Details

#facetsObject (readonly)

An array of Term objects



35
36
37
# File 'lib/kasabi/api/facet.rb', line 35

def facets
  @facets
end

#fieldsObject (readonly)

The fields used to generate the results



32
33
34
# File 'lib/kasabi/api/facet.rb', line 32

def fields
  @fields
end

#queryObject (readonly)

The query used to generate the facet results, as echoed in the response



29
30
31
# File 'lib/kasabi/api/facet.rb', line 29

def query
  @query
end

Class Method Details

.parse(data) ⇒ Object

Parses the XML format from a successful API response to generate a Results object instance



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kasabi/api/facet.rb', line 45

def Results.parse(data)
  doc = REXML::Document.new(data)
  root = doc.root
  head = root.elements[1]
  
  query = ""
  fields = ""
  queryEl = head.get_elements("query")[0]
  if queryEl != nil
    query = queryEl.text
  end
  fieldsEl = head.get_elements("fields")[0]
  if fieldsEl != nil
    fields = fieldsEl.text
  end
  
  results = Results.new(query, fields)
  
  fields = root.get_elements("fields")[0]
  if fields == nil
    raise "No fields in document!"
  end
  
  fields.get_elements("field").each do |field|
    field_name = field.attribute("name").value
    results.facets[field_name] = Array.new
    
    field.get_elements("term").each do |term|
      term = Term.new(term.attribute("number").value.to_i, 
        term.attribute("search-uri").value, 
        term.text() )
        
      results.facets[field_name] << term
    end
    
  end
  
  return results          
end