about_page 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- about_page (0.0.2)
4
+ about_page (0.0.3)
5
5
  rails (~> 3.2)
6
6
 
7
7
  GEM
@@ -11,7 +11,7 @@ module AboutPage
11
11
  @configuration.set_headers!(response)
12
12
 
13
13
  respond_to do |format|
14
- format.html { render :status => @configuration.ok? ? 200 : 417 } # about_page.html.erb
14
+ format.html { render :status => @configuration.valid? ? 200 : 417 } # about_page.html.erb
15
15
  format.json { render :json => @configuration.to_json }
16
16
  format.xml { render :xml => @configuration.to_xml }
17
17
  end
@@ -1,9 +1,7 @@
1
1
  <h1><%= key.to_s.humanize %></h1>
2
- <% if profile.respond_to? :messages %>
3
2
  <% profile.messages.each do |msg| %>
4
3
  <div><%= msg %></div>
5
- <% end %>
6
- <% end %>
4
+ <% end if profile.respond_to? :messages %>
7
5
  <ul>
8
6
  <% profile.each_pair do |k,v| %>
9
7
  <li><%= [k,v].join(': ') %></li>
@@ -1,6 +1,6 @@
1
1
  <div class="about_page">
2
2
  <% @configuration.each do |key, profile| %>
3
- <div class="pane pane-<%= key.to_s.parameterize %> <%= 'not-ok' if profile.respond_to? :ok? and !profile.ok? %>">
3
+ <div class="pane pane-<%= key.to_s.parameterize %> <%= 'not-ok' if profile.respond_to? :valid? and !profile.valid? %>">
4
4
  <%= render_about_pane(key, profile) %>
5
5
  </div>
6
6
  <% end %>
@@ -50,8 +50,8 @@ module AboutPage
50
50
  self.nodes.each { |key, profile| profile.preflight(request) }
51
51
  end
52
52
 
53
- def ok?
54
- self.nodes.select { |key, profile| profile.respond_to? :ok? }.all? { |key, profile| profile.ok? }
53
+ def valid?
54
+ self.nodes.select { |key, profile| profile.respond_to? :ok? }.all? { |key, profile| profile.valid? }
55
55
  end
56
56
 
57
57
  def nodes
@@ -63,8 +63,20 @@ module AboutPage
63
63
  end
64
64
 
65
65
  class Node
66
+ include ActiveModel::Validations
67
+
66
68
  def preflight request
