Class: Yahoo::Music::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/yahoo-music/base.rb

Direct Known Subclasses

Artist, Category, Image, Release, Review, Track, Video

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/yahoo-music/base.rb', line 79

def initialize(xml)
  raise ArgumentError unless xml.kind_of?(Hpricot)
          
  self.class.attributes.each do |attribute, options|
    value = xml.attributes[options[:matcher] || attribute.to_s]
    begin
      if options[:type] == Integer
        value = value.to_i
      elsif options[:type] == Float
        value = value.to_f
      elsif options[:type] == Date
        value = Date.parse(value) rescue nil
      elsif options[:type] == Boolean
        value = !! value.to_i.nonzero?
      elsif self.class.associations.include?(attribute)
        klass = options[:type]
        value = xml.search(klass.name).collect{|elem| klass.new(elem)}
        value = nil if value.empty?
      end
    ensure
      self.instance_variable_set("@#{attribute}", value)
    end     
  end        
end

Class Attribute Details

.associationsObject

Returns the value of attribute associations.



5
6
7
# File 'lib/yahoo-music/base.rb', line 5

def associations
  @associations
end

.attributesObject

Returns the value of attribute attributes.



5
6
7
# File 'lib/yahoo-music/base.rb', line 5

def attributes
  @attributes
end

Class Method Details

.api_path(service, resource, method, *args) ⇒ Object



63
64
65
66
67
# File 'lib/yahoo-music/base.rb', line 63

def api_path(service, resource, method, *args)
  response_type = method.nil? ? :item : :list
  parameters = [service, API_VERSION, response_type, resource, method, *args].compact
  return parameters.collect!{|param| CGI::escape(param.to_s).downcase}.join('/')
end

.attribute(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/yahoo-music/base.rb', line 8

def attribute(*args)
  @attributes   ||= {}
  @associations ||= []
  
  options = args.extract_options!
  name, type = args
  class_eval %(attr_accessor :#{name})
  @attributes[name] = options.update({:type => type})
  
  if Yahoo::Music::Base.subclasses.include?(type.inspect)
    @associations << name
    
    # Define plural and singular association methods
    define_method("#{name}".pluralize.to_sym) do
      value = instance_variable_get("@#{name}") || query_association_by_id(name, self.id)
      instance_variable_set("@#{name}", value)
      return value
    end
    
    define_method("#{name}".singularize.to_sym) do
      value = instance_variable_get("@#{name}") || query_association_by_id(name, self.id)
      value = value.first
      instance_variable_set("@#{name}", value)
      return value
    end
  end
  
  if options[:type] == Boolean
    define_method("#{name}?".to_sym) do
      value = instance_variable_get("@#{name}")
      return value
    end
  end
end

.fetch_and_parse(resource, options = {}) ⇒ Object



57
58
59
60
61
# File 'lib/yahoo-music/base.rb', line 57

def fetch_and_parse(resource, options = {})      
  raise YahooWebServiceError, "No App ID specified" if connection.nil?    
  options = options.update({'response' => self.associations.join(',')}) if self.associations.any?
  return Hpricot::XML(connection.get(resource, options))
end

.name_with_demodulizationObject



51
52
53
# File 'lib/yahoo-music/base.rb', line 51

def name_with_demodulization
  self.name_without_demodulization.demodulize        
end

.search(*args) ⇒ Object

Search by a parameter for a specific service Ex. Artist.search(term) options



72
73
74
75
76
# File 'lib/yahoo-music/base.rb', line 72

def search(*args)
  options = args.extract_options!
  xml = fetch_and_parse(api_path(self.name, nil, :search, options[:search_mode] || :all, args.join(',')), options)
  return xml.search(self.name).collect{|elem| self.new(elem)}
end

Instance Method Details

#initialize_with_polymorphism(arg) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/yahoo-music/base.rb', line 104

def initialize_with_polymorphism(arg)
  case arg
  when String
    initialize_without_polymorphism(query_by_string(arg))
  when Integer
    initialize_without_polymorphism(query_by_id(arg))
  when Hpricot
    initialize_without_polymorphism(arg)
  end
end