kivanio (owner)

Fork Of

Revisions

gist: 227909 Download_button fork
public
Public Clone URL: git://gist.github.com/227909.git
Embed All Files: show embed
resource_count.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Rack::Middleware that returns the number of models in the database for any call to a URL like modelname/count.
#
# Examples:
# /users/count
# => returns User.count in plain text
#
# /articles/1/comments/count.xml
# => returns Comment.count as XML, e.g.
# <comments type="integer">5</comments>
# Note: this does not return Articles.find(1).comments.count like it maybe should!
#
# Usage:
# Simply require in environment.rb with: config.middleware.use "ResourceCount"
#
# Requirements when not using Rails:
# 1.) Use Rails conventions (controller name is pluralized name of model)
# 2.) Model.count should be available, but size or length also works
# 3.) These String methods from Rails need to be available: singularize, classify and constantize
class ResourceCount
 
  def initialize(app)
    @app = app
  end
 
  def call(env)
    dup.count(env)
  end
 
  def count(env)
    if env['PATH_INFO'] =~ %r{/(.+)/count(\.xml)?$}
      model_name = $1.singularize.classify.constantize
      count = model_name.count rescue model_name.size rescue model_name.length
    
      if $2 == '.xml'
        @body = "<#{$1} type=\"integer\">#{count}</#{$1}>"
        @content_type = 'text/xml'
      else
        @body = count.to_s
        @content_type = 'text/plain'
      end
      [200, {'Content-Type' => @content_type, 'Content-Length' => @body.length.to_s}, @body]
    
    else
      @app.call(env)
    end
  end
end