MySpace API

This is the official MySpace Ruby library, which communicates to the MySpace API REST servers and converts JSON into proper Ruby objects.

In order to use the library, you need to sign up for a developer account and ensure you have the oath and json modules installed.

Developer Account, Access Keys and Permissions

To get a developer account, you need to sign up at:

developer.myspace.com

Once approved, you will need to create an application to get your api key and secret key. The api key is a http URL (e.g. www.myspace.com/2543521521) and the secret key will be a 32 character hex string.

You can find the keys in the Application Builder, by clicking on the ‘Edit App Information’ link. The apiKey is the ‘Application Uri’ and the secretKey is ‘Application Domain’.

The keys can be placed into myspace.conf or specified at runtime when you new a MySpace::Connection. The myspace.conf file can be in the current directory where the script is being run from, or can be specified by setting the MYSPACE_CONF environment variable.

You only have access to users that have installed your application. Also, write access to these user’s profiles (to update status or upload a picture) is at the user’s discretion.

Depencencies

This library has some external dependencies:

oauth     ( http://oauth.googlecode.com/svn/code/ruby )
json/pure ( http://json.rubyforge.org )

You can install json via rubygem, which should provide a native C ‘ext’ and pure ruby ‘pure’ extension. The native ‘json/ext’ module is faster, but could lead to some compatibility issues, so we stuck with the pure variant.

At this point, oauth is not available as a rubygem and needs to be installed from the URL above.

Usage

 require "myspace"

 userId    = 123
 friendId  = 1234
 albumId   = 12345

 # NOTE:
 # Permission to access a user's data is restricted -- you can't just access
 # any user's data.  In order to access a user's data, they need to have
 # installed your app.

 # Snag a MySpace::User object for user with id userId
 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
 user = MySpace::User.get(userId)
 puts user.name + " has a profile picture at " + user.image

 # Grab an an album
 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
 album = MySpace::Album.get(userId,albumId)
 if album.photoCount > 1
   puts "album at #{album.albumUri} has #{album.photoCount} photos"
 else
   puts "#{album.title} has only one photo"
 end

 # Iterate through all the photos for a user
 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
 photos = MySpace::Photos.get(userId)
 puts "User has #{photos.count} photos"
 photos.photos.each {|photo|
   puts "id #{photo.id}, caption #{photo.caption}, imageUri #{photo.imageUri}"
 }

 # Read a user's interests
 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
 interests = MySpace::Interest.get(userId)
 puts "shrug at the fountain" if interests.books =~ /rand/i

 # Read a user's profile data
 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
 profile = MySpace::Profile.get(userId)
 puts "You #{profile.age >= 18 ? "can" : "can't"} vote"

 # Check if two or more users are friendly
 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
 include MySpace

 friendId  = 92281327

 friendship = Friendship.get(userId,friendId)
 friend = friends.friendship.first
   puts "user #{userId} is#{friend.areFriends ? "" : " not"} friends with user #{friend.friendId}"
 }

 friendIds = "92281327;92281328"

 friendship = Friendship.get(userId,friendIds)
 friendship.friends.each {|f|
   puts "user #{userId} is#{f.areFriends ? "" : " not"} friends with user #{f.friendId}"
 }

 # Friends
 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
 friends = MySpace::Friends.get(userId, {:page => 1, :page_size => 200})
 puts "User #{userId} has #{friends.count} friends."

 # friends.friends is an Array of MySpace::User objects
 friends.friends.each {|user|
   puts "user #{user.name} #{user.webUri}"
 }

 # Mood
 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
 puts "user id #{userId} mood " + MySpace::Mood.get(userId).mood 

 # Get info on a user's videos
 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
 MySpace::Videos.get(userId).videos.each {|vid|
   puts "
   Name: #{vid.description.empty? ? "(no description)" : vid.description} (id #{vid.id})
Runtime: #{vid.runtime} seconds
 Rating: #{vid.totalrating}
  Views: #{vid.totalviews}
  Votes: #{vid.totalvotes}

}

# Profile data detail
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
detail = MySpace::Detail.get(userId)
if detail.drink =~ /no/i && detail.smoke =~ /no/
  puts "What *do* you do?"
end
puts detail.instance_variables.collect {|d| "#{d} = #{detail.instance_variable_get d}\n" }

# User's status
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
puts "User has status: #{MySpace::Status.get(userId).status}"

# Kitchen sink
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
require "pp"

%w( Detail Friends Groups Interest Mood Photos Profile Status User Videos ).each {|klass|
  puts "\n+-+-+-+-+-+-+-+-+-+-+ #{klass} +-+-+-+-+-+-+-+-+-+-+-+-+"
  obj = eval("MySpace::#{klass}")
  pp obj.send(:get,userId)
}

Class Reference

Album

  • albumUri (String)

  • defaultImage (String) (String)

  • id (Fixnum)

  • location (String)

  • photoCount (Fixnum)

  • photosUri (String)

  • privacy (String)

  • title (String)

  • user (MySpace::User)

Detail

  • user (MySpace::User)

  • status (String)

  • ethnicity (String)

  • drink (String)

  • zodiacsign (String)

  • orientation (String)

  • religion (String)

  • herefor (String)

  • smoke (String)

  • education (String)

  • income (String)

  • children (String)

  • hometown (String)

  • bodyType (String)

FriendStatus

  • friendId (Fixnum)

  • areFriends (TrueClass FalseClass)

Friendship

  • user (MySpace::User)

  • friendship (Array MySpace::FriendStatus)

Friends

  • user (MySpace::User)

  • topFriends (Array MySpace::User)

  • friends (Array MySpace::User)

  • next (String)

  • prev (String)

  • count (Fixnum)

Interest

  • user (MySpace::User)

  • heroes (String)

  • general (String)

  • music (String)

  • television (String)

  • movies (String)

  • books (String)

Mood

  • user (MySpace::User)

  • mood (String)

Photos

  • user (MySpace::User)

  • photos (Array MySpace::Photo)

  • count (Fixnum)

Photo

  • caption (String)

  • id (Fixnum)

  • imageUri (String)

  • photoUri (String)

  • user (MySpace::User)

Profile

  • city (String)

  • region (String)

  • country (String)

  • gender (String)

  • postalcode (String)

  • culture (String)

  • basicprofile (String)

  • age (Fixnum)

  • aboutme (String)

  • maritalstatus (String)

  • hometown (String)

Status

  • user (MySpace::User)

  • status (String)

User

  • name (String)

  • uri (String)

  • webUri (String)

  • userType (String)

  • userId (Fixnum)

  • onlineNow (TrueClass FalseClass)

  • image (String)

Videos

  • videos (Array MySpace::Video)

  • count (Fixnum)

  • prev (String)

  • next (String)

Video

  • country (String)

  • datecreated (String)

  • dateupdated (String)

  • description (String)

  • id (Fixnum)

  • image (String)

  • language (String)

  • mediastatus (String)

  • mediatype (String)

  • name (String)

  • onlineNow (TrueClass FalseClass)

  • privacy (String)

  • resourceuserid (String)

  • runtime (String)

  • thumbnail (String)

  • title (String)

  • totalrating (String)

  • totalviews (String)

  • totalvotes (String)

  • uri (String)

  • user (MySpace::User)

  • userId (Fixnum)

  • userType (String)

  • videoUri (String)

License

myspace-ruby is copyright© 2008 MySpace. It is free software, and is distributed under the MIT license.

Warranty

This software is provided “as is” and without any express or implied warranties, including, without limitation, the implied warranties of merchantibility and fitness for a particular purpose.

Author

Chris Bell, copyright © 2008, MySpace