myspace-ruby 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. data/README +284 -0
  2. data/doc/classes/MySpace.html +666 -0
  3. data/doc/classes/MySpace/Album.html +225 -0
  4. data/doc/classes/MySpace/Connection.html +427 -0
  5. data/doc/classes/MySpace/Detail.html +191 -0
  6. data/doc/classes/MySpace/FriendStatus.html +131 -0
  7. data/doc/classes/MySpace/Friends.html +141 -0
  8. data/doc/classes/MySpace/Friendship.html +205 -0
  9. data/doc/classes/MySpace/Group.html +301 -0
  10. data/doc/classes/MySpace/Groups.html +146 -0
  11. data/doc/classes/MySpace/Interest.html +156 -0
  12. data/doc/classes/MySpace/InvalidClassDefinition.html +111 -0
  13. data/doc/classes/MySpace/InvalidCredentials.html +111 -0
  14. data/doc/classes/MySpace/InvalidDataFormat.html +111 -0
  15. data/doc/classes/MySpace/InvalidObjectMap.html +111 -0
  16. data/doc/classes/MySpace/InvalidRequest.html +111 -0
  17. data/doc/classes/MySpace/InvalidRequestParams.html +111 -0
  18. data/doc/classes/MySpace/InvalidResponse.html +111 -0
  19. data/doc/classes/MySpace/Mood.html +131 -0
  20. data/doc/classes/MySpace/Object.html +348 -0
  21. data/doc/classes/MySpace/Photo.html +146 -0
  22. data/doc/classes/MySpace/Photos.html +136 -0
  23. data/doc/classes/MySpace/Profile.html +176 -0
  24. data/doc/classes/MySpace/Status.html +131 -0
  25. data/doc/classes/MySpace/UnknownDataType.html +111 -0
  26. data/doc/classes/MySpace/User.html +162 -0
  27. data/doc/classes/MySpace/Video.html +256 -0
  28. data/doc/classes/MySpace/Videos.html +126 -0
  29. data/doc/classes/MySpaceAPITest.html +916 -0
  30. data/doc/created.rid +1 -0
  31. data/doc/files/lib/myspace_rb.html +185 -0
  32. data/doc/files/tests/test_rb.html +109 -0
  33. data/doc/fr_class_index.html +28 -0
  34. data/doc/fr_file_index.html +29 -0
  35. data/doc/fr_method_index.html +54 -0
  36. data/doc/index.html +24 -0
  37. data/doc/rdoc-style.css +208 -0
  38. data/lib/myspace.rb +45 -0
  39. data/lib/myspace/classes/album.rb +25 -0
  40. data/lib/myspace/classes/detail.rb +22 -0
  41. data/lib/myspace/classes/friends.rb +16 -0
  42. data/lib/myspace/classes/friendship.rb +27 -0
  43. data/lib/myspace/classes/group.rb +55 -0
  44. data/lib/myspace/classes/interest.rb +16 -0
  45. data/lib/myspace/classes/mood.rb +9 -0
  46. data/lib/myspace/classes/photo.rb +21 -0
  47. data/lib/myspace/classes/profile.rb +22 -0
  48. data/lib/myspace/classes/status.rb +9 -0
  49. data/lib/myspace/classes/user.rb +17 -0
  50. data/lib/myspace/classes/video.rb +41 -0
  51. data/lib/myspace/connection.rb +110 -0
  52. data/lib/myspace/error.rb +12 -0
  53. data/lib/myspace/loader.rb +74 -0
  54. data/lib/myspace/object.rb +116 -0
  55. data/tests/test.rb +148 -0
  56. metadata +108 -0