69
+ errors.clear
70
+ @request_expectations = request.params.select { |k,v| k =~ /^#{namespace}\./ }
71
+ end
72
+
73
+ def expects key
74
+ @request_expectations["#{namespace}.#{key}"] || self.options[:expects][key] if @request_expectations
75
+ end
67
76
 
77
+ def messages
78
+ run_validations!
79
+ errors.to_a.uniq
68
80
  end
69
81
 
70
82
  def set_headers! response
@@ -73,16 +85,13 @@ module AboutPage
73
85
 
74
86
  def add_header response, text
75
87
  response.headers['X-AboutPage-Warning'] ||= ""
76
- response.headers['X-AboutPage-Warning'] += "#{text};"
88
+ response.headers['X-AboutPage-Warning'] += "#{self.class.name}: #{text};"
77
89
 
78
90
  end
79
91
 
80
- def ok?
81
- true
82
- end
83
-
84
- def messages
85
- []
92
+ protected
93
+ def namespace
94
+ self.class.name.split("::").last.downcase
86
95
  end
87
96
  end
88
97
  end
@@ -2,30 +2,31 @@ module AboutPage
2
2
  class Fedora < AboutPage::Configuration::Node
3
3
  delegate :each_pair, :to_json, :to_xml, :to => :to_h
4
4
 
5
+ validates_each :profile do |record, attr, value|
6
+ unless value.present?
7
+ record.errors.add attr, ": unable to connect to Fedora: #{record.rubydora.inspect}"
8
+ end
9
+ end
10
+
5
11
  attr_accessor :rubydora
6
12
 
7
13
  def initialize rubydora_instance
8
14
  self.rubydora = rubydora_instance
9
15
  end
10
16
 
11
- def to_h
17
+ def profile
12
18
  rubydora.profile || {}
13
19
  end
14
20
 
15
- def ok?
16
- !to_h.empty?
21
+ def to_h
22
+ profile
17
23
  end
18
24
 
19
- def messages
20
- a = []
21
- a << "Unable to connect to fedora: #{self.rubydora.inspect}" if rubydora.profile.nil?
22
-
23
- a
24
- end
25
25
 
26
26
  def preflight request
27
27
  # FIXME: ew.
28
28
  self.rubydora.instance_variable_set('@profile', nil)
29
+ super(request)
29
30
  end
30
31
 
31
32
  end
@@ -4,11 +4,21 @@ module AboutPage
4
4
 
5
5
  attr_accessor :rsolr, :options
6
6
 
7
+ validates_each :schema do |record, attr, value|
8
+ unless value.present?
9
+ record.errors.add attr, ": unable to connect to Solr: #{record.rsolr.inspect}"
10
+ end
11
+ end
12
+ validates :numDocs, :numericality => { :greater_than_or_equal_to => Proc.new { |c| c.expects(:numDocs) } }
13
+
14
+
7
15
  def initialize rsolr_instance, options = {}
8
16
  self.rsolr = rsolr_instance
9
17
  self.options = options
18
+ self.options[:expects] ||= {}
19
+ self.options[:expects][:numDocs] ||= 1
10
20
 
11
- self.options[:minimum_numdocs] ||= 1
21
+ @request_expectations = {}
12
22
  end
13
23
 
14
24
  def schema
@@ -17,33 +27,38 @@ module AboutPage
17
27
  {}
18
28
  end
19
29
 
20
- def index
21
- schema['index'] || {}
22
- end
23
- alias_method :to_h, :index
30
+ def registry
31
+ @registry ||= begin
32
+ h = {}
33
+ resp = rsolr.get 'admin/registry.jsp', :params => { :wt => 'xml' }
34
+ doc = Nokogiri::XML resp
24
35
 
25
- def ok?
26
- return false if schema.empty?
27
- return false if index[:numDocs].to_i < minimum_numdocs
36
+ doc.xpath('/solr/*').each do |node|
37
+ next if node.name == "solr-info"
38
+ h[node.name] = node.text
39
+ end
28
40
 
29
- true
41
+ h
42
+ end
43
+ rescue
44
+ {}
30
45
  end
31
46
 
32
- def messages
33
- a = []
34
- a << "Unable to connect to solr: #{self.rsolr.inspect}" if schema.empty?
35
- a << "Solr numDocs (#{index[:numDocs]}) is less than the minimum #{minimum_numdocs}" if !schema.empty? and index[:numDocs].to_i < minimum_numdocs
47
+ def index
48
+ (schema || {})['index'] || {}
49
+ end
36
50
 
37
- a
51
+ def to_h
52
+ index.merge(registry)
38
53
  end
39
54
 
55
+ def numDocs; index[:numDocs]; end
56
+
40
57
  def preflight request
41
58
  @schema = nil
42
- @minimum_numdocs = request.params['solr.numDocs'].to_i if request.params['solr.numDocs'].to_i
43
- end
59
+ @registry = nil
44
60
 
45
- def minimum_numdocs
46
- @minimum_numdocs || self.options[:minimum_numdocs]
61
+ super
47
62
  end
48
63
  end
49
64
  end
@@ -1,3 +1,3 @@
1
1
  module AboutPage
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -27,31 +27,30 @@ describe AboutPage::Solr do
27
27
  end
28
28
  end
29
29
 
30
-
31
- describe "#ok?" do
30
+ describe "#valid?" do
32
31
  it "should be ok if the number of documents in the index is greater than or equal to the :minimum_numdocs" do
33
- subject.stub(:index).and_return { { :numDocs => 1 }}
34
- subject.stub(:minimum_numdocs).and_return 1
35
- subject.should be_ok
32
+ subject.stub(:schema).and_return { { 'index' => { :numDocs => 1 } } }
33
+ subject.stub(:expects).and_return 1
34
+ subject.should be_valid
36
35
  end
37
36
 
38
37
  it "should be not be ok if the number of documents in the index is less than the :minimum_numdocs" do
39
- subject.stub(:index).and_return { { :numDocs => 1 }}
40
- subject.stub(:minimum_numdocs).and_return 5
41
- subject.should_not be_ok
38
+ subject.stub(:schema).and_return { { 'index' => { :numDocs => 1 } } }
39
+ subject.stub(:expects).and_return 5
40
+ subject.should_not be_valid
42
41
  end
43
42
 
44
43
  it "should not be ok if the index :numDocs param is not set" do
45
- subject.stub(:index).and_return { Hash.new }
46
- subject.stub(:minimum_numdocs).and_return 1
47
- subject.should_not be_ok
44
+ subject.stub(:schema).and_return { { 'index' => { } } }
45
+ subject.stub(:expects).and_return 1
46
+ subject.should_not be_valid
48
47
  end
49
48
  end
50
49
 
51
50
  describe "#set_headers!" do
52
51
  it "should add helpful headers when something is wrong" do
53
- subject.stub(:index).and_return { { :numDocs => 1 }}
54
- subject.stub(:minimum_numdocs).and_return 5
52
+ subject.stub(:schema).and_return { { 'index' => { :numDocs => 1 } } }
53
+ subject.stub(:expects).and_return 5
55
54
 
56
55
  subject.should_receive(:add_header)
57
56
  subject.set_headers! mock()
@@ -60,19 +59,19 @@ describe AboutPage::Solr do
60
59
 
61
60
  describe "#minimum_numdocs" do
62
61
  it "should default to 1" do
63
- subject.minimum_numdocs.should == 1
62
+ subject.expects(:numDocs).should == 1
64
63
  end
65
64
 
66
65
  it "should use parameters given in the configuration" do
67
- node = AboutPage::Solr.new(@mock_solr_connection, :minimum_numdocs => 5)
68
- node.minimum_numdocs.should == 5
66
+ node = AboutPage::Solr.new(@mock_solr_connection, :expects => { :numDocs => 5 })
67
+ node.expects(:numDocs).should == 5
69
68
  end
70
69
 
71
70
  it "should use the request parameters to set the minimum_numdocs" do
72
- node = AboutPage::Solr.new(@mock_solr_connection, :minimum_numdocs => 5)
71
+ node = AboutPage::Solr.new(@mock_solr_connection, :expects => { :numDocs => 5 })
73
72
  node.preflight(mock(:params => { 'solr.numDocs' => 1000 }))
74
73
 
75
- node.minimum_numdocs.should == 1000
74
+ node.expects(:numDocs).should == 1000
76
75
  end
77
76
  end
78
77
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: about_page
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-05-21 00:00:00.000000000 Z
13
+ date: 2012-05-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -187,7 +187,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
187
187
  version: '0'
188
188
  segments:
189
189
  - 0
190
- hash: 823128254637505744
190
+ hash: 2890899449821864305
191
191
  required_rubygems_version: !ruby/object:Gem::Requirement
192
192
  none: false
193
193
  requirements:
@@ -196,7 +196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
196
  version: '0'
197
197
  segments:
198
198
  - 0
199
- hash: 823128254637505744
199
+ hash: 2890899449821864305
200
200
  requirements: []
201
201
  rubyforge_project:
202
202
  rubygems_version: 1.8.24