Class: MathMetadata::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/math_metadata_lookup/result.rb

Overview

Example of Result class

# initialize search engine
l = MathMetadata::Lookup.new(:verbose => false)

# result containing articles (l.article, l.heuristic, l.reference)
result = l.article(:title => 'sobolev')
result.each do |site|
  next unless site[:result]
  site[:result].each do |article|
    puts "#{site[:name]}: #{article[:title]} (#{article[:authors].join('; ')})"
  end
end

# result containing authors (l.author)
result = l.author(:name => 'Vesely, Jiri')
result.each do |site|
  next unless site[:result]
  site[:result].each do |author|
    puts "#{site[:name]}: #{author[:preferred]} (#{author[:forms].join('; ')})"
  end
end

Constant Summary collapse

FORMATS =
[:ruby, :yaml, :json, :xml, :html, :text]

Instance Method Summary collapse

Constructor Details

#initialize(meta = []) ⇒ Result

Returns a new instance of Result.



37
38
39
# File 'lib/math_metadata_lookup/result.rb', line 37

def initialize( meta=[] )
  @metadata = meta
end

Instance Method Details

#<<(val) ⇒ Object



42
43
44
# File 'lib/math_metadata_lookup/result.rb', line 42

def <<(val)
  @metadata << val
end

#eachObject



47
48
49
50
51
# File 'lib/math_metadata_lookup/result.rb', line 47

def each
  @metadata.each do |site|
    yield site
  end
end

#format(f = :ruby) ⇒ Object



69
70
71
# File 'lib/math_metadata_lookup/result.rb', line 69

def format( f=:ruby )
  self.send "to_#{f}"
end

#num_resultsObject



59
60
61
# File 'lib/math_metadata_lookup/result.rb', line 59

def num_results
  @metadata.inject(0){|s,m| s += m[:result].size}
end

#resultsObject



64
65
66
# File 'lib/math_metadata_lookup/result.rb', line 64

def results
  @metadata.inject([]){|s,m| s << m[:result]}.flatten
end

#sizeObject



54
55
56
# File 'lib/math_metadata_lookup/result.rb', line 54

def size
  @metadata.size
end

#to_arrayObject



122
123
124
# File 'lib/math_metadata_lookup/result.rb', line 122

def to_array
  @metadata
end

#to_htmlObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/math_metadata_lookup/result.rb', line 74

def to_html
  result = ""
  @metadata.each do |site|
    next unless site[:result]
    result << %~
<div class="site">
<h3>Site: #{site[:name]}</h3>~
    site[:result].to_a.each do |entity|
      result << entity.to_html
    end
    result << %~</div>~
  end
  result
end

#to_jsonObject



117
118
119
# File 'lib/math_metadata_lookup/result.rb', line 117

def to_json
  @metadata.to_json
end

#to_textObject Also known as: to_str



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/math_metadata_lookup/result.rb', line 127

def to_text
  result = ""
  @metadata.each do |site|
    next unless site[:result]
    result << "Site: #{site[:name]}\n"
    result << "URL: #{site[:url]}\n"
    result << "\n"
    site[:result].each do |entity|
      result << entity.to_text
    end
    result << "\n"
  end
  result
end

#to_xmlObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/math_metadata_lookup/result.rb', line 90

def to_xml
  result = ""

  result << %~<?xml version="1.0" encoding="utf-8"?>
<mml>~
  @metadata.each do |site|
    next unless site[:result]
    result << %~
<site name="#{site[:name]}">~
    site[:result].each do |entity|
      result << entity.to_xml
    end
    result << %~
</site>
~
  end
  result << %~</mml>~

  result
end

#to_yamlObject



112
113
114
# File 'lib/math_metadata_lookup/result.rb', line 112

def to_yaml
  @metadata.ya2yaml
end