Class: MTV::Music::Base
- Inherits:
-
Object
- Object
- MTV::Music::Base
- Defined in:
- lib/mtv-music/base.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#uri ⇒ Object
Returns the value of attribute uri.
Class Method Summary collapse
- .api_path(method, id = nil) ⇒ Object
- .attribute(*args) ⇒ Object
- .attributes ⇒ Object
- .fetch_and_parse(resource, options = {}) ⇒ Object
- .name_with_demodulization ⇒ Object
-
.search(term, options = {}) ⇒ Object
feed-format Specifies the format of the feed to be returned.
Instance Method Summary collapse
-
#initialize(xml) ⇒ Base
constructor
A new instance of Base.
- #initialize_with_polymorphism(arg) ⇒ Object
Constructor Details
#initialize(xml) ⇒ Base
Returns a new instance of Base.
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, | element = xml.search([:matcher]).first next if element.nil? value = [:attribute] ? element.attributes[[:attribute]] : element.inner_text# rescue nil begin if [:type] == Integer value = value.to_i elsif [:type] == Float value = value.to_f elsif [:type] == Date value = Date.parse(value) rescue nil end ensure self.instance_variable_set("@#{attribute}", value) end end end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
70 71 72 |
# File 'lib/mtv-music/base.rb', line 70 def id @id end |
#uri ⇒ Object
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 ||= {} = args. name, type = args class_eval %(attr_accessor :#{name}) @attributes[name] = .update({:type => type}) end |
.attributes ⇒ Object
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, = {}) return Hpricot::XML(connection.get(resource, )) end |
.name_with_demodulization ⇒ Object
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, = {}) = .update({:term => term}) case date = [:date] when Range [:date] = "%s-%s" % [date.first, date.last].collect{|d| d.strftime("%m%d%Y")} when Time [:date] = "%s-%s" % [date, Time.now].collect{|d| d.strftime("%m%d%Y")} end xml = fetch_and_parse(api_path(:search), ) 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 |