Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

pass all specs parsing JSON

  • Loading branch information...
commit f19f31b5ea913c8438849a457729e2e6407eecf6 1 parent 036244f
@dstnbrkr dstnbrkr authored committed
View
17 lib/itunes_link_maker.rb
@@ -32,8 +32,8 @@ 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={})
@@ -41,12 +41,17 @@ def self.quick_search(query, options={})
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
View
2  lib/result.rb
@@ -10,7 +10,7 @@ def initialize(name, type, display_url)
end
def url
- @url ||= get_url
+ @display_url
end
def eql?(result)
View
2  spec/itunes_link_maker_spec.rb
@@ -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
View
2  spec/result_spec.rb
@@ -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])
Please sign in to comment.
Something went wrong with that request. Please try again.