Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #39 from ckdake/master
Adds basic exception handling for some invalid data
  • Loading branch information
youpy committed Nov 29, 2012
2 parents aacf0c1 + 0b30ec6 commit d0aa7ce
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/lastfm/response.rb
Expand Up @@ -7,6 +7,8 @@ class Response

def initialize(body)
@xml = XmlSimple.xml_in(body, 'ForceArray' => ['image', 'tag', 'user', 'event', 'correction'])
rescue REXML::ParseException
@xml = XmlSimple.xml_in(body.encode(Encoding.find("ISO-8859-1"), :undef => :replace), 'ForceArray' => ['image', 'tag', 'user', 'event', 'correction'])
end

def success?
Expand Down
18 changes: 18 additions & 0 deletions spec/fixtures/user_get_recent_tracks_malformed.xml
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<lfm status="ok">
<recenttracks user="RJ" page="1" perPage="10" totalPages="3019">
<track>
<artist mbid="">￾䬀愀瀀 匀氀愀瀀</artist>
<name>￾匀琀爀漀戀攀 䰀椀最栀琀猀 ㈀ 䤀戀椀稀愀 ⠀䐀攀愀搀洀愀甀㔀 瘀猀 䔀氀氀椀攀 䜀漀甀氀搀椀渀最 瘀猀 匀眀攀搀椀猀栀 䠀漀甀猀攀 䴀愀昀椀愀 瘀猀 䘀攀爀最椀攀 瘀猀 䰀椀氀 䨀漀渀⤀</name>
<streamable>0</streamable>
<mbid></mbid>
<album mbid="">￾䔀愀爀洀椀氀欀⸀挀漀洀 ⴀ ⌀䴀愀猀栀甀瀀䴀漀渀搀愀礀</album>
<url>http://www.last.fm/music/%EF%BF%BE%E4%AC%80%E6%84%80%E7%80%80%E2%80%80%E5%8C%80%E6%B0%80%E6%84%80%E7%80%80/_/%EF%BF%BE%E5%8C%80%E7%90%80%E7%88%80%E6%BC%80%E6%88%80%E6%94%80%E2%80%80%E4%B0%80%E6%A4%80%E6%9C%80%E6%A0%80%E7%90%80%E7%8C%80%E2%80%80%E3%88%80%E2%80%80%E4%A4%80%E6%88%80%E6%A4%80%E7%A8%80%E6%84%80%E2%80%80%E2%A0%80%E4%90%80%E6%94%80%E6%84%80%E6%90%80%E6%B4%80%E6%84%80%E7%94%80%E3%94%80%E2%80%80%E7%98%80%E7%8C%80%E2%80%80%E4%94%80%E6%B0%80%E6%B0%80%E6%A4%80%E6%94%80%E2%80%80%E4%9C%80%E6%BC%80%E7%94%80%E6%B0%80%E6%90%80%E6%A4%80%E6%B8%80%E6%9C%80%E2%80%80%E7%98%80%E7%8C%80%E2%80%80%E5%8C%80%E7%9C%80%E6%94%80%E6%90%80%E6%A4%80%E7%8C%80%E6%A0%80%E2%80%80%E4%A0%80%E6%BC%80%E7%94%80%E7%8C%80%E6%94%80%E2%80%80%E4%B4%80%E6%84%80%E6%98%80%E6%A4%80%E6%84%80%E2%80%80%E7%98%80%E7%8C%80%E2%80%80%E4%98%80%E6%94%80%E7%88%80%E6%9C%80%E6%A4%80%E6%94%80%E2%80%80%E7%98%80%E7%8C%80%E2%80%80%E4%B0%80%E6%A4%80%E6%B0%80%E2%80%80%E4%A8%80%E6%BC%80%E6%B8%80%E2%A4%80</url>
<image size="small"></image>
<image size="medium"></image>
<image size="large"></image>
<image size="extralarge"></image>
<date uts="1338664693">2 Jun 2012, 19:18</date>
</track>
</recenttracks>
</lfm>
11 changes: 11 additions & 0 deletions spec/method_specs/user_spec.rb
Expand Up @@ -210,6 +210,17 @@
tracks[1]['artist']['content'].should == 'Kylie Minogue'
tracks.size.should == 2
end

it 'should not error when a user\'s recent tracks includes malformed data' do
@lastfm.should_receive(:request).with('user.getRecentTracks', {
:user => 'test',
:page => nil,
:limit => nil,
:to => nil,
:from => nil
}).and_return(make_response('user_get_recent_tracks_malformed'))
tracks = @lastfm.user.get_recent_tracks(:user => 'test')
end
end

describe '#get_top_tags' do
Expand Down

0 comments on commit d0aa7ce

Please sign in to comment.