Skip to content

Commit

Permalink
pass all specs parsing JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
dstnbrkr authored and spohlenz committed May 6, 2010
1 parent 036244f commit f19f31b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
17 changes: 11 additions & 6 deletions lib/itunes_link_maker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,26 @@ def self.default_options
end

def self.search(query, options={})
html = get_html(query, default_options.merge(options))
parse_html(html).uniq
json = get_json(query, default_options.merge(options))
parse_json(json).uniq
end

def self.quick_search(query, options={})
search(query, options).first
end

private
def self.parse_html(json)
parser = JSON.parse(json)
parser.collect { |e| Result.new(e["itemName"], e["mediaType"], e["itemLinkUrl"]) }
def self.parse_json(json)
results = []
unless json.empty?
parser = JSON.parse(json)
# FIXME: don't need type argument
results = parser["results"].collect { |e| Result.new(e["itemName"], :name, e["itemLinkUrl"]) }
end
results
end

def self.get_html(query, options={})
def self.get_json(query, options={})
open(url_for(options.merge('term' => query))).read
end

Expand Down
2 changes: 1 addition & 1 deletion lib/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(name, type, display_url)
end

def url
@url ||= get_url
@display_url
end

def eql?(result)
Expand Down
2 changes: 1 addition & 1 deletion spec/itunes_link_maker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def quick_search
end

it "should return the result object" do
expected = ItunesLinkMaker::Result.new('Such Great Heights', :name, '/WebObjects/MZStoreServices.woa/wa/itmsSearchDisplayUrl?desc=The+Postal+Service+-+Give+Up+-+Such+Great+Heights&WOURLEncoding=ISO8859_1&lang=1&url=http%3A%2F%2Fphobos.apple.com%2FWebObjects%2FMZStore.woa%2Fwa%2FviewAlbum%3Fi%3D2522315%26id%3D2522333%26s%3D143441')
expected = ItunesLinkMaker::Result.new('Such Great Heights', :name, 'http://itunes.apple.com/us/album/such-great-heights/id2522333?i=2522315&uo=4')
quick_search.should == expected
end
end
2 changes: 1 addition & 1 deletion spec/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def create_result(options={})
options = {
:name => 'Such Great Heights',
:type => :name,
:display_url => '/WebObjects/MZStoreServices.woa/wa/itmsSearchDisplayUrl?desc=The+Postal+Service+-+Give+Up+-+Such+Great+Heights&WOURLEncoding=ISO8859_1&lang=1&url=http%3A%2F%2Fphobos.apple.com%2FWebObjects%2FMZStore.woa%2Fwa%2FviewAlbum%3Fi%3D2522315%26id%3D2522333%26s%3D143441'
:display_url => 'http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=2522315&id=2522333&s=143441'
}.merge(options)

ItunesLinkMaker::Result.new(options[:name], options[:type], options[:display_url])
Expand Down

0 comments on commit f19f31b

Please sign in to comment.