Sha256: 7baae0206d5bd7adc53ea5babaa45775dbabed8b1dc1bd4e30eca7366f114b1a

Contents?: true

Size: 1020 Bytes

Versions: 2

Compression:

Stored size: 1020 Bytes

Contents

require 'rubygems'
require 'xmlsimple'

class Lastfm
  class Response
    attr_reader :xml

    def initialize(body)
      # workaround for https://github.com/youpy/ruby-lastfm/issues/83
      body = fix_body(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?
      @xml['status'] == 'ok'
    end

    def message
      @xml['error']['content']
    end

    def error
      @xml['error']['code'].to_i
    end

    private

    def fix_body(body)
      namespace_attr = 'xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"'

      body.sub(/(<results[^>]*)/) do |str|
        if str.match(namespace_attr)
          str
        else
          '%s %s' % [str, namespace_attr]
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lastfm-1.27.4 lib/lastfm/response.rb
lastfm-1.27.3 lib/lastfm/response.rb