lib/myspace/myspace.rb in myspaceid-sdk-0.1.8 vs lib/myspace/myspace.rb in myspaceid-sdk-0.1.9
- old
+ new
@@ -11,26 +11,39 @@
OAUTH_REQUEST_TOKEN_URL = '/request_token' unless const_defined?('OAUTH_REQUEST_TOKEN_URL')
OAUTH_AUTHORIZATION_URL = '/authorize' unless const_defined?('OAUTH_AUTHORIZATION_URL')
OAUTH_ACCESS_TOKEN_URL = '/access_token' unless const_defined?('OAUTH_ACCESS_TOKEN_URL')
- API_USERINFO_URL = '/v1/user.json' unless const_defined?('API_USERINFO_URL')
- API_ALBUMS_URL = '/v1/users/%s/albums.json' unless const_defined?('API_ALBUMS_URL')
- API_ALBUM_URL = '/v1/users/%s/albums/%s/photos.json' unless const_defined?('API_ALBUM_URL')
- API_FRIENDS_URL = '/v1/users/%s/friends.json' unless const_defined?('API_FRIENDS_URL')
- API_FRIENDSHIP_URL = '/v1/users/%s/friends/%s.json' unless const_defined?('API_FRIENDSHIP_URL')
- API_MOOD_URL = '/v1/users/%s/mood.json' unless const_defined?('API_MOOD_URL')
- API_PHOTOS_URL = '/v1/users/%s/photos.json' unless const_defined?('API_PHOTOS_URL')
- API_PHOTO_URL = '/v1/users/%s/photos/%s.json' unless const_defined?('API_PHOTO_URL')
- API_PROFILE_URL = '/v1/users/%s/profile.json' unless const_defined?('API_PROFILE_URL')
- API_STATUS_URL = '/v1/users/%s/status.json' unless const_defined?('API_STATUS_URL')
- API_STATUS_PUT_URL = '/v1/users/%s/status' unless const_defined?('API_STATUS_PUT_URL')
- API_VIDEOS_URL = '/v1/users/%s/videos.json' unless const_defined?('API_VIDEOS_URL')
- API_VIDEO_URL = '/v1/users/%s/videos/%s.json' unless const_defined?('API_VIDEO_URL')
- API_ACTIVITIES_URL = '/v1/users/%s/activities.atom' unless const_defined?('API_ACTIVITIES_URL')
- API_FRIENDS_ACTIVITIES_URL = '/v1/users/%s/friends/activities.atom' unless const_defined?('API_FRIENDS_ACTIVITIES_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)
+
attr_reader :consumer
attr_accessor :http_logger
attr_accessor :request_token
attr_accessor :access_token
@@ -105,11 +118,11 @@
@access_token = request_token.get_access_token
end
# Get the user id of the currently logged in user.
def get_userid()
- user_info = call_myspace_api_json(API_USERINFO_URL)
+ user_info = call_myspace_api(:user_info, :v1_json => true)
user_info['userId'].to_s
end
# Get the photo album descriptions for the user +user_id+:
#
@@ -146,12 +159,11 @@
# "image"=>
# "http://c1.ac-images.myspacecdn.com/images02/45/s_f820313641924f0f90004932c8bc310c.jpg"}}
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)
+ call_myspace_api(:albums, params.dup.update(:user_id => user_id, :v1_json => true))
end
# Get the photo descriptions for the photos of album +album_id+
# for the user +user_id+:
#
@@ -185,17 +197,16 @@
# "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"}}
- def get_album(user_id, album_id)
+ def get_album(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)
- url = sprintf(API_ALBUM_URL, user_id, album_id)
- call_myspace_api_json(url)
+ call_myspace_api(:album, params.dup.update(:user_id => user_id, :album_id => album_id, :v1_json => true))
end
# Gets the list of friends for the user +user_id+:
#
# {"topFriends"=>"http://api.myspace.com/v1/users/456073223/friends?list=top",
@@ -219,12 +230,11 @@
# "image"=>
# "http://c1.ac-images.myspacecdn.com/images02/45/s_f820313641924f0f90004932c8bc310c.jpg"}}
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)
+ call_myspace_api(:friends, params.dup.update(:user_id => user_id, :v1_json => true))
end
# Tests whether user +user_id+ is friends with one or more other users:
#
# {"friendship"=>
@@ -246,12 +256,11 @@
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)
+ call_myspace_api(:friendship, :user_id => user_id, :friend_ids => friend_ids.join(';'), :v1_json => true)
end
# Gets the mood of user +user_id+:
#
# {"mood"=>"tested",
@@ -266,15 +275,14 @@
# "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"}}
- def get_mood(user_id)
+ def get_mood(user_id, params = {})
user_id = user_id.to_s
validate_identifier(:user_id, user_id)
- url = sprintf(API_MOOD_URL, user_id)
- call_myspace_api_json(url)
+ call_myspace_api(:mood, params.dup.update(:user_id => user_id, :v1_json => true))
end
# Gets the photo descriptions for the photos that belong to user +user_id+:
#
# {"photos"=>
@@ -309,12 +317,11 @@
# "image"=>
# "http://c1.ac-images.myspacecdn.com/images02/45/s_f820313641924f0f90004932c8bc310c.jpg"}}
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)
+ call_myspace_api(:photos, params.dup.update(:user_id => user_id, :v1_json => true))
end
# Gets the photo description for photo +photo_id+ for user +user_id+:
#
# {"smallImageUri"=>
@@ -334,17 +341,16 @@
# "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"}}
- def get_photo(user_id, photo_id)
+ def get_photo(user_id, photo_id, params = {})
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)
+ call_myspace_api(:photo, params.dup.update(:user_id => user_id, :photo_id => photo_id, :v1_json => true))
end
# Gets the profile info for user +user_id+:
#
# {"region"=>"California",
@@ -369,12 +375,11 @@
# "age"=>88,
# "maritalstatus"=>"Single"}
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)
+ call_myspace_api(:profile, params.dup.update(:user_id => user_id, :v1_json => true))
end
# Gets the status of user +user_id+:
#
# {"mood"=>"tested",
@@ -390,17 +395,23 @@
# "userType"=>"RegularUser",
# "userId"=>456073223,
# "image"=>
# "http://c1.ac-images.myspacecdn.com/images02/45/s_f820313641924f0f90004932c8bc310c.jpg"},
# "status"=>"Testing"}
- def get_status(user_id)
+ def get_status(user_id, params = {})
user_id = user_id.to_s
validate_identifier(:user_id, user_id)
- url = sprintf(API_STATUS_URL, user_id)
- call_myspace_api_json(url)
+ call_myspace_api(:status_get, params.dup.update(:user_id => user_id, :v1_json => true))
end
+ # Sets the status of the user +user_id+
+ def set_status(user_id, status)
+ user_id = user_id.to_s
+ validate_identifier(:user_id, user_id)
+ call_myspace_api(:status_put, :user_id => user_id, :body => {:status => status})
+ end
+
# Gets the video descriptions for the videos of user +user_id+:
#
# {"videos"=>
# [{"totalrating"=>"0",
# "title"=>"110403na",
@@ -440,15 +451,14 @@
# "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"}}
- def get_videos(user_id)
+ def get_videos(user_id, params = {})
user_id = user_id.to_s
validate_identifier(:user_id, user_id)
- url = sprintf(API_VIDEOS_URL, user_id)
- call_myspace_api_json(url)
+ call_myspace_api(:videos, params.dup.update(:user_id => user_id, :v1_json => true))
end
# Gets the video description for the video +video_id+ of user +user_id+:
#
# {"totalrating"=>"0",
@@ -478,68 +488,159 @@
# "userId"=>456073223,
# "image"=>
# "http://c1.ac-images.myspacecdn.com/images02/45/s_f820313641924f0f90004932c8bc310c.jpg"},
# "totalvotes"=>"0",
# "videoUri"=>"http://api.myspace.com/v1/users/456073223/videos/53551799"}
- def get_video(user_id, video_id)
+ def get_video(user_id, video_id, params = {})
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)
+ call_myspace_api(:video, params.dup.update(:user_id => user_id, :video_id => video_id, :v1_json => true))
end
- def get_activities(user_id)
+ def get_activities(user_id, params = {})
user_id = user_id.to_s
validate_identifier(:user_id, user_id)
- url = sprintf(API_ACTIVITIES_URL, user_id)
- call_myspace_api_xml(url)
+ call_myspace_api(:activities, params.dup.update(:user_id => user_id))
end
- def get_friends_activities(user_id)
+ def get_friends_activities(user_id, params = {})
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)
+ call_myspace_api(:friends_activities, params.dup.update(:user_id => user_id))
end
- def set_status(user_id, status)
+ def get_global_appdata(*keys)
+ 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
+ end
+
+ def clear_global_appdata(*keys)
+ call_myspace_api(:appdata_global_delete, :keys => keys.join(';'))
+ end
+
+ def get_user_appdata(user_id, *keys)
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}})
+ 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
+ end
end
- def call_myspace_api(url, method = :get, params = {}, &block)
+ def set_user_appdata(user_id, params = {})
+ user_id = user_id.to_s
+ validate_identifier(:user_id, user_id)
+ deletes = MySpace.remove_null_values(params)
+ call_myspace_api(:appdata_user_put, :user_id => user_id, :body => params) if params.length > 0
+ call_myspace_api(:appdata_user_delete, :user_id => user_id, :keys => deletes.join(';')) if deletes.length > 0
+ end
+
+ def clear_user_appdata(user_id, *keys)
+ user_id = user_id.to_s
+ validate_identifier(:user_id, user_id)
+ call_myspace_api(:appdata_user_delete, :user_id => user_id, :keys => keys.join(';'))
+ 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)
+ end
+ end
+ end
+
+ def appdata_to_hash(&block)
+ appdata = yield
+ return {} unless appdata['keyvaluecollection']
+ appdata['keyvaluecollection'].inject({}) do |hash, entry|
+ hash.update(entry['key'].to_sym => entry['value'])
+ end
+ end
+
+
+ def call_myspace_api(name, params = {}, &block)
params = params.dup
+ ep = EndPoint.find(name)
+ url = ep.compute_path(params)
+ timeout = params.delete(:timeout) || TIMEOUT_SECS
body = params.delete(:body)
headers = params.delete(:headers)
+ params.delete(:v1_json)
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
+ resp = nil
@http_logger.info("sending: '#{url}'") if @http_logger
- resp = @access_token.request(method, url, body, headers)
+ begin
+ Timeout::timeout(timeout, TimeoutException) do
+ resp = @access_token.request(ep.method, url, body, headers)
+ end
+ rescue TimeoutException => e
+ e.timeout = timeout
+ e.url = url
+ raise e
+ end
@http_logger.info("received: '#{resp.code}': '#{resp.body}'") if @http_logger
validate_response(resp, url)
- yield(resp.body)
- end
- def call_myspace_api_json(url, method = :get, params = {})
- call_myspace_api(url, method, params) do |body|
- JSON::parse(body)
+ content_type = resp['content-type']
+ if content_type
+ if content_type =~ /json/
+ return JSON::parse(resp.body)
+ elsif content_type =~ /xml/
+ return REXML::Document.new(resp.body)
+ end
+
+ raise "unknown content type: #{content_type}"
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
+ # 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')