Class: MySpace::MySpace

Inherits:
Object
  • Object
show all
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'
TIMEOUT_SECS =

tests regularly timeout at 2 seconds

3
ID_REGEXP =
/[0-9]+/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/myspace/myspace.rb', line 71

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_tokenObject

Returns the value of attribute access_token.



48
49
50
# File 'lib/myspace/myspace.rb', line 48

def access_token
  @access_token
end

#consumerObject (readonly)

Returns the value of attribute consumer.



45
46
47
# File 'lib/myspace/myspace.rb', line 45

def consumer
  @consumer
end

#http_loggerObject

Returns the value of attribute http_logger.



46
47
48
# File 'lib/myspace/myspace.rb', line 46

def http_logger
  @http_logger
end

#request_tokenObject

Returns the value of attribute request_token.



47
48
49
# File 'lib/myspace/myspace.rb', line 47

def request_token
  @request_token
end

Class Method Details

.remove_null_values(hash) ⇒ Object



523
524
525
526
527
528
529
530
531
# File 'lib/myspace/myspace.rb', line 523

def self.remove_null_values(hash)
  hash.keys.inject([]) do |nulls, key|
    unless hash[key]
      hash.delete(key)
      nulls << key
    end
    nulls
  end
end

Instance Method Details

#appdata_to_hash(&block) ⇒ Object



582
583
584
585
586
587
588
# File 'lib/myspace/myspace.rb', line 582

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

#call_myspace_api(name, params = {}, &block) ⇒ Object



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'lib/myspace/myspace.rb', line 591

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
  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)

  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

#clear_global_appdata(*keys) ⇒ Object



540
541
542
# File 'lib/myspace/myspace.rb', line 540

def clear_global_appdata(*keys)
  call_myspace_api(:appdata_global_delete, :keys => keys.join(';'))
end

#clear_user_appdata(user_id, *keys) ⇒ Object



564
565
566
567
568
# File 'lib/myspace/myspace.rb', line 564

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

#get_access_token(request_token = @request_token) ⇒ Object

Get an access token once the user has authorized us.



110
111
112
113
114
115
116
117
118
119
# File 'lib/myspace/myspace.rb', line 110

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, params = {}) ⇒ Object



501
502
503
504
505
# File 'lib/myspace/myspace.rb', line 501

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

#get_album(user_id, album_id, params = {}) ⇒ 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”=> “”}



202
203
204
205
206
207
208
# File 'lib/myspace/myspace.rb', line 202

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)
  call_myspace_api(:album, params.dup.update(:user_id => user_id, :album_id => album_id, :v1_json => true))
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”=> “”}



161
162
163
164
165
# File 'lib/myspace/myspace.rb', line 161

def get_albums(user_id, params = {})
  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_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.



105
106
107
# File 'lib/myspace/myspace.rb', line 105

def get_authorization_url(request_token, callback_url)
  "#{request_token.authorize_url}&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”=> “”}



232
233
234
235
236
# File 'lib/myspace/myspace.rb', line 232

def get_friends(user_id, params = {})
  user_id = user_id.to_s
  validate_identifier(:user_id, user_id)
  call_myspace_api(:friends, params.dup.update(:user_id => user_id, :v1_json => true))
end

#get_friends_activities(user_id, params = {}) ⇒ Object



507
508
509
510
511
# File 'lib/myspace/myspace.rb', line 507

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

#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”=> “”}



254
255
256
257
258
259
260
261
262
# File 'lib/myspace/myspace.rb', line 254

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
  call_myspace_api(:friendship, :user_id => user_id, :friend_ids => friend_ids.join(';'), :v1_json => true)
end

#get_global_appdata(*keys) ⇒ Object



513
514
515
516
517
518
519
520
521
# File 'lib/myspace/myspace.rb', line 513

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

#get_mood(user_id, params = {}) ⇒ 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”=> “”}



280
281
282
283
284
# File 'lib/myspace/myspace.rb', line 280

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))
end

#get_photo(user_id, photo_id, params = {}) ⇒ 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”=> “”}



346
347
348
349
350
351
352
# File 'lib/myspace/myspace.rb', line 346

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)
  call_myspace_api(:photo, params.dup.update(:user_id => user_id, :photo_id => photo_id, :v1_json => true))
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”=> “”}



319
320
321
322
323
# File 'lib/myspace/myspace.rb', line 319

def get_photos(user_id, params = {})
  user_id = user_id.to_s
  validate_identifier(:user_id, user_id)
  call_myspace_api(:photos, params.dup.update(:user_id => user_id, :v1_json => true))
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”}



377
378
379
380
381
# File 'lib/myspace/myspace.rb', line 377

def get_profile(user_id, params = {})
  user_id = user_id.to_s
  validate_identifier(:user_id, user_id)
  call_myspace_api(:profile, params.dup.update(:user_id => user_id, :v1_json => true))
end

#get_request_tokenObject

Get an unauthorized request token from MySpace.



98
99
100
# File 'lib/myspace/myspace.rb', line 98

def get_request_token
  @consumer.get_request_token
end

#get_status(user_id, params = {}) ⇒ 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”}



400
401
402
403
404
# File 'lib/myspace/myspace.rb', line 400

def get_status(user_id, params = {})
  user_id = user_id.to_s
  validate_identifier(:user_id, user_id)
  call_myspace_api(:status_get, params.dup.update(:user_id => user_id, :v1_json => true))
end

#get_user_appdata(user_id, *keys) ⇒ Object



544
545
546
547
548
549
550
551
552
553
554
# File 'lib/myspace/myspace.rb', line 544

def get_user_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_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

#get_user_friends_appdata(user_id, *keys) ⇒ Object



570
571
572
573
574
575
576
577
578
579
580
# File 'lib/myspace/myspace.rb', line 570

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

#get_useridObject

Get the user id of the currently logged in user.



122
123
124
125
# File 'lib/myspace/myspace.rb', line 122

def get_userid()
   = call_myspace_api(:user_info, :v1_json => true)
  ['userId'].to_s
end

#get_video(user_id, video_id, params = {}) ⇒ 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”}



493
494
495
496
497
498
499
# File 'lib/myspace/myspace.rb', line 493

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)
  call_myspace_api(:video, params.dup.update(:user_id => user_id, :video_id => video_id, :v1_json => true))
end

#get_videos(user_id, params = {}) ⇒ 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”=> “”}



456
457
458
459
460
# File 'lib/myspace/myspace.rb', line 456

def get_videos(user_id, params = {})
  user_id = user_id.to_s
  validate_identifier(:user_id, user_id)
  call_myspace_api(:videos, params.dup.update(:user_id => user_id, :v1_json => true))
end

#set_global_appdata(params = {}) ⇒ Object



533
534
535
536
537
538
# File 'lib/myspace/myspace.rb', line 533

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

#set_status(user_id, status) ⇒ Object

Sets the status of the user user_id



407
408
409
410
411
# File 'lib/myspace/myspace.rb', line 407

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

#set_user_appdata(user_id, params = {}) ⇒ Object



556
557
558
559
560
561
562
# File 'lib/myspace/myspace.rb', line 556

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