Class: LastFM::Struct

Inherits:
Struct
  • Object
show all
Defined in:
lib/lastfm/struct.rb

Overview

Prodives modifications to Ruby’s Struct class for use within the LastFM module space. Must be called ‘Struct’ to play nice with YARD’s @attr documentation.

Direct Known Subclasses

Album, Artist, Buylink, Event, Shout, Tag, Track, Venue, Wiki

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h = {}) ⇒ Struct

Override Struct’s initialize method to accept a hash of members instead.



8
9
10
# File 'lib/lastfm/struct.rb', line 8

def initialize(h={})
  members.each{|m| self[m] = h[m.to_sym]}
end

Class Method Details

.from_xml(xml, initial_attributes = {}) ⇒ LastFM::Struct

Construct a LastFM::Struct object from XML, using each inheritor’s attr_from_node method to determine how to manipulate individual attributes.

Parameters:

  • xml (LibXML::XML::Document)

    XML obtained from a Last.fm API call

  • initial_attributes (Hash) (defaults to: {})

    Attributes to set before parsing the XML

Returns:

  • (LastFM::Struct)

    object contructed from attributes contained in XML

Raises:

  • (NotImplementedError)


25
26
27
28
29
30
31
# File 'lib/lastfm/struct.rb', line 25

def self.from_xml(xml, initial_attributes={})
  raise NotImplementedError unless self.method_defined?(:update_from_node)
  xml = xml.find_first(self.package) if xml.is_a?(LibXML::XML::Document)
  model = self.new(initial_attributes)
  xml.find('*').each{|child| model.update_from_node(child)}
  model
end

.packageString

Get the Last.fm package name for a Ruby class

Returns:

  • (String)

    the Last.fm package name



15
16
17
# File 'lib/lastfm/struct.rb', line 15

def self.package
  self.to_s.downcase.split('::').last
end