Skip to content

Commit

Permalink
Adding Custodian::Tag.count
Browse files Browse the repository at this point in the history
  • Loading branch information
infovore committed Mar 6, 2009
1 parent 34ea63f commit 5bdce2f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/custodian/tag_class.rb
@@ -1,6 +1,12 @@
module Custodian
class Tag
require 'hpricot'

def self.count(conditions)
conditions = normalise_conditions(conditions)
xml = request('/content/tags', conditions)
count_tags(xml)
end

def self.find(type, conditions={})
if type == :all
Expand All @@ -18,6 +24,10 @@ def self.find_tags_by_conditions(conditions={})
def self.extract_tags(xml)
Hpricot(xml).search('//tags/tag').collect{|r| Tag.new(r)}
end

def self.count_tags(xml)
Hpricot(xml).search("/tags").first.attributes["count"].to_i
end

private

Expand Down
11 changes: 11 additions & 0 deletions spec/tag_spec.rb
Expand Up @@ -19,6 +19,17 @@
end
end

describe "Counting results for a particular query" do
before(:each) do
@search = Custodian::Tag.extract_tags(File.open(File.dirname(__FILE__) + '/tags.xml'))
Custodian::Tag.stub!(:count_tags).and_return(Hpricot(File.open(File.dirname(__FILE__) + '/tags.xml')).search("/tags").first.attributes["count"].to_i)
end

it "should return the total number of available results for a query" do
Custodian::Tag.count_tags(:q => "uk").should == 13
end
end

describe "A tag" do
before(:each) do
@tag = Custodian::Tag.new(Hpricot(File.open(File.dirname(__FILE__) + '/tag.xml')))
Expand Down

0 comments on commit 5bdce2f

Please sign in to comment.