lib/myspace/myspace.rb in myspaceid-sdk-0.1.9 vs lib/myspace/myspace.rb in myspaceid-sdk-0.1.11
- old
+ new
@@ -14,36 +14,43 @@
OAUTH_ACCESS_TOKEN_URL = '/access_token' unless const_defined?('OAUTH_ACCESS_TOKEN_URL')
# tests regularly timeout at 2 seconds
TIMEOUT_SECS = 3 unless const_defined?('TIMEOUT_SECS')
- EndPoint.define(:user_info, '/v1/user', :get, :v1_json)
- EndPoint.define(:albums, '/v1/users/{user_id}/albums', :get, :v1_json)
- EndPoint.define(:album, '/v1/users/{user_id}/albums/{album_id}/photos', :get, :v1_json)
- EndPoint.define(:friends, '/v1/users/{user_id}/friends', :get, :v1_json)
- EndPoint.define(:friendship, '/v1/users/{user_id}/friends/{friend_ids}', :get, :v1_json)
- EndPoint.define(:mood, '/v1/users/{user_id}/mood', :get, :v1_json)
- EndPoint.define(:photos, '/v1/users/{user_id}/photos', :get, :v1_json)
- EndPoint.define(:photo, '/v1/users/{user_id}/photos/{photo_id}', :get, :v1_json)
- EndPoint.define(:profile, '/v1/users/{user_id}/profile', :get, :v1_json)
- EndPoint.define(:status_get, '/v1/users/{user_id}/status', :get, :v1_json)
- EndPoint.define(:status_put, '/v1/users/{user_id}/status', :put)
- EndPoint.define(:videos, '/v1/users/{user_id}/videos', :get, :v1_json)
- EndPoint.define(:video, '/v1/users/{user_id}/videos/{video_id}', :get, :v1_json)
- EndPoint.define(:activities, '/v1/users/{user_id}/activities.atom', :get)
- EndPoint.define(:friends_activities, '/v1/users/{user_id}/friends/activities.atom', :get)
- EndPoint.define(:appdata_global_get, '/v1/appdata/global', :get, :v1_json)
- EndPoint.define(:appdata_global_keys_get, '/v1/appdata/global/{keys}', :get, :v1_json)
- EndPoint.define(:appdata_global_put, '/v1/appdata/global', :put)
- EndPoint.define(:appdata_global_delete, '/v1/appdata/global/{keys}', :delete)
- EndPoint.define(:appdata_user_get, '/v1/users/{user_id}/appdata', :get, :v1_json)
- EndPoint.define(:appdata_user_keys_get, '/v1/users/{user_id}/appdata/{keys}', :get, :v1_json)
- EndPoint.define(:appdata_user_put, '/v1/users/{user_id}/appdata', :put)
- EndPoint.define(:appdata_user_delete, '/v1/users/{user_id}/appdata/{keys}', :delete)
- EndPoint.define(:appdata_friends_get, '/v1/users/{user_id}/friends/appdata', :get, :v1_json)
- EndPoint.define(:appdata_friends_keys_get, '/v1/users/{user_id}/friends/appdata/{keys}', :get, :v1_json)
+ EndPoint.define(:user_info, '/v1/user', :get, :v1_json)
+ EndPoint.define(:albums, '/v1/users/{user_id}/albums', :get, :v1_json)
+ EndPoint.define(:album_info, '/v1/users/{user_id}/albums/{album_id}', :get, :v1_json)
+ EndPoint.define(:album, '/v1/users/{user_id}/albums/{album_id}/photos', :get, :v1_json)
+ EndPoint.define(:friends, '/v1/users/{user_id}/friends', :get, :v1_json)
+ EndPoint.define(:friendship, '/v1/users/{user_id}/friends/{friend_ids}', :get, :v1_json)
+ EndPoint.define(:friends_list, '/v1/users/{user_id}/friendslist/{friend_ids}', :get, :v1_json)
+ EndPoint.define(:mood_get, '/v1/users/{user_id}/mood', :get, :v1_json)
+ EndPoint.define(:mood_put, '/v1/users/{user_id}/mood', :put)
+ EndPoint.define(:moods, '/v1/users/{user_id}/moods', :get, :v1_json)
+ EndPoint.define(:photos, '/v1/users/{user_id}/photos', :get, :v1_json)
+ EndPoint.define(:photo, '/v1/users/{user_id}/photos/{photo_id}', :get, :v1_json)
+ EndPoint.define(:profile, '/v1/users/{user_id}/profile', :get, :v1_json)
+ EndPoint.define(:status_get, '/v1/users/{user_id}/status', :get, :v1_json)
+ EndPoint.define(:status_put, '/v1/users/{user_id}/status', :put)
+ EndPoint.define(:videos, '/v1/users/{user_id}/videos', :get, :v1_json)
+ EndPoint.define(:video, '/v1/users/{user_id}/videos/{video_id}', :get, :v1_json)
+ EndPoint.define(:activities, '/v1/users/{user_id}/activities.atom', :get)
+ EndPoint.define(:friends_activities, '/v1/users/{user_id}/friends/activities.atom', :get)
+ EndPoint.define(:appdata_global_get, '/v1/appdata/global', :get, :v1_json)
+ EndPoint.define(:appdata_global_keys_get, '/v1/appdata/global/{keys}', :get, :v1_json)
+ EndPoint.define(:appdata_global_put, '/v1/appdata/global', :put)
+ EndPoint.define(:appdata_global_delete, '/v1/appdata/global/{keys}', :delete)
+ EndPoint.define(:appdata_user_get, '/v1/users/{user_id}/appdata', :get, :v1_json)
+ EndPoint.define(:appdata_user_keys_get, '/v1/users/{user_id}/appdata/{keys}', :get, :v1_json)
+ EndPoint.define(:appdata_user_put, '/v1/users/{user_id}/appdata', :put)
+ EndPoint.define(:appdata_user_delete, '/v1/users/{user_id}/appdata/{keys}', :delete)
+ EndPoint.define(:appdata_friends_get, '/v1/users/{user_id}/friends/appdata', :get, :v1_json)
+ EndPoint.define(:appdata_friends_keys_get, '/v1/users/{user_id}/friends/appdata/{keys}', :get, :v1_json)
+ EndPoint.define(:indicators, '/v1/users/{user_id}/indicators', :get, :v1_json)
+
+
attr_reader :consumer
attr_accessor :http_logger
attr_accessor :request_token
attr_accessor :access_token
@@ -72,10 +79,11 @@
@http_logger = params[:logger]
site = params[:site] || :prod
@consumer = ::OAuth::Consumer.new(oauth_token_key,
oauth_token_secret,
:scheme => :query_string,
+ # :scheme => :header,
: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)
@@ -162,10 +170,39 @@
user_id = user_id.to_s
validate_identifier(:user_id, user_id)
call_myspace_api(:albums, params.dup.update(:user_id => user_id, :v1_json => true))
end
+ # Get the photo album description for user +user_id+ and album +album_id+
+ #
+ # {"photosUri"=>"http://api.myspace.com/v1/users/456073223/albums/40418/photos",
+ # "photoCount"=>1,
+ # "location"=>"",
+ # "title"=>"My Photos",
+ # "id"=>40418,
+ # "defaultImage"=>
+ # "http://c1.ac-images.myspacecdn.com/images02/45/m_f820313641924f0f90004932c8bc310c.jpg",
+ # "privacy"=>"Everyone",
+ # "user"=>
+ # {"name"=>"Bob",
+ # "uri"=>"http://api.myspace.com/v1/users/456073223",
+ # "webUri"=>"http://www.myspace.com/bobvontestacount",
+ # "largeImage"=>
+ # "http://c1.ac-images.myspacecdn.com/images02/45/l_f820313641924f0f90004932c8bc310c.jpg",
+ # "userType"=>"RegularUser",
+ # "userId"=>456073223,
+ # "image"=>
+ # "http://c1.ac-images.myspacecdn.com/images02/45/s_f820313641924f0f90004932c8bc310c.jpg"},
+ # "albumUri"=>"http://api.myspace.com/v1/users/456073223/albums/40418"}
+ def get_album_info(user_id, album_id, params = {})
+ user_id = user_id.to_s
+ validate_identifier(:user_id, user_id)
+ album_id = album_id.to_s
+ validate_identifier(:album_id, album_id)
+ call_myspace_api(:album_info, params.dup.update(:user_id => user_id, :album_id => album_id, :v1_json => true))
+ end
+
# Get the photo descriptions for the photos of album +album_id+
# for the user +user_id+:
#
# {"photos"=>
# [{"smallImageUri"=>
@@ -259,10 +296,23 @@
validate_identifier(:friend_ids, friend_id)
end
call_myspace_api(:friendship, :user_id => user_id, :friend_ids => friend_ids.join(';'), :v1_json => true)
end
+ # Gets the list of friends for the user +user_id+, for the friends
+ # in +friend_ids+. Use this call if you only need information
+ # about a specific set of friends whose ids you already know.
+ def get_friends_list(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
+ call_myspace_api(:friends_list, :user_id => user_id, :friend_ids => friend_ids.join(';'), :v1_json => true)
+ end
+
# Gets the mood of user +user_id+:
#
# {"mood"=>"tested",
# "moodImageUrl"=>
# "http://x.myspacecdn.com/images/blog/moods/iBrads/indescribable.gif",
@@ -278,13 +328,54 @@
# "image"=>
# "http://c1.ac-images.myspacecdn.com/images02/45/s_f820313641924f0f90004932c8bc310c.jpg"}}
def get_mood(user_id, params = {})
user_id = user_id.to_s
validate_identifier(:user_id, user_id)
- call_myspace_api(:mood, params.dup.update(:user_id => user_id, :v1_json => true))
+ call_myspace_api(:mood_get, params.dup.update(:user_id => user_id, :v1_json => true))
end
+ # Sets the mood of the user +user_id+ to +mood_id+, which must be
+ # a number from this list:
+ #
+ # http://wiki.developer.myspace.com/index.php?title=Myspace_mood_data_names_codes_images
+ def set_mood(user_id, mood_id)
+ user_id = user_id.to_s
+ validate_identifier(:user_id, user_id)
+ mood_id = mood_id.to_s
+ validate_identifier(:mood_id, mood_id)
+ call_myspace_api(:mood_put, :user_id => user_id, :body => {:mood => mood_id})
+ end
+
+ # Gets and caches the list of available moods for user +user_id+.
+ def moods(user_id)
+ @moods ||= {}
+ @moods[user_id] ||= get_moods(user_id)
+ end
+
+ # Gets the mood names available for user +user_id+.
+ def mood_names(user_id)
+ moods(user_id).collect do |mood|
+ mood['moodName']
+ end
+ end
+
+ # Given a +user_id+ and the name of a mood, returns the
+ # corresponding mood id
+ def mood_id(user_id, name)
+ moods(user_id).each do |mood|
+ return mood['moodId'] if mood['moodName'] == name
+ end
+ end
+
+ # Gets the list of available moods for user +user_id+.
+ def get_moods(user_id, params = {})
+ user_id = user_id.to_s
+ validate_identifier(:user_id, user_id)
+ moods = call_myspace_api(:moods, params.dup.update(:user_id => user_id, :v1_json => true))
+ moods['moods']
+ end
+
# Gets the photo descriptions for the photos that belong to user +user_id+:
#
# {"photos"=>
# [{"smallImageUri"=>
# "http://c1.ac-images.myspacecdn.com/images02/45/s_f820313641924f0f90004932c8bc310c.jpg",
@@ -496,42 +587,37 @@
video_id = video_id.to_s
validate_identifier(:video_id, video_id)
call_myspace_api(:video, params.dup.update(:user_id => user_id, :video_id => video_id, :v1_json => true))
end
+ # Gets the activity stream of user +user_id+
def get_activities(user_id, params = {})
user_id = user_id.to_s
validate_identifier(:user_id, user_id)
call_myspace_api(:activities, params.dup.update(:user_id => user_id))
end
+ # Gets the activity streams of the friends of user +user_id+
def get_friends_activities(user_id, params = {})
user_id = user_id.to_s
validate_identifier(:user_id, user_id)
call_myspace_api(:friends_activities, params.dup.update(:user_id => user_id))
end
-
+
+ # Gets the global application data. This can be anything the
+ # application wants. If you pass +keys+, only return data
+ # corresponding to the passed keys.
def get_global_appdata(*keys)
- appdata_to_hash do
+ MySpace.appdata_to_hash do
if keys.length > 0
call_myspace_api(:appdata_global_keys_get, :keys => keys.join(';'), :v1_json => true)
else
call_myspace_api(:appdata_global_get, :v1_json => true)
end
end
end
- def self.remove_null_values(hash)
- hash.keys.inject([]) do |nulls, key|
- unless hash[key]
- hash.delete(key)
- nulls << key
- end
- nulls
- end
- end
-
def set_global_appdata(params = {})
deletes = MySpace.remove_null_values(params)
call_myspace_api(:appdata_global_put, :body => params) if params.length > 0
call_myspace_api(:appdata_global_delete, :keys => deletes.join(';')) if deletes.length > 0
@@ -542,11 +628,11 @@
end
def get_user_appdata(user_id, *keys)
user_id = user_id.to_s
validate_identifier(:user_id, user_id)
- appdata_to_hash do
+ MySpace.appdata_to_hash do
if keys.length > 0
call_myspace_api(:appdata_user_keys_get, :user_id => user_id, :keys => keys.join(';'), :v1_json => true)
else
call_myspace_api(:appdata_user_get, :user_id => user_id, :v1_json => true)
end
@@ -568,27 +654,46 @@
end
def get_user_friends_appdata(user_id, *keys)
user_id = user_id.to_s
validate_identifier(:user_id, user_id)
- appdata_to_hash do
- if keys.length > 0
- call_myspace_api(:appdata_friends_get, :user_id => user_id, :v1_json => true)
- else
- call_myspace_api(:appdata_friends_keys_get, :user_id => user_id, :keys => keys.join(';'), :v1_json => true)
+ if keys.length > 0
+ call_myspace_api(:appdata_friends_keys_get, :user_id => user_id, :keys => keys.join(';'), :v1_json => true)
+ else
+ call_myspace_api(:appdata_friends_get, :user_id => user_id, :v1_json => true)
+ end.inject({}) do |hash, friend|
+ hash.update(friend['userid'].to_s => MySpace.appdata_to_hash(friend))
+ end
+ end
+
+ protected
+
+ def self.remove_null_values(hash)
+ hash.keys.inject([]) do |nulls, key|
+ unless hash[key]
+ hash.delete(key)
+ nulls << key
end
+ nulls
end
end
- def appdata_to_hash(&block)
- appdata = yield
+ def self.appdata_to_hash(appdata = {}, &block)
+ appdata = yield if block_given?
return {} unless appdata['keyvaluecollection']
appdata['keyvaluecollection'].inject({}) do |hash, entry|
hash.update(entry['key'].to_sym => entry['value'])
end
end
+ public
+
+ def get_indicators(user_id)
+ user_id = user_id.to_s
+ validate_identifier(:user_id, user_id)
+ call_myspace_api(:indicators, :user_id => user_id, :v1_json => true)
+ end
def call_myspace_api(name, params = {}, &block)
params = params.dup
ep = EndPoint.find(name)
url = ep.compute_path(params)
@@ -627,22 +732,10 @@
raise "unknown content type: #{content_type}"
end
end
- # def call_myspace_api_json(url, method = :get, params = {})
- # call_myspace_api(url, method, params) do |body|
- # JSON::parse(body)
- # end
- # end
-
- # def call_myspace_api_xml(url, method = :get, params = {})
- # call_myspace_api(url, method, params) do |body|
- # REXML::Document.new(body)
- # end
- # end
-
protected
ID_REGEXP = /[0-9]+/ unless const_defined?('ID_REGEXP')
def validate_identifier(parameter, identifier)
@@ -662,5 +755,6 @@
raise RestException.new(code, response.message, url) unless ['200', '201'].include?(code)
end
end
end
end
+