@@ -0,0 +1,12 @@
1
+ module MySpace
2
+ class InvalidDataFormat < Exception; end
3
+ class InvalidResponse < Exception; end
4
+ class InvalidCredentials < Exception; end
5
+ class InvalidClassDefinition < Exception; end
6
+ class InvalidObjectMap < Exception; end
7
+ class UnknownDataType < Exception; end
8
+ class InvalidRequest < Exception; end
9
+ class InvalidRequestParams < Exception; end
10
+ end
11
+
12
+ fail "\nThis is a library, not a command line app" if $0 == __FILE__
@@ -0,0 +1,74 @@
1
+ # MySpace libs
2
+ %w( connection object
3
+ album detail friends friendship group
4
+ interest mood photo profile status user video
5
+ ).each(&method(:require))
6
+ # Base libraries
7
+ %w( pp net/http cgi ).each(&method(:require))
8
+
9
+ require "rubygems"
10
+
11
+ # External libs
12
+ REQUIRED = {
13
+ "oauth" => "http://oauth.googlecode.com/svn/code/ruby/",
14
+ # http://rubyforge.org/frs/download.php/22501/json-1.1.1-mswin32.gem
15
+ "json/pure" => "http://json.rubyforge.org/",
16
+ }
17
+
18
+ # Hard exit and describe what libraries failed
19
+ def remote_install_failed(load_failures)
20
+ puts "Installation failed, try to install the following modules manually:\n"
21
+ load_failures.each {|lib| puts "'#{lib}' ('#{REQUIRED[lib]}')" }
22
+ exit(1)
23
+ end
24
+
25
+ # Try to remotely install libs based on our load failures
26
+ def remote_install(load_failures)
27
+ puts "
28
+ We couldn't load all modules required for the MySpace API. You appear to need:"
29
+ puts
30
+ load_failures.each {|lib| puts "'#{lib.gsub(/\/.*$/,'')}' (#{REQUIRED[lib]})" }
31
+ print "
32
+ If you like, we can try to fetch what you need from the Internet, and install it.
33
+
34
+ Do you want to try and install from the net? [Y/n]: "
35
+ answer = $stdin.getc.chr
36
+ if answer.downcase == "y" || answer.chomp.empty?
37
+ begin
38
+ require 'rubygems/remote_installer'
39
+ installer = Gem::RemoteInstaller.new
40
+ installed_gems = []
41
+ load_failures.each {|lib|
42
+ lib.gsub!(/\/.*$/,'')
43
+ installed_gems << installer.install(lib,">0",false,$LOAD_PATH[0])
44
+ }
45
+
46
+ if installed_gems.compact.length == load_failures.compact.length
47
+ installed_gems.compact.each {|spec| puts "installed #{spec.full_name}" }
48
+ else
49
+ remote_install_failed(load_failures)
50
+ end
51
+ rescue Exception => e
52
+ remote_install_failed(load_failures)
53
+ end
54
+ else
55
+ puts "
56
+ Ok, but you need to get the required libraries before you can continue.
57
+
58
+ Go to http://developer.myspace.com for more information or updates.
59
+ "
60
+ end
61
+ end
62
+
63
+ # The actual load
64
+ load_failures = []
65
+ REQUIRED.keys.each {|lib|
66
+ begin
67
+ require lib
68
+ rescue LoadError
69
+ load_failures << lib
70
+ end
71
+ }
72
+ remote_install(load_failures) if !load_failures.empty?
73
+
74
+ fail "This is a library, not a command line app\n" if $0 == __FILE__
@@ -0,0 +1,116 @@
1
+ # Base class for all the MySpace objects
2
+ module MySpace
3
+ class Object
4
+ # (String) the REST path on MySpace::API_HOST (printf format)
5
+ @pathFormat = nil
6
+ # (Integer) the number of parameters needed for @pathFormat
7
+ @numParams = 1
8
+ # (Hash) optional parameters for the request (passed as GET query string)
9
+ @optParams = nil
10
+ # (String) The type of data we expect from the API servers
11
+ @dataType = "Hash"
12
+ # (String) The type of the child objects if we're dealing with collections
13
+ @childType = nil
14
+ # (String) The instance variable that holds the collection
15
+ @childBase = nil
16
+ # (String) the format we want from the api servers (MySpace::API_FORMATS)
17
+ @format = "json"
18
+
19
+ def initialize(attribs={})
20
+ attribs.each {|key,val| instance_variable_set("@#{key}",val) }
21
+ end
22
+
23
+ class << self
24
+ # Attempts to convert JSON to MySpace objects
25
+ def json_to_obj(body)
26
+ begin
27
+ obj = self.new(JSON.parse(body))
28
+ obj.user = MySpace::User.new(obj.user) if obj.respond_to?("user")
29
+ rescue Exception => e
30
+ raise InvalidResponse, "could not parse json from #{body}: '#{e}'"
31
+ end
32
+ obj
33
+ end
34
+
35
+ # Attempts to convert JSON to a MySpace objects (with child collections)
36
+ def json_to_obj_collection(body)
37
+ begin
38
+ obj = json_to_obj(body)
39
+
40
+ col = obj.instance_variable_get "@#{@childBase}"
41
+ raise InvalidResponse, "No '#{@childBase}' in response" if !col
42
+
43
+ col = obj.instance_variable_set "@#{@childBase}",
44
+ col.collect {|o| eval("#{@childType}.new(o)") }
45
+
46
+ rescue Exception => e
47
+ raise InvalidResponse, "could not parse json from #{body}: '#{e}'"
48
+ end
49
+ obj
50
+ end
51
+
52
+ # Builds a REST request based on the supplied params and optParams
53
+ def build_request(params,optParams=nil,format="json")
54
+ if params == Array && params.length != @numParams
55
+ raise InvalidRequestParams.new(
56
+ "#{self}.get needs #{@numParams} (#{@pathFormat})")
57
+ end
58
+
59
+ raise InvalidRequest.new(
60
+ "try a collection of #{self} objects") if @pathFormat.nil?
61
+
62
+ begin
63
+ path = "/#{API_VERSION}#{@pathFormat % params}.#{format}"
64
+ rescue Exception => e
65
+ raise InvalidRequestParams.new(
66
+ "couldn't build a path from params '#{params.join("'")}' \
67
+ and format string #{@pathFormat}. Exception is: (#{e})")
68
+ end
69
+
70
+ if optParams
71
+ raise InvalidRequestParams.new(
72
+ "optParams should be a Hash.") if !optParams.kind_of?(Hash)
73
+
74
+ path += "?"
75
+
76
+ optParams.each {|k,v|
77
+ if @optParams && !@optParams.has_key?(k)
78
+ raise InvalidRequestParams.new("option '#{k}' is not valid.")
79
+ end
80
+ path += "#{k}=#{v}&"
81
+ }
82
+ path.sub!(/&$/,"")
83
+ end
84
+ path
85
+ end
86
+
87
+ # Performs a request to the API servers and returns the raw JSON/XML
88
+ def get_raw(params,optParams=nil,format="json")
89
+ path = self.build_request(params,optParams,format)
90
+ MySpace.connection.get_body(path)
91
+ end
92
+
93
+ # Performs a get request to the API servers and returns some MySpace object
94
+ def get(params,optParams=nil)
95
+
96
+ path = self.build_request(params,optParams)
97
+ #puts "#{path} pathFormat #{@pathFormat} childType #{@childType}
98
+ #childBase #{@childBase} numParams #{@numParams}"
99
+
100
+ body = MySpace.connection.get_body(path)
101
+
102
+ if @dataType == "Hash"
103
+ self.json_to_obj(body)
104
+ elsif @dataType == "Array"
105
+ body.sub!(/\{"Groups"\:/,'{"groups":') # you did not see this
106
+ self.json_to_obj_collection(body)
107
+ else
108
+ raise InvalidRequestParams.new("#{@dataType} is not a valid data type.")
109
+ end
110
+ end
111
+ end # end static class methods
112
+ end # end class Object
113
+ end # end module MySpace
114
+
115
+ fail "This is a library, not a command line app\n" if $0 == __FILE__
116
+
data/tests/test.rb ADDED
@@ -0,0 +1,148 @@
1
+ require "test/unit"
2
+
3
+ class MySpaceAPITest < Test::Unit::TestCase
4
+ # set these to something that will work for you
5
+ # (until we have a proper test account)
6
+ @@userId = 0
7
+ @@friendId = 0
8
+ @@friendIds = "0;1"
9
+ @@albumId = 0
10
+
11
+ @@classes = %w[ album detail friends friendship group
12
+ interest mood photo profile status user video ]
13
+
14
+ def setup
15
+ $:.unshift File.dirname(__FILE__) + "/../lib"
16
+ require "myspace"
17
+ @@classes.each(&method(:require))
18
+ end
19
+
20
+ # basic tests to make sure we actually get the response we expect
21
+ def test_album
22
+ a = MySpace::Album.get(@@userId,@@albumId)
23
+ assert_kind_of MySpace::Album, a
24
+ assert_match /http\:/, a.albumUri
25
+ assert_match /http\:/, a.defaultImage
26
+ assert_kind_of Fixnum, a.id
27
+ assert_kind_of String, a.location
28
+ assert_kind_of Fixnum, a.photoCount
29
+ assert_match /http\:/, a.photosUri
30
+ assert_kind_of String, a.privacy
31
+ assert_kind_of String, a.title
32
+ assert_kind_of MySpace::User, a.user
33
+ end
34
+
35
+ def test_detail
36
+ d = MySpace::Detail.get(@@userId)
37
+ assert_kind_of MySpace::Detail, d
38
+ assert_kind_of MySpace::User,d.user
39
+ assert_kind_of String,d.status
40
+ assert_kind_of String,d.ethnicity
41
+ assert_kind_of String,d.drink
42
+ assert_kind_of String,d.zodiacsign
43
+ assert_kind_of String,d.orientation
44
+ assert_kind_of String,d.religion
45
+ assert_kind_of String,d.herefor
46
+ assert_kind_of String,d.smoke
47
+ assert_kind_of String,d.education
48
+ assert_kind_of String,d.income
49
+ assert_kind_of String,d.children
50
+ assert_kind_of String,d.hometown
51
+ assert_kind_of String,d.bodyType
52
+ end
53
+
54
+ def test_friends
55
+ f = MySpace::Friends.get(@@userId)
56
+ assert_kind_of MySpace::Friends, f
57
+ assert_kind_of Fixnum, f.count
58
+ assert_kind_of Array, f.friends
59
+ assert_kind_of MySpace::User, f.friends.first
60
+ end
61
+
62
+ def test_friendship
63
+ f = MySpace::Friendship.get(@@userId,@@friendId)
64
+ assert_kind_of MySpace::Friendship, f
65
+ assert_kind_of MySpace::User,f.user
66
+ assert_kind_of Array,f.friendship
67
+ assert(f.friendship.first.areFriends.kind_of?(TrueClass) ||
68
+ f.friendship.first.areFriends.kind_of?(FalseClass))
69
+ assert_kind_of Fixnum,f.friendship.first.friendId
70
+ end
71
+
72
+ def test_group
73
+ assert_kind_of MySpace::Groups, MySpace::Groups.get(@@userId)
74
+ end
75
+
76
+ def test_interest
77
+ i = MySpace::Interest.get(@@userId)
78
+ assert_kind_of MySpace::Interest, i
79
+ assert_kind_of MySpace::User,i.user
80
+ assert_kind_of String, i.books
81
+ assert_kind_of String, i.general
82
+ assert_kind_of String, i.heroes
83
+ assert_kind_of String, i.movies
84
+ assert_kind_of String, i.music
85
+ assert_kind_of String, i.television
86
+ end
87
+
88
+ def test_mood
89
+ m = MySpace::Mood.get(@@userId)
90
+ assert_kind_of MySpace::Mood, m
91
+ assert_kind_of String, m.mood
92
+ assert_kind_of MySpace::User, m.user
93
+ end
94
+
95
+ def test_photo
96
+ p = MySpace::Photos.get(@@userId)
97
+ assert_kind_of MySpace::Photos, p
98
+ assert_kind_of Fixnum, p.count
99
+ assert_kind_of Array, p.photos
100
+ assert_kind_of MySpace::User, p.user
101
+ assert_kind_of MySpace::Photo, p.photos.first
102
+ assert_kind_of String, p.photos.first.caption
103
+ assert_kind_of Fixnum, p.photos.first.id
104
+ assert_match /^http\:/, p.photos.first.imageUri
105
+ assert_match /^http\:/, p.photos.first.photoUri
106
+ end
107
+
108
+ def test_profile
109
+ p = MySpace::Profile.get(@@userId)
110
+ assert_kind_of MySpace::Profile, p
111
+ assert_kind_of String, p.aboutme
112
+ assert_kind_of Fixnum, p.age
113
+ assert_kind_of String, p.city
114
+ assert_kind_of String, p.country
115
+ assert_kind_of String, p.culture
116
+ assert_kind_of String, p.gender
117
+ assert_kind_of String, p.hometown
118
+ assert_kind_of String, p.maritalstatus
119
+ assert_kind_of String, p.postalcode
120
+ assert_kind_of String, p.region
121
+ end
122
+
123
+ def test_status
124
+ s = MySpace::Status.get(@@userId)
125
+ assert_kind_of MySpace::Status, s
126
+ assert_kind_of String, s.status
127
+ end
128
+
129
+ def test_user
130
+ assert_kind_of MySpace::User, MySpace::User.get(@@userId)
131
+ u = MySpace::User.get(@@userId)
132
+ assert_match /http\:/,u.image
133
+ assert_match /http\:/,u.webUri
134
+ assert_match /^\d+$/,u.userId.to_s
135
+ assert_kind_of Fixnum,u.userId
136
+ assert_kind_of String,u.name
137
+ assert(u.onlineNow.kind_of?(TrueClass) ||
138
+ u.onlineNow.kind_of?(FalseClass))
139
+
140
+ end
141
+
142
+ def test_video
143
+ assert_kind_of MySpace::Videos, MySpace::Videos.get(@@userId)
144
+ # TODO
145
+ end
146
+
147
+ end
148
+
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: myspace-ruby
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.7.0
7
+ date: 2008-03-06 00:00:00 -08:00
8
+ summary: A library to interface with the MySpace REST API.
9
+ require_paths:
10
+ - lib
11
+ email: cbell|myspace.com
12
+ homepage: myspace-ruby.rubyforge.org/myspace-ruby
13
+ rubyforge_project: myspace-ruby
14
+ description:
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Chris Bell
31
+ files:
32
+ - tests/test.rb
33
+ - doc/index.html
34
+ - doc/fr_file_index.html
35
+ - doc/classes
36
+ - doc/fr_method_index.html
37
+ - doc/created.rid
38
+ - doc/rdoc-style.css
39
+ - doc/fr_class_index.html
40
+ - doc/files
41
+ - doc/classes/MySpace.html
42
+ - doc/classes/MySpace
43
+ - doc/classes/MySpaceAPITest.html
44
+ - doc/files/lib
45
+ - doc/files/tests
46
+ - doc/files/pkg
47
+ - doc/classes/MySpace/InvalidCredentials.html
48
+ - doc/classes/MySpace/User.html
49
+ - doc/classes/MySpace/Groups.html
50
+ - doc/classes/MySpace/Mood.html
51
+ - doc/classes/MySpace/Group.html
52
+ - doc/classes/MySpace/Detail.html
53
+ - doc/classes/MySpace/InvalidRequest.html
54
+ - doc/classes/MySpace/InvalidRequestParams.html
55
+ - doc/classes/MySpace/InvalidDataFormat.html
56
+ - doc/classes/MySpace/Photos.html
57
+ - doc/classes/MySpace/Status.html
58
+ - doc/classes/MySpace/InvalidObjectMap.html
59
+ - doc/classes/MySpace/Video.html
60
+ - doc/classes/MySpace/Friendship.html
61
+ - doc/classes/MySpace/Object.html
62
+ - doc/classes/MySpace/Friends.html
63
+ - doc/classes/MySpace/UnknownDataType.html
64
+ - doc/classes/MySpace/Photo.html
65
+ - doc/classes/MySpace/InvalidResponse.html
66
+ - doc/classes/MySpace/Connection.html
67
+ - doc/classes/MySpace/InvalidClassDefinition.html
68
+ - doc/classes/MySpace/Profile.html
69
+ - doc/classes/MySpace/Interest.html
70
+ - doc/classes/MySpace/Videos.html
71
+ - doc/classes/MySpace/Album.html
72
+ - doc/classes/MySpace/FriendStatus.html
73
+ - doc/files/lib/myspace_rb.html
74
+ - doc/files/lib/myspace
75
+ - doc/files/tests/test_rb.html
76
+ - doc/files/pkg/myspace-ruby-0_7_0
77
+ - lib/myspace.rb
78
+ - lib/myspace/object.rb
79
+ - lib/myspace/connection.rb
80
+ - lib/myspace/loader.rb
81
+ - lib/myspace/error.rb
82
+ - lib/myspace/classes/video.rb
83
+ - lib/myspace/classes/album.rb
84
+ - lib/myspace/classes/profile.rb
85
+ - lib/myspace/classes/mood.rb
86
+ - lib/myspace/classes/interest.rb
87
+ - lib/myspace/classes/group.rb
88
+ - lib/myspace/classes/detail.rb
89
+ - lib/myspace/classes/friendship.rb
90
+ - lib/myspace/classes/user.rb
91
+ - lib/myspace/classes/status.rb
92
+ - lib/myspace/classes/friends.rb
93
+ - lib/myspace/classes/photo.rb
94
+ - README
95
+ test_files:
96
+ - tests/test.rb
97
+ rdoc_options: []
98
+
99
+ extra_rdoc_files:
100
+ - README
101
+ executables: []
102
+
103
+ extensions: []
104
+
105
+ requirements: []
106
+
107
+ dependencies: []
108
+