Skip to content

Commit

Permalink
Changed the Hpricot search method
Browse files Browse the repository at this point in the history
- If multiple values, returns them as a comma-separated string
- Returns nil if no value found
  • Loading branch information
David Eisinger committed Jul 8, 2008
1 parent 54a75ce commit e0ff378
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/amazon_product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
class AmazonProduct < ActiveRecord::Base
# belongs_to :amazonable, :polymorphic => true

def hdoc(key)
def hdoc(key, separator = ', ')
@doc ||= Hpricot.XML(xml)
@doc.at(key).inner_html
values = (@doc/key).collect {|e| e.inner_html } * separator
values unless values.blank?
end

def get(key)
Expand Down
7 changes: 6 additions & 1 deletion test/acts_as_amazon_product_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def test_find_with_multiple
assert_equal("Mike Clark", @book_ror.amazon.author)
end

def test_returns_nil_if_attribute_not_found
assert_equal(nil, @book_ror.amazon.get('contributor'))
end

def test_update
assert_not_nil(@book_perl.amazon)
isbn = @book_perl.amazon.isbn
Expand All @@ -115,6 +119,7 @@ def test_update

def test_product_with_all_defaults
assert_not_nil(@movie_dh.amazon)
assert_equal 'Bruce Willis', @movie_dh.amazon.get('itemattributes/actor')
assert_equal 'Bruce Willis, Timothy Olyphant, Justin Long, Maggie Q, Cliff Curtis',
@movie_dh.amazon.get('itemattributes/actor')
end
end

0 comments on commit e0ff378

Please sign in to comment.