rockstar 0.6.3 → 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,19 +11,19 @@ module Rockstar
11
11
  @from = from
12
12
  @to = to
13
13
  end
14
-
14
+
15
15
  def from=(value)
16
16
  @from = value.to_i
17
17
  end
18
-
18
+
19
19
  def to=(value)
20
20
  @to = value.to_i
21
21
  end
22
-
22
+
23
23
  def from
24
24
  @from.to_i
25
25
  end
26
-
26
+
27
27
  def to
28
28
  @to.to_i
29
29
  end
@@ -1,14 +1,14 @@
1
1
  module Rockstar
2
2
  class Event < Base
3
-
3
+
4
4
  attr_accessor :eid, :title, :artists, :headliners, :start_date, :end_date,
5
- :description, :attendance, :reviews, :tag, :url, :website, :tickets,
5
+ :description, :attendance, :reviews, :tag, :url, :website, :tickets,
6
6
  :cancelled, :tags, :images, :venue
7
-
7
+
8
8
  class << self
9
9
  def new_from_xml(xml, doc)
10
10
  e = Event.new(
11
- (xml).at(:id).inner_html,
11
+ (xml).at(:id).inner_html,
12
12
  (xml).at(:title).inner_html
13
13
  )
14
14
 
@@ -52,20 +52,20 @@ module Rockstar
52
52
  e
53
53
  end
54
54
  end
55
-
55
+
56
56
  def initialize(id, title)
57
57
  raise ArgumentError, "ID is required" if id.blank?
58
58
  raise ArgumentError, "Title is required" if title.blank?
59
59
  @eid = id
60
60
  @title = title
61
61
  end
62
-
62
+
63
63
  def image(which=:medium)
64
64
  which = which.to_s
65
- raise ArgumentError unless ['small', 'medium', 'large', 'extralarge', 'mega'].include?(which)
65
+ raise ArgumentError unless ['small', 'medium', 'large', 'extralarge', 'mega'].include?(which)
66
66
  if (self.images.nil?)
67
67
  load_info
68
- end
68
+ end
69
69
  self.images[which]
70
70
  end
71
71
  end
data/lib/rockstar/geo.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  module Rockstar
2
2
  class Geo < Base
3
-
4
-
5
3
  # Get events in a specific location. Opts can be
6
4
  #
7
5
  # * :location => 'madrid' # A city name from geo.metros
@@ -9,9 +7,9 @@ module Rockstar
9
7
  #
10
8
  # Additionally you can set the distance from that point with
11
9
  # :distance => 50 # 50 km from the given location
12
- #
10
+ #
13
11
  def events(opts = {}, force = false)
14
- get_instance("geo.getEvents", :events, :event, opts, force)
12
+ get_instance("geo.getEvents", :events, :event, opts, force)
15
13
  end
16
14
 
17
15
  def metros(country, force = false)
@@ -1,24 +1,24 @@
1
1
  module Rockstar
2
2
  class Metro < Base
3
-
3
+
4
4
  attr_accessor :name, :country
5
5
 
6
6
  class << self
7
7
  def new_from_xml(xml, doc)
8
8
  Metro.new(
9
- (xml).at(:name).inner_html,
9
+ (xml).at(:name).inner_html,
10
10
  (xml).at(:country).inner_html
11
11
  )
12
12
  end
13
13
  end
14
-
14
+
15
15
  def initialize(name, country)
16
16
  raise ArgumentError, "Name is required" if name.blank?
17
17
  raise ArgumentError, "Country is required" if country.blank?
18
18
  @name = name
19
19
  @country = country
20
20
  end
21
-
21
+
22
22
  end
23
23
  end
24
24
 
data/lib/rockstar/rest.rb CHANGED
@@ -20,19 +20,19 @@ module Rockstar
20
20
 
21
21
  def request(resource, method = "get", args = {}, sign_request=false)
22
22
  url = URI.parse(@base_url)
23
-
23
+
24
24
  if (!resource.blank?)
25
25
  args[:method] = resource
26
26
  args[:api_key]= Rockstar.lastfm_api_key
27
27
  end
28
-
28
+
29
29
  if args
30
30
  sorted_keys = args.keys.sort_by{|k|k.to_s}
31
31
  query = sorted_keys.collect { |k| "%s=%s" % [escape(k.to_s), escape(args[k].to_s)] }.join("&")
