Class: MySpace::MySpace
- Inherits:
-
Object
- Object
- MySpace::MySpace
- Defined in:
- lib/myspace/myspace.rb
Overview
The MySpace API object provides access to the MySpace REST API.
Constant Summary collapse
- APPLICATION_TYPE_ONSITE =
'onsite'
- APPLICATION_TYPE_OFFSITE =
'offsite'
- OAUTH_SITES =
{ :prod => 'http://api.myspace.com', :stage => 'http://stage-api.myspace.com' }
- OAUTH_REQUEST_TOKEN_URL =
'/request_token'
- OAUTH_AUTHORIZATION_URL =
'/authorize'
- OAUTH_ACCESS_TOKEN_URL =
'/access_token'
- API_USERINFO_URL =
'/v1/user.json'
- API_ALBUMS_URL =
'/v1/users/%s/albums.json'
- API_ALBUM_URL =
'/v1/users/%s/albums/%s/photos.json'
- API_FRIENDS_URL =
'/v1/users/%s/friends.json'
- API_FRIENDSHIP_URL =
'/v1/users/%s/friends/%s.json'
- API_MOOD_URL =
'/v1/users/%s/mood.json'
- API_PHOTOS_URL =
'/v1/users/%s/photos.json'
- API_PHOTO_URL =
'/v1/users/%s/photos/%s.json'
- API_PROFILE_URL =
'/v1/users/%s/profile.json'
- API_STATUS_URL =
'/v1/users/%s/status.json'
- API_STATUS_PUT_URL =
'/v1/users/%s/status'
- API_VIDEOS_URL =
'/v1/users/%s/videos.json'
- API_VIDEO_URL =
'/v1/users/%s/videos/%s.json'
- API_ACTIVITIES_URL =
'/v1/users/%s/activities.atom'
- API_FRIENDS_ACTIVITIES_URL =
'/v1/users/%s/friends/activities.atom'
- ID_REGEXP =
/[0-9]+/
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#consumer ⇒ Object
readonly
Returns the value of attribute consumer.
-
#http_logger ⇒ Object
Returns the value of attribute http_logger.
-
#request_token ⇒ Object
Returns the value of attribute request_token.
Instance Method Summary collapse
- #call_myspace_api(url, method = :get, params = {}) {|resp.body| ... } ⇒ Object
- #call_myspace_api_json(url, method = :get, params = {}) ⇒ Object
- #call_myspace_api_xml(url, method = :get, params = {}) ⇒ Object
-
#get_access_token(request_token = @request_token) ⇒ Object
Get an access token once the user has authorized us.
- #get_activities(user_id) ⇒ Object
-
#get_album(user_id, album_id) ⇒ Object
Get the photo descriptions for the photos of album
album_id
for the useruser_id
:. -
#get_albums(user_id, params = {}) ⇒ Object
Get the photo album descriptions for the user
user_id
:. -
#get_authorization_url(request_token, callback_url) ⇒ Object
Get the url to which to redirect the user in order to authorize our access to their account.
-
#get_friends(user_id, params = {}) ⇒ Object
Gets the list of friends for the user
user_id
:. - #get_friends_activities(user_id) ⇒ Object
-
#get_friendship(user_id, *friend_ids) ⇒ Object
Tests whether user
user_id
is friends with one or more other users:. -
#get_mood(user_id) ⇒ Object
Gets the mood of user
user_id
:. -
#get_photo(user_id, photo_id) ⇒ Object
Gets the photo description for photo
photo_id
for useruser_id
:. -
#get_photos(user_id, params = {}) ⇒ Object
Gets the photo descriptions for the photos that belong to user
user_id
:. -
#get_profile(user_id, params = {}) ⇒ Object
Gets the profile info for user
user_id
:. -
#get_request_token ⇒ Object
Get an unauthorized request token from MySpace.
-
#get_status(user_id) ⇒ Object
Gets the status of user
user_id
:. -
#get_userid ⇒ Object
Get the user id of the currently logged in user.
-
#get_video(user_id, video_id) ⇒ Object
Gets the video description for the video
video_id
of useruser_id
:. -
#get_videos(user_id) ⇒ Object
Gets the video descriptions for the videos of user
user_id
:. -
#initialize(oauth_token_key, oauth_token_secret, params = {}) ⇒ MySpace
constructor
Save the application key/secret(s) and initialize OAuth code.
- #set_status(user_id, status) ⇒ Object
Constructor Details
#initialize(oauth_token_key, oauth_token_secret, params = {}) ⇒ MySpace
Save the application key/secret(s) and initialize OAuth code.
If optional param :application_type
is passed, it must be one of MySpace::APPLICATION_TYPE_ONSITE
or MySpace::APPLICATION_TYPE_OFFSITE
. If the application is an onsite application, an access token is not required, since the user must separately give your application permission to access their data. If the application is an offsite application, it must get an access token from the user to access their data.
If optional param :request_token
is passed :request_token_secret
must also be passed, and they will be used to create the default argument to MySpace#get_access_token below. If optional param :access_token
is passed, :access_token_secret
must also be passed, and they will be used to create the access token for the REST API calls.
If optional param :site
is passed, it must be either :prod
or :stage
, and MySpace OAuth calls will be directed to either the production or stage API server accordingly.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/myspace/myspace.rb', line 58 def initialize(oauth_token_key, oauth_token_secret, params = {}) @http_logger = params[:logger] site = params[:site] || :prod @consumer = ::OAuth::Consumer.new(oauth_token_key, oauth_token_secret, :scheme => :query_string, :http_method => :get, :site => OAUTH_SITES[site], :request_token_path => OAUTH_REQUEST_TOKEN_URL, :access_token_path => OAUTH_ACCESS_TOKEN_URL, :authorize_path => OAUTH_AUTHORIZATION_URL) if params[:application_type] == APPLICATION_TYPE_ONSITE @access_token = ::OAuth::AccessToken.new(@consumer, "", "") elsif params[:access_token] @access_token = ::OAuth::AccessToken.new(@consumer, params[:access_token], params[:access_token_secret]) end if params[:request_token] @request_token = ::OAuth::RequestToken.new(@consumer, params[:request_token], params[:request_token_secret]) end end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
35 36 37 |
# File 'lib/myspace/myspace.rb', line 35 def access_token @access_token end |
#consumer ⇒ Object (readonly)
Returns the value of attribute consumer.
32 33 34 |
# File 'lib/myspace/myspace.rb', line 32 def consumer @consumer end |
#http_logger ⇒ Object
Returns the value of attribute http_logger.
33 34 35 |
# File 'lib/myspace/myspace.rb', line 33 def http_logger @http_logger end |
#request_token ⇒ Object
Returns the value of attribute request_token.
34 35 36 |
# File 'lib/myspace/myspace.rb', line 34 def request_token @request_token end |
Instance Method Details
#call_myspace_api(url, method = :get, params = {}) {|resp.body| ... } ⇒ Object
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/myspace/myspace.rb', line 513 def call_myspace_api(url, method = :get, params = {}, &block) params = params.dup body = params.delete(:body) headers = params.delete(:headers) query_str = params.collect do |key, value| CGI.escape(key.to_s) + '=' + CGI.escape(value.to_s) end.join('&') url << '?' + query_str if query_str.length > 0 @http_logger.info("sending: '#{url}'") if @http_logger resp = @access_token.request(method, url, body, headers) @http_logger.info("received: '#{resp.code}': '#{resp.body}'") if @http_logger validate_response(resp, url) yield(resp.body) end |
#call_myspace_api_json(url, method = :get, params = {}) ⇒ Object
531 532 533 534 535 |
# File 'lib/myspace/myspace.rb', line 531 def call_myspace_api_json(url, method = :get, params = {}) call_myspace_api(url, method, params) do |body| JSON::parse(body) end end |
#call_myspace_api_xml(url, method = :get, params = {}) ⇒ Object
537 538 539 540 541 |
# File 'lib/myspace/myspace.rb', line 537 def call_myspace_api_xml(url, method = :get, params = {}) call_myspace_api(url, method, params) do |body| REXML::Document.new(body) end end |
#get_access_token(request_token = @request_token) ⇒ Object
Get an access token once the user has authorized us.
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/myspace/myspace.rb', line 97 def get_access_token(request_token = @request_token) # response = @consumer.token_request(@consumer.http_method, # (@consumer.access_token_url? ? @consumer.access_token_url : @consumer.access_token_path), # request_token, # {}, # headers) # @access_token = ::OAuth::AccessToken.new(@consumer, response[:oauth_token], response[:oauth_token_secret]) @access_token = request_token.get_access_token end |
#get_activities(user_id) ⇒ Object
492 493 494 495 496 497 |
# File 'lib/myspace/myspace.rb', line 492 def get_activities(user_id) user_id = user_id.to_s validate_identifier(:user_id, user_id) url = sprintf(API_ACTIVITIES_URL, user_id) call_myspace_api_xml(url) end |
#get_album(user_id, album_id) ⇒ Object
Get the photo descriptions for the photos of album album_id
for the user user_id
:
[{“smallImageUri”=> “”, “photoUri”=> “api.myspace.com/v1/users/456073223/albums/40418/photos/100809”, “id”=>100809, “uploadDate”=>“2/27/2009 10:14:12 AM”, “caption”=>“”, “lastUpdatedDate”=>“”, “imageUri”=> “
”, “user”=> {“name”=>“Bob”, “uri”=>“api.myspace.com/v1/users/456073223”, “webUri”=>“www.myspace.com/bobvontestacount”, “largeImage”=> “
”, “userType”=>“RegularUser”, “userId”=>456073223, “image”=> “
”}], “count”=>1, “user”=> “uri”=>“api.myspace.com/v1/users/456073223”, “webUri”=>“www.myspace.com/bobvontestacount”, “largeImage”=> “
”, “userType”=>“RegularUser”, “userId”=>456073223, “image”=> “
”}
190 191 192 193 194 195 196 197 |
# File 'lib/myspace/myspace.rb', line 190 def get_album(user_id, album_id) user_id = user_id.to_s validate_identifier(:user_id, user_id) album_id = album_id.to_s validate_identifier(:album_id, album_id) url = sprintf(API_ALBUM_URL, user_id, album_id) call_myspace_api_json(url) end |
#get_albums(user_id, params = {}) ⇒ Object
Get the photo album descriptions for the user user_id
:
[{“photosUri”=> “api.myspace.com/v1/users/456073223/albums/40418/photos”, “photoCount”=>1, “location”=>“”, “title”=>“My Photos”, “id”=>40418, “defaultImage”=> “”, “privacy”=>“Everyone”, “user”=> {“name”=>“Bob”, “uri”=>“api.myspace.com/v1/users/456073223”, “webUri”=>“www.myspace.com/bobvontestacount”, “largeImage”=> “
”, “userType”=>“RegularUser”, “userId”=>456073223, “image”=> “
”, “albumUri”=>“api.myspace.com/v1/users/456073223/albums/40418”}], “count”=>1, “user”=> “uri”=>“api.myspace.com/v1/users/456073223”, “webUri”=>“www.myspace.com/bobvontestacount”, “largeImage”=> “
”, “userType”=>“RegularUser”, “userId”=>456073223, “image”=> “
”}
148 149 150 151 152 153 |
# File 'lib/myspace/myspace.rb', line 148 def get_albums(user_id, params = {}) user_id = user_id.to_s validate_identifier(:user_id, user_id) url = sprintf(API_ALBUMS_URL, user_id) call_myspace_api_json(url, :get, params) end |
#get_authorization_url(request_token, callback_url) ⇒ Object
Get the url to which to redirect the user in order to authorize our access to their account. This url will redirect back to callback_url
once the user authorizes us.
92 93 94 |
# File 'lib/myspace/myspace.rb', line 92 def (request_token, callback_url) "#{request_token.}&oauth_callback=#{CGI::escape(callback_url)}" end |
#get_friends(user_id, params = {}) ⇒ Object
Gets the list of friends for the user user_id
:
href="http://api.myspace.com/v1/users/456073223/friends?list=top">api.myspace.com/v1/users/456073223/friends?list=top”, “Friends”=> [{“name”=>“Tom”, “uri”=>“api.myspace.com/v1/users/6221”, “webUri”=>“www.myspace.com/tom”, “largeImage”=>“”, “userType”=>“RegularUser”, “userId”=>6221, “image”=>“
”], “count”=>1, “user”=> “uri”=>“api.myspace.com/v1/users/456073223”, “webUri”=>“www.myspace.com/bobvontestacount”, “largeImage”=> “
”, “userType”=>“RegularUser”, “userId”=>456073223, “image”=> “
”}
221 222 223 224 225 226 |
# File 'lib/myspace/myspace.rb', line 221 def get_friends(user_id, params = {}) user_id = user_id.to_s validate_identifier(:user_id, user_id) url = sprintf(API_FRIENDS_URL, user_id) call_myspace_api_json(url, :get, params) end |
#get_friends_activities(user_id) ⇒ Object
499 500 501 502 503 504 |
# File 'lib/myspace/myspace.rb', line 499 def get_friends_activities(user_id) user_id = user_id.to_s validate_identifier(:user_id, user_id) url = sprintf(API_FRIENDS_ACTIVITIES_URL, user_id) call_myspace_api_xml(url) end |
#get_friendship(user_id, *friend_ids) ⇒ Object
Tests whether user user_id
is friends with one or more other users:
[{“areFriends”=>true, “friendId”=>6221, “friendId”=>12341234, “friendId”=>456073223], “user”=> “uri”=>“api.myspace.com/v1/users/456073223”, “webUri”=>“www.myspace.com/bobvontestacount”, “largeImage”=> “”, “userType”=>“RegularUser”, “userId”=>456073223, “image”=> “
”}
244 245 246 247 248 249 250 251 252 253 |
# File 'lib/myspace/myspace.rb', line 244 def get_friendship(user_id, *friend_ids) user_id = user_id.to_s validate_identifier(:user_id, user_id) friend_ids.each do |friend_id| friend_id = friend_id.to_s validate_identifier(:friend_ids, friend_id) end url = sprintf(API_FRIENDSHIP_URL, user_id, friend_ids.join(';')) call_myspace_api_json(url) end |
#get_mood(user_id) ⇒ Object
Gets the mood of user user_id
:
“moodImageUrl”=> “”, “moodLastUpdated”=>“2/27/2009 10:19:25 AM”, “user”=> {“name”=>“Bob”, “uri”=>“api.myspace.com/v1/users/456073223”, “webUri”=>“www.myspace.com/bobvontestacount”, “largeImage”=> “
”, “userType”=>“RegularUser”, “userId”=>456073223, “image”=> “
”}
271 272 273 274 275 276 |
# File 'lib/myspace/myspace.rb', line 271 def get_mood(user_id) user_id = user_id.to_s validate_identifier(:user_id, user_id) url = sprintf(API_MOOD_URL, user_id) call_myspace_api_json(url) end |
#get_photo(user_id, photo_id) ⇒ Object
Gets the photo description for photo photo_id
for user user_id
:
“”, “photoUri”=>“api.myspace.com/v1/users/456073223/photos/100809”, “id”=>100809, “uploadDate”=>“2/27/2009 10:14:12 AM”, “caption”=>“”, “lastUpdatedDate”=>“”, “imageUri”=> “
”, “user”=> {“name”=>“Bob”, “uri”=>“api.myspace.com/v1/users/456073223”, “webUri”=>“www.myspace.com/bobvontestacount”, “largeImage”=> “
”, “userType”=>“RegularUser”, “userId”=>456073223, “image”=> “
”}
339 340 341 342 343 344 345 346 |
# File 'lib/myspace/myspace.rb', line 339 def get_photo(user_id, photo_id) user_id = user_id.to_s validate_identifier(:user_id, user_id) photo_id = photo_id.to_s validate_identifier(:photo_id, photo_id) url = sprintf(API_PHOTO_URL, user_id, photo_id) call_myspace_api_json(url) end |
#get_photos(user_id, params = {}) ⇒ Object
Gets the photo descriptions for the photos that belong to user user_id
:
[{“smallImageUri”=> “”, “photoUri”=>“api.myspace.com/v1/users/456073223/photos/100809”, “id”=>100809, “uploadDate”=>“2/27/2009 10:14:12 AM”, “caption”=>“”, “lastUpdatedDate”=>“”, “imageUri”=> “
”, “user”=> {“name”=>“Bob”, “uri”=>“api.myspace.com/v1/users/456073223”, “webUri”=>“www.myspace.com/bobvontestacount”, “largeImage”=> “
”, “userType”=>“RegularUser”, “userId”=>456073223, “image”=> “
”}], “count”=>1, “user”=> “uri”=>“api.myspace.com/v1/users/456073223”, “webUri”=>“www.myspace.com/bobvontestacount”, “largeImage”=> “
”, “userType”=>“RegularUser”, “userId”=>456073223, “image”=> “
”}
311 312 313 314 315 316 |
# File 'lib/myspace/myspace.rb', line 311 def get_photos(user_id, params = {}) user_id = user_id.to_s validate_identifier(:user_id, user_id) url = sprintf(API_PHOTOS_URL, user_id) call_myspace_api_json(url, :get, params) end |
#get_profile(user_id, params = {}) ⇒ Object
Gets the profile info for user user_id
:
“city”=>“BEVERLY HILLS”, “country”=>“US”, “postalcode”=>“90210”, “gender”=>“Male”, “type”=>“full”, “culture”=>“en-US”, “aboutme”=>“”, “hometown”=>“”, “basicprofile”=> {“name”=>“Bob”, “uri”=>“api.myspace.com/v1/users/456073223”, “webUri”=>“www.myspace.com/bobvontestacount”, “largeImage”=> “”, “userId”=>456073223, “lastUpdatedDate”=>“2/27/2009 10:20:02 AM”, “image”=> “
”, “age”=>88, “maritalstatus”=>“Single”}
371 372 373 374 375 376 |
# File 'lib/myspace/myspace.rb', line 371 def get_profile(user_id, params = {}) user_id = user_id.to_s validate_identifier(:user_id, user_id) url = sprintf(API_PROFILE_URL, user_id) call_myspace_api_json(url, :get, params) end |
#get_request_token ⇒ Object
Get an unauthorized request token from MySpace.
85 86 87 |
# File 'lib/myspace/myspace.rb', line 85 def get_request_token @consumer.get_request_token end |
#get_status(user_id) ⇒ Object
Gets the status of user user_id
:
“moodImageUrl”=> “”, “moodLastUpdated”=>“2/27/2009 10:19:25 AM”, “user”=> {“name”=>“Bob”, “uri”=>“api.myspace.com/v1/users/456073223”, “webUri”=>“www.myspace.com/bobvontestacount”, “largeImage”=> “
”, “userType”=>“RegularUser”, “userId”=>456073223, “image”=> “
”, “status”=>“Testing”}
395 396 397 398 399 400 |
# File 'lib/myspace/myspace.rb', line 395 def get_status(user_id) user_id = user_id.to_s validate_identifier(:user_id, user_id) url = sprintf(API_STATUS_URL, user_id) call_myspace_api_json(url) end |
#get_userid ⇒ Object
Get the user id of the currently logged in user.
109 110 111 112 |
# File 'lib/myspace/myspace.rb', line 109 def get_userid() user_info = call_myspace_api_json(API_USERINFO_URL) user_info['userId'].to_s end |
#get_video(user_id, video_id) ⇒ Object
Gets the video description for the video video_id
of user user_id
:
“title”=>“110403na”, “resourceuserid”=>“456073223”, “mediastatus”=>“ProcessingSuccessful”, “dateupdated”=>“3/5/2009 11:24:23 AM”, “country”=>“US”, “totalviews”=>“0”, “thumbnail”=> “”, “language”=>“en”, “id”=>53551799, “totalcomments”=>“0”, “runtime”=>“219”, “datecreated”=>“3/5/2009 11:24:23 AM”, “privacy”=>“Public”, “mediatype”=>“4”, “description”=>“110403na”, “user”=> {“name”=>“Bob”, “uri”=>“api.myspace.com/v1/users/456073223”, “webUri”=>“www.myspace.com/bobvontestacount”, “largeImage”=> “
”, “userType”=>“RegularUser”, “userId”=>456073223, “image”=> “
”, “totalvotes”=>“0”, “videoUri”=>“api.myspace.com/v1/users/456073223/videos/53551799”}
483 484 485 486 487 488 489 490 |
# File 'lib/myspace/myspace.rb', line 483 def get_video(user_id, video_id) user_id = user_id.to_s validate_identifier(:user_id, user_id) video_id = video_id.to_s validate_identifier(:video_id, video_id) url = sprintf(API_VIDEO_URL, user_id, video_id) call_myspace_api_json(url) end |
#get_videos(user_id) ⇒ Object
Gets the video descriptions for the videos of user user_id
:
[{“totalrating”=>“0”, “title”=>“110403na”, “resourceuserid”=>“456073223”, “mediastatus”=>“ProcessingSuccessful”, “dateupdated”=>“3/5/2009 11:24:23 AM”, “country”=>“US”, “totalviews”=>“0”, “thumbnail”=> “”, “language”=>“en”, “id”=>53551799, “totalcomments”=>“0”, “runtime”=>“219”, “datecreated”=>“3/5/2009 11:24:23 AM”, “privacy”=>“Public”, “mediatype”=>“4”, “description”=>“110403na”, “user”=> {“name”=>“Bob”, “uri”=>“api.myspace.com/v1/users/456073223”, “webUri”=>“www.myspace.com/bobvontestacount”, “largeImage”=> “
”, “userType”=>“RegularUser”, “userId”=>456073223, “image”=> “
”, “totalvotes”=>“0”, “videoUri”=>“api.myspace.com/v1/users/456073223/videos/53551799”}], “count”=>1, “user”=> “uri”=>“api.myspace.com/v1/users/456073223”, “webUri”=>“www.myspace.com/bobvontestacount”, “largeImage”=> “
”, “userType”=>“RegularUser”, “userId”=>456073223, “image”=> “
”}
445 446 447 448 449 450 |
# File 'lib/myspace/myspace.rb', line 445 def get_videos(user_id) user_id = user_id.to_s validate_identifier(:user_id, user_id) url = sprintf(API_VIDEOS_URL, user_id) call_myspace_api_json(url) end |
#set_status(user_id, status) ⇒ Object
506 507 508 509 510 511 |
# File 'lib/myspace/myspace.rb', line 506 def set_status(user_id, status) user_id = user_id.to_s validate_identifier(:user_id, user_id) url = sprintf(API_STATUS_PUT_URL, user_id) call_myspace_api_xml(url, :put, {:body => {:status => status}}) end |