Class: MTV::Music::Base

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

Direct Known Subclasses

Artist, Video

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mtv-music/base.rb', line 72

def initialize(xml)
  raise ArgumentError unless xml.kind_of?(Hpricot)
  
  if xml.at(:uri).inner_text == "http://api.mtvnservices.com/1/"
    raise MTVNetworkServiceError, xml.at(:title).inner_text, xml.at(:content).inner_text
  end
  
  @uri = xml.at(:id).inner_text
  @id  = @uri.split('/').last
  
  self.class.attributes.each do |attribute, options|
    element = xml.search(options[:matcher]).first
    next if element.nil?
    value = options[:attribute] ? element.attributes[options[:attribute]] : element.inner_text# rescue nil
    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
      end
    ensure
      self.instance_variable_set("@#{attribute}", value)
    end     
  end        
  
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



70
71
72
# File 'lib/mtv-music/base.rb', line 70

def id
  @id
end

#uriObject

Returns the value of attribute uri.



70
71
72
# File 'lib/mtv-music/base.rb', line 70

def uri
  @uri
end

Class Method Details

.api_path(method, id = nil) ⇒ Object



32
33
34
35
# File 'lib/mtv-music/base.rb', line 32

def api_path(method, id = nil)
  parameters = [self.name, id, method].compact
  return parameters.collect!{|param| CGI::escape(param.to_s).downcase}.join('/')
end

.attribute(*args) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/mtv-music/base.rb', line 9

def attribute(*args)
  @attributes   ||= {}
  
  options = args.extract_options!
  name, type = args
  class_eval %(attr_accessor :#{name})
  @attributes[name] = options.update({:type => type})
end

.attributesObject



18
19
20
# File 'lib/mtv-music/base.rb', line 18

def attributes
  @attributes || {}
end

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



28
29
30
# File 'lib/mtv-music/base.rb', line 28

def fetch_and_parse(resource, options = {})
  return Hpricot::XML(connection.get(resource, options))
end

.name_with_demodulizationObject



22
23
24
# File 'lib/mtv-music/base.rb', line 22

def name_with_demodulization
  self.name_without_demodulization.demodulize        
end

.search(term, options = {}) ⇒ Object

feed-format Specifies the format of the feed to be returned.

value:    atom, mrss           
example:  ?feed-format=mrss

max-results Limit the maximum number of results. The default value is 25. The maximum value is 100

value:    25, 1-100   
example:  ?max-results=25

start-index Choose the first result to display in the returns. The first return is 1.

When used with the max-results parameter, multiple pages of results can be created.  
value:    1, %d       
example:  ?start-index=26

date Limit the returns to date range specified.

value:    MMDDYYYY-MMDDYYYY 
example:  ?date=01011980-12311989

sort Specifies the sort order for returns.

value:    relevance, date_ascending, date_descending 
example:  ?sort=date_ascending


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mtv-music/base.rb', line 55

def search(term, options = {})
  options = options.update({:term => term})
  case date = options[:date]
  when Range
    options[:date] = "%s-%s" % [date.first, date.last].collect{|d| d.strftime("%m%d%Y")}
  when Time
    options[:date] = "%s-%s" % [date, Time.now].collect{|d| d.strftime("%m%d%Y")}
  end
  
  xml = fetch_and_parse(api_path(:search), options)
  results = xml.search(:entry).collect{|elem| self.new(fetch_and_parse((elem/'id').inner_text))}
  return results.delete_if{|e| e.uri.blank?}
end

Instance Method Details

#initialize_with_polymorphism(arg) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/mtv-music/base.rb', line 101

def initialize_with_polymorphism(arg)
  case arg
  when String
    if arg =~ /http:\/\//
      initialize_without_polymorphism(query_by_uri(arg))
    else
      initialize_without_polymorphism(query_by_string(arg))
    end
  when Hpricot
    initialize_without_polymorphism(arg)
  end
end