Browse files

Working in symbol arguments to facet search / return fields

  • Loading branch information...
1 parent cd2c661 commit c5f677e00a456daee520492b6c8a5220bcf2684d Jacob Harris committed Feb 18, 2009
Showing with 23 additions and 3 deletions.
  1. +5 −1 lib/nytimes_articles/article.rb
  2. +9 −2 lib/nytimes_articles/facet.rb
  3. +9 −0 test/nytimes/articles/test_facet.rb
View
6 lib/nytimes_articles/article.rb
@@ -246,7 +246,7 @@ def self.parse_reply(reply)
def self.add_facets_param(out_params, in_params)
if in_params[:facets]
- out_params['facets'] = in_params[:facets].to_a.join(',')
+ out_params['facets'] = in_params[:facets].to_a.map {|f| Facet.symbol_to_api_name(f)}.join(',')
end
end
@@ -300,6 +300,10 @@ def self.facet_argument(name, value, exclude = false)
value = [value]
end
+ if name.is_a? Symbol
+ name = Facet.symbol_to_api_name(name)
+ end
+
"#{'-' if exclude}#{name}:[#{value.join(',')}]"
end
View
11 lib/nytimes_articles/facet.rb
@@ -66,8 +66,15 @@ def initialize(facet_type, term, count)
##
# Takes a symbol name and subs it to a string constant
def self.symbol_to_api_name(facet)
- unless facet.is_a? Symbol
- raise ArgumentError, "Only accepts symbol arguments"
+ case facet
+ when String
+ return facet
+ when Facet
+ return facet.facet_type
+ when Symbol
+ # fall through
+ else
+ raise ArgumentError, "Unsupported type to Facet#symbol_to_api_name"
end
case facet
View
9 test/nytimes/articles/test_facet.rb
@@ -89,6 +89,15 @@ class TestNytimes::TestArticles::TestFacet < Test::Unit::TestCase
assert_equal Facet::PAGE, Facet.symbol_to_api_name(:pages)
end
+ should "return the string passed in if passed a string" do
+ assert_equal "FOOBAR", Facet.symbol_to_api_name('FOOBAR')
+ end
+
+ should "return the facet's facet_type if passed a Facet object" do
+ f = Facet.new(Facet::ORGANIZATION, 'THE NEW YORK TIMES', nil)
+ assert_equal Facet::ORGANIZATION, Facet.symbol_to_api_name(f)
+ end
+
should "raise an ArgumentError if not passed a symbol" do
assert_raise(ArgumentError) { Facet.symbol_to_api_name(23) }
end

0 comments on commit c5f677e

Please sign in to comment.