32
32
 
33
33
  if !args[:sk].nil? ||sign_request # Session Key available => sign the request or sign_request = true?
34
34
  signed = sorted_keys.collect {|k| "%s%s" % [k.to_s, args[k].to_s]}.join()
35
-
35
+
36
36
  auth = Digest::MD5.hexdigest("#{signed}#{Rockstar.lastfm_api_secret}")
37
37
  query += "&api_sig=#{auth}"
38
38
  end
@@ -56,7 +56,7 @@ module Rockstar
56
56
  res = http.start() { |conn| conn.request(req) }
57
57
  res.body
58
58
  end
59
-
59
+
60
60
  private
61
61
  def escape(str)
62
62
  URI.escape(str, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
data/lib/rockstar/tag.rb CHANGED
@@ -1,17 +1,17 @@
1
1
  # Below is code samples for how to find the top albums and tracks for a tag.
2
- #
2
+ #
3
3
  # tag = Rockstar::Tag.new('country')
4
- #
4
+ #
5
5
  # puts 'Top Albums'
6
6
  # tag.top_albums.each { |a| puts "(#{a.count}) #{a.name} by #{a.artist}" }
7
- #
7
+ #
8
8
  # puts
9
- #
9
+ #
10
10
  # puts 'Top Tracks'
11
11
  # tag.top_tracks.each { |t| puts "(#{t.count}) #{t.name} by #{t.artist}" }
12
- #
12
+ #
13
13
  # Which would output something similar to:
14
- #
14
+ #
15
15
  # Top Albums
16
16
  # (29) American IV: The Man Comes Around by Johnny Cash
17
17
  # (14) Folks Pop In at the Waterhouse by Various Artists
@@ -33,7 +33,7 @@
33
33
  # (5) Breathe by Faith Hill
34
34
  # (5) Unearthed (disc 4: My Mother's Hymn Book) by Johnny Cash
35
35
  # (4) Home by Dixie Chicks
36
- #
36
+ #
37
37
  # Top Tracks
38
38
  # (221) Hurt by Johnny Cash
39
39
  # (152) I Walk the Line by Johnny Cash
@@ -58,7 +58,7 @@
58
58
  module Rockstar
59
59
  class Tag < Base
60
60
  attr_accessor :name, :count, :url
61
-
61
+
62
62
  class << self
63
63
  def new_from_xml(xml, doc=nil)
64
64
  name = (xml).at(:name).inner_html
@@ -67,7 +67,7 @@ module Rockstar
67
67
  t.url = Base.fix_url((xml).at(:url).inner_html)
68
68
  t
69
69
  end
70
-
70
+
71
71
  def top_tags
72
72
  doc = fetch_and_parse("tag.getTopTags")
73
73
  @top_tags = (doc/"toptags/tag").collect do |tag|
@@ -78,17 +78,17 @@ module Rockstar
78
78
  end
79
79
  end
80
80
  end
81
-
81
+
82
82
  def initialize(name)
83
83
  raise ArgumentError, "Name is required" if name.blank?
84
84
  @name = name
85
85
  end
86
-
87
-
86
+
87
+
88
88
  def top_artists(force=false)
89
89
  get_instance("tag.getTopArtists", :top_artists, :artist, {:tag => @name}, force)
90
90
  end
91
-
91
+
92
92
  def top_albums(force=false)
93
93
  get_instance("tag.getTopAlbums", :top_albums, :album, {:tag => @name}, force)
94
94
  end
@@ -1,17 +1,17 @@
1
1
  # Below is an example of how to get the top fans for a track.
2
- #
2
+ #
3
3
  # track = Rockstar::Track.new('Carrie Underwood', 'Before He Cheats')
4
4
  # puts 'Fans'
5
5
  # puts "=" * 4
6
6
  # track.fans.each { |u| puts u.username }
7
- #
7
+ #
8
8
  # Which would output something like:
9
- #
9
+ #
10
10
  # track = Rockstar::Track.new('Carrie Underwood', 'Before He Cheats')
11
11
  # puts 'Fans'
12
12
  # puts "=" * 4
13
13
  # track.fans.each { |u| puts "(#{u.weight}) #{u.username}" }
14
- #
14
+ #
15
15
  # Fans
16
16
  # ====
17
17
  # (69163) PimpinRose
@@ -43,13 +43,13 @@ module Rockstar
43
43
  class Track < Base
44
44
  attr_accessor :artist, :artist_mbid, :name, :mbid, :playcount, :rank, :url
45
45
  attr_accessor :summary, :content, :streamable, :album, :album_mbid, :date, :date_uts, :duration
46
-
46
+
47
47
  # only seems to be used on top tracks for tag
48
48
  attr_accessor :count, :thumbnail, :image, :images
49
-
49
+
50
50
  # for weekly top tracks
51
51
  attr_accessor :chartposition
52
-
52
+
53
53
  class << self
54
54
  def new_from_xml(xml, doc=nil)
55
55
  artist = (xml).at(:artist)['name'] if (xml).at(:artist) && !(xml).at(:artist)['name'].nil?
@@ -98,7 +98,7 @@ module Rockstar
98
98
  query["trackNumber[0]"] = params[:trackNumber] if !params[:trackNumber].blank?
99
99
  query["mbid[0]"] = params[:mbid] if !params[:mbid].blank?
100
100
  query["duration[0]"] = params[:duration] if !params[:duration].blank?
101
-
101
+
102
102
  doc = Hpricot::XML(Track.connection.post("track.scrobble", true, query))
103
103
 
104
104
  if doc.at("lfm")["status"] == "failed"
@@ -111,7 +111,7 @@ module Rockstar
111
111
  raise RequestFailedError, doc.at("lfm").at("error").inner_html
112
112
  end
113
113
  end
114
-
114
+
115
115
  doc.at("lfm")["status"]
116
116
  end
117
117
 
@@ -142,7 +142,7 @@ module Rockstar
142
142
  query["trackNumber"] = params[:trackNumber] if !params[:trackNumber].blank?
143
143
  query["mbid"] = params[:mbid] if !params[:mbid].blank?
144
144
  query["duration"] = params[:duration] if !params[:duration].blank?
145
-
145
+
146
146
  doc = Hpricot::XML(Track.connection.post("track.updateNowPlaying", true, query))
147
147
 
148
148
  if doc.at("lfm")["status"] == "failed"
@@ -155,12 +155,12 @@ module Rockstar
155
155
  raise RequestFailedError, doc.at("lfm").at("error").inner_html
156
156
  end
157
157
  end
158
-
158
+
159
159
  doc.at("lfm")["status"]
160
160
  end
161
-
161
+
162
162
  end
163
-
163
+
164
164
  def initialize(artist, name, o={})
165
165
  raise ArgumentError, "Artist is required" if artist.blank?
166
166
  raise ArgumentError, "Name is required" if name.blank?
@@ -188,7 +188,7 @@ module Rockstar
188
188
  self.streamable = (xml).at(:track)['streamable'] if (xml).at(:track) && (xml).at(:track)['streamable']
189
189
  self.streamable = (xml).at(:streamable).inner_html == '1' ? 'yes' : 'no' if streamable.nil? && (xml).at(:streamable)
190
190
  self.duration = (xml).at(:duration).inner_html.to_i if (xml).at(:duration)
191
-
191
+
192
192
  self.count = xml['count'] if xml['count']
193
193
  self.album = (xml).at(:album).inner_html if (xml).at(:album)
194
194
  self.album_mbid = (xml).at(:album)['mbid'] if (xml).at(:album) && (xml).at(:album)['mbid']
@@ -204,21 +204,21 @@ module Rockstar
204
204
  (xml/'image').each {|image|
205
205
  self.images[image['size']] = image.inner_html if self.images[image['size']].nil?
206
206
  }
207
-
207
+
208
208
  self.thumbnail = images['small']
209
209
  self.image = images['medium']
210
210
 
211
211
  self
212
212
  end
213
-
213
+
214
214
  def albums(force=false)
215
215
  get_instance("track.getInfo", :albums, :album, {:track => @name, :artist => @artist}, force)
216
216
  end
217
-
217
+
218
218
  def fans(force=false)
219
219
  get_instance("track.getTopFans", :fans, :user, {:track => @name, :artist => @artist}, force)
220
220
  end
221
-
221
+
222
222
  def tags(force=false)
223
223
  get_instance("track.getTopTags", :tags, :tag, {:track => @name, :artist => @artist}, force)
224
224
  end
@@ -226,7 +226,7 @@ module Rockstar
226
226
  def similar(limit = 10, force = false)
227
227
  get_instance('track.getSimilar', :similar, :track, {:track => @name, :artist => @artist, :limit => limit}, force)
228
228
  end
229
-
229
+
230
230
  # The session_key is returned by auth.session.key
231
231
  def love(session_key)
232
232
  Track.love(@artist, @name, session_key)
data/lib/rockstar/user.rb CHANGED
@@ -1,17 +1,17 @@
1
1
  # Probably the most common use of this lib would be to get your most recent tracks or your top tracks. Below are some code samples.
2
2
  # user = Rockstar::User.new('jnunemaker')
3
- #
3
+ #
4
4
  # puts "#{user.username}'s Recent Tracks"
5
5
  # puts "=" * (user.username.length + 16)
6
6
  # user.recent_tracks.each { |t| puts t.name }
7
- #
7
+ #
8
8
  # puts
9
9
  # puts
10
- #
10
+ #
11
11
  # puts "#{user.username}'s Top Tracks"
12
12
  # puts "=" * (user.username.length + 13)
13
13
  # user.top_tracks.each { |t| puts "(#{t.playcount}) #{t.name}" }
14
- #
14
+ #
15
15
  # Which would output something like:
16
16
  #
17
17
  # jnunemaker's Recent Tracks
@@ -22,8 +22,8 @@
22
22
  # Lord I Guess I'll Never Know
23
23
  # Country Song
24
24
  # Bitter Sweet Symphony (Radio Edit)
25
- #
26
- #
25
+ #
26
+ #
27
27
  # jnunemaker's Top Tracks
28
28
  # =======================
29
29
  # (62) Probably Wouldn't Be This Way
@@ -44,39 +44,39 @@
44
44
  # (35) You're Beautiful
45
45
  # (35) Walk Away
46
46
  # (34) Stickwitu
47
- module Rockstar
47
+ module Rockstar
48
48
  class User < Base
49
49
  # attributes needed to initialize
50
50
  attr_reader :username, :period
51
-
51
+
52
52
  # profile attributes
53
53
  attr_accessor :id, :cluster, :url, :realname, :mbox_sha1sum, :registered
54
54
  attr_accessor :registered_unixtime, :age, :gender, :country, :playcount, :avatar, :realname, :images
55
55
 
56
56
  # neighbor attributes
57
57
  attr_accessor :match
58
-
58
+
59
59
  # track fans attributes
60
60
  attr_accessor :weight
61
-
61
+
62
62
  class << self
63
63
  def new_from_xml(xml, doc=nil)
64
64
  u = User.new((xml).at(:name).inner_html)
65
65
  u.url = Base.fix_url((xml).at(:url).inner_html) if (xml).at(:url)
66
-
66
+
67
67
  u.images = {}
68
68
  (xml/'image').each {|image|
69
69
  u.images[image['size']] = image.inner_html if u.images[image['size']].nil?
70
70
  }
71
-
71
+
72
72
  u.avatar = u.images['small']
73
-
73
+
74
74
  u.weight = (xml).at(:weight).inner_html if (xml).at(:weight)
75
75
  u.match = (xml).at(:match).inner_html if (xml).at(:match)
76
76
  u.realname= (xml).at(:realname).inner_html if (xml).at(:realname)
77
77
  u
78
78
  end
79
-
79
+
80
80
  def find(*args)
81
81
  options = {:include_profile => false}
82
82
  options.merge!(args.pop) if args.last.is_a?(Hash)
@@ -84,7 +84,7 @@ module Rockstar
84
84
  users.length == 1 ? users.pop : users
85
85
  end
86
86
  end
87
-
87
+
88
88
  def initialize(username, o={})
89
89
  options = {:include_profile => false, :period => 'overall'}.merge(o)
90
90
  raise ArgumentError if username.blank?
@@ -92,19 +92,7 @@ module Rockstar
92
92
  @period = options[:period]
93
93
  load_profile() if options[:include_profile]
94
94
  end
95
-
96
- def current_events(format=:ics)
97
- warn "[DEPRECATION] `current_events` is deprecated. The current api doesn't offer ics/ical formatted data."
98
- end
99
-
100
- def friends_events(format=:ics)
101
- warn "[DEPRECATION] `friends_events` is deprecated. The current api doesn't offer ics/ical formatted data."
102
- end
103
-
104
- def recommended_events(format=:ics)
105
- warn "[DEPRECATION] `recommended_events` is deprecated. The current api doesn't offer ics/ical formatted data."
106
- end
107
-
95
+
108
96
  def load_profile
109
97
  doc = self.class.fetch_and_parse("user.getInfo", {:user => @username})
110
98
  @id = (doc).at(:id).inner_html
@@ -116,82 +104,72 @@ module Rockstar
116
104
  @gender = (doc).at(:gender).inner_html
117
105
  @country = (doc).at(:country).inner_html
118
106
  @playcount = (doc).at(:playcount).inner_html
119
-
107
+
120
108
  @images = {}
121
109
  (doc/'image').each {|image|
122
110
  @images[image['size']] = image.inner_html
123
111
  }
124
-
112
+
125
113
  @avatar = @images["small"]
126
114
  end
127
-
115
+
128
116
  def top_artists(force=false, options = {} )
129
117
  default_options = {
130
118
  :period => self.period
131
119
  }
132
120
  options = default_options.merge(options)
133
121
  options[:user] = @username
134
-
122
+
135
123
  get_instance("user.getTopArtists", :top_artists, :artist, options, force)
136
124
  end
137
-
125
+
138
126
  def top_albums(force=false)
139
127
  get_instance("user.getTopAlbums", :top_albums, :album, {:user => @username}, force)
140
128
  end
141
-
129
+
142
130
  def top_tracks(force=false)
143
131
  get_instance("user.getTopTracks", :top_tracks, :track, {:user => @username}, force)
144
132
  end
145
-
133
+
146
134
  def top_tags(force=false)
147
135
  get_instance("user.getTopTags", :top_tags, :tag, {:user => @username}, force)
148
136
  end
149
-
137
+
150
138
  def friends(force=false)
151
139
  get_instance("user.getFriends", :friends, :user, {:user => @username}, force)
152
140
  end
153
-
141
+
154
142
  def neighbours(force=false)
155
143
  get_instance("user.getNeighbours", :neighbours, :user, {:user => @username}, force)
156
144
  end
157
-
145
+
158
146
  def recent_tracks(force=false)
159
147
  get_instance("user.getRecentTracks", :recent_tracks, :track, {:user => @username}, force)
160
148
  end
161
-
162
- def recent_banned_tracks(force=false)
163
- warn "[DEPRECATION] `recent_banned_tracks` is deprecated. The current api doesn't offer this function"
164
- []
165
- end
166
-
149
+
167
150
  def recent_loved_tracks(force=false)
168
151
  get_instance("user.getLovedTracks", :recent_loved_tracks, :track, {:user => @username}, force)
169
152
  end
170
-
171
- def recommendations(force=false)
172
- warn "[DEPRECATION] `recommendations` is deprecated. Please use recommended_artists"
173
- []
174
- end
175
153
 
176
154
  # The session_key is returned by auth.session.key
177
155
  def recommended_artists(session_key, force=false)
178
156
  get_instance("user.getRecommendedArtists", :recommendations, :artist, {:user => @username, :sk => session_key}, force)
179
157
  end
180
-
158
+
181
159
  def charts(force=false)
182
160
  get_instance("user.getWeeklyChartList", :charts, :chart, {:user => @username}, force)
183
161
  end
184
-
162
+
185
163
  def weekly_artist_chart(from=nil, to=nil)
186
164
  doc = self.class.fetch_and_parse("user.getWeeklyArtistChart", {:user => @username, :from => from, :to => to})
187
165
  (doc/:artist).inject([]) { |elements, el| elements << Artist.new_from_xml(el); elements }
188
166
  end
189
-
167
+
190
168
  def weekly_album_chart(from=nil, to=nil)
191
169
  doc = self.class.fetch_and_parse("user.getWeeklyAlbumChart", {:user => @username, :from => from, :to => to})
192
170
  (doc/:album).inject([]) { |elements, el| elements << Album.new_from_xml(el); elements }
193
171
  end
194
-
172
+
195
173
  def weekly_track_chart(from=nil, to=nil)
196
174
  doc = self.class.fetch_and_parse("user.getWeeklyTrackChart", {:user => @username, :from => from, :to => to})
197
175
  (doc/:track).inject([]) { |elements, el| elements << Track.new_from_xml(el); elements }