gdata 0.0.1

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.
@@ -0,0 +1,75 @@
1
+ # Copyright (C) 2008 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ module GData
17
+ module HTTP
18
+
19
+ # Very simple class to hold everything about an HTTP request.
20
+ class Request
21
+
22
+ DEFAULT_OPTIONS = {
23
+ :body => '',
24
+ :method => :get,
25
+ :headers => {} }
26
+
27
+ # The URL of the request.
28
+ attr_accessor :url
29
+ # The body of the request.
30
+ attr_accessor :body
31
+ # The HTTP method being used in the request.
32
+ attr_accessor :method
33
+ # The HTTP headers of the request.
34
+ attr_accessor :headers
35
+
36
+ # Only the URL itself is required, everything else is optional.
37
+ def initialize(url, options = {})
38
+ @url = url
39
+ options = DEFAULT_OPTIONS.merge(options)
40
+ options.each do |key, value|
41
+ self.send("#{key}=", value)
42
+ end
43
+ end
44
+
45
+ # Returns whether or not a request is chunked.
46
+ def chunked?
47
+ if @headers['Transfer-Encoding'] == 'chunked'
48
+ return true
49
+ else
50
+ return false
51
+ end
52
+ end
53
+
54
+ # Sets if the request is using chunked transfer-encoding.
55
+ def chunked=(enabled)
56
+ if enabled
57
+ @headers['Transfer-Encoding'] = 'chunked'
58
+ else
59
+ @headers.delete('Transfer-Encoding')
60
+ end
61
+ end
62
+
63
+ # Calculates and sets the length of the body.
64
+ def calculate_length!
65
+ if not @headers['Content-Length'] and not chunked?
66
+ if @body
67
+ @headers['Content-Length'] = @body.length
68
+ else
69
+ @headers['Content-Length'] = 0
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,29 @@
1
+ # Copyright (C) 2008 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module GData
16
+ module HTTP
17
+
18
+ # An extremely simple class to hold the values of an HTTP response.
19
+ class Response
20
+
21
+ # The HTTP response code.
22
+ attr_accessor :status_code
23
+ # The body of the HTTP response.
24
+ attr_accessor :body
25
+ # The headers of the HTTP response.
26
+ attr_accessor :headers
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,53 @@
1
+ # Copyright (C) 2008 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ $:.unshift(File.dirname(__FILE__))
16
+ require 'test_helper'
17
+
18
+ class TC_GData_Auth_AuthSub < Test::Unit::TestCase
19
+
20
+ include TestHelper
21
+
22
+ def test_make_authenticated_request
23
+ token = self.get_authsub_token()
24
+ key = self.get_authsub_private_key()
25
+ service = GData::Client::YouTube.new
26
+ if token
27
+
28
+ service.authsub_token = token
29
+ if key
30
+ service.authsub_private_key = key
31
+ end
32
+
33
+ feed = service.get('http://gdata.youtube.com/feeds/api/users/default/uploads?max-results=1')
34
+ self.assert_not_nil(feed, 'Feed should not be nil')
35
+ end
36
+ end
37
+
38
+ def test_generate_url
39
+ scope = 'http://gdata.youtube.com'
40
+ next_url = 'http://example.com'
41
+ secure = true
42
+ session = false
43
+ url = GData::Auth::AuthSub.get_url(next_url, scope, secure, session)
44
+ self.assert_equal('https://www.google.com/accounts/AuthSubRequest?next=http%3A%2F%2Fexample.com&scope=http%3A%2F%2Fgdata.youtube.com&session=0&secure=1', url)
45
+
46
+ # test generating with a pre-populated scope
47
+ yt = GData::Client::YouTube.new
48
+ client_url = yt.authsub_url(next_url, secure, session)
49
+ self.assert_equal(url, client_url)
50
+
51
+ end
52
+
53
+ end
@@ -0,0 +1,38 @@
1
+ # Copyright (C) 2008 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ $:.unshift(File.dirname(__FILE__))
16
+ require 'test_helper'
17
+
18
+ class TC_GData_Auth_ClientLogin < Test::Unit::TestCase
19
+
20
+ include TestHelper
21
+
22
+ def test_has_config
23
+ self.assert_not_nil(self.get_username(),
24
+ 'Must have a username in test_config.yml')
25
+ self.assert_not_nil(self.get_password(),
26
+ 'Must have a password in test_config.yml')
27
+ end
28
+
29
+ def test_get_token
30
+ client_login = GData::Auth::ClientLogin.new('cl')
31
+ source = 'GDataUnitTest'
32
+ assert_nothing_raised do
33
+ token = client_login.get_token(self.get_username(), self.get_password(), source)
34
+ end
35
+ end
36
+
37
+
38
+ end
@@ -0,0 +1,29 @@
1
+ # Copyright (C) 2008 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ $:.unshift(File.dirname(__FILE__))
16
+ require 'test_helper'
17
+
18
+ class TC_GData_Client_Base < Test::Unit::TestCase
19
+
20
+ include TestHelper
21
+
22
+ def test_simple_gdata_get
23
+ service = GData::Client::Base.new
24
+ feed = service.get('http://gdata.youtube.com/feeds/base/videos?max-results=1')
25
+ assert_not_nil(feed, 'feed can not be nil')
26
+ end
27
+
28
+ end
29
+
@@ -0,0 +1,76 @@
1
+ # Copyright (C) 2008 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ $:.unshift(File.dirname(__FILE__))
16
+ require 'test_helper'
17
+
18
+ class TC_GData_Client_YouTube < Test::Unit::TestCase
19
+
20
+ include TestHelper
21
+
22
+ def setup
23
+ @yt = GData::Client::YouTube.new
24
+ @yt.clientlogin(self.get_username(), self.get_password())
25
+ @yt.client_id = 'ytapi-Google-GDataUnitTests-lcqr3u89-1'
26
+ @yt.developer_key = 'AI39si4vwXwDLR5MrtsdR1ULUD8__EnEccla-0bnqV40KpeFDIyCwEv0VJqZKHUsO3MvVM_bXHp3cAr55HmMYMhqfxzLMUgDXA'
27
+ end
28
+
29
+ def test_authenticated_uploads_feed
30
+
31
+
32
+ feed = @yt.get('http://gdata.youtube.com/feeds/api/users/default/uploads?max-results=1')
33
+ self.assert_not_nil(feed, 'feed can not be nil')
34
+ end
35
+
36
+ def test_favorites
37
+
38
+ video_id = 'zlfKdbWwruY'
39
+
40
+ entry = <<-EOF
41
+ <entry xmlns="http://www.w3.org/2005/Atom">
42
+ <id>#{video_id}</id>
43
+ </entry>
44
+ EOF
45
+
46
+ response = @yt.post('http://gdata.youtube.com/feeds/api/users/default/favorites', entry)
47
+
48
+ edit_uri = response.elements["link[@rel='edit']"].attributes['href']
49
+
50
+ @yt.delete(edit_uri)
51
+
52
+ end
53
+
54
+ def test_playlist
55
+ entry = <<-EOF
56
+ <entry xmlns="http://www.w3.org/2005/Atom"
57
+ xmlns:yt="http://gdata.youtube.com/schemas/2007">
58
+ <title type="text">Ruby Utility Unit Test</title>
59
+ <summary>This is a test playlist.</summary>
60
+ </entry>
61
+ EOF
62
+
63
+ response = @yt.post('http://gdata.youtube.com/feeds/api/users/default/playlists', entry)
64
+
65
+ edit_uri = response.elements["link[@rel='edit']"].attributes['href']
66
+
67
+ response.elements["summary"].text = "Updated description"
68
+
69
+ response = @yt.put(edit_uri, response.to_s)
70
+
71
+ self.assert_equal("Updated description", response.elements["summary"].text)
72
+
73
+ @yt.delete(edit_uri)
74
+ end
75
+
76
+ end
@@ -0,0 +1,36 @@
1
+ # Copyright (C) 2008 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ $:.unshift(File.dirname(__FILE__))
16
+ require 'test_helper'
17
+
18
+ class TC_GData_HTTP_Request < Test::Unit::TestCase
19
+
20
+ include TestHelper
21
+
22
+ def test_world_is_sane
23
+ assert(true, 'World is not sane.')
24
+ end
25
+
26
+ def test_google_is_live
27
+ request = GData::HTTP::Request.new('http://www.google.com')
28
+
29
+ service = GData::HTTP::DefaultService.new
30
+
31
+ response = service.make_request(request)
32
+
33
+ assert_equal(200, response.status_code)
34
+ end
35
+
36
+ end
@@ -0,0 +1,7 @@
1
+ ---
2
+ username: myaccount
3
+ password: mypassword
4
+ # uncomment to test authsub
5
+ #authsub_token: someauthsubtoken
6
+ # uncomment to test secure authsub
7
+ #authsub_private_key: /path/to/private/key
@@ -0,0 +1,47 @@
1
+ # Copyright (C) 2008 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'yaml'
16
+ require 'test/unit'
17
+ require 'test/unit/ui/console/testrunner'
18
+
19
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
20
+ require 'gdata'
21
+
22
+ module TestHelper
23
+
24
+ def get_config()
25
+ if not defined?(@config_file)
26
+ @config_file = YAML::load_file(File.join(File.dirname(__FILE__), 'test_config.yml'))
27
+ end
28
+ return @config_file
29
+ end
30
+
31
+ def get_username()
32
+ return self.get_config()['username']
33
+ end
34
+
35
+ def get_password()
36
+ return self.get_config()['password']
37
+ end
38
+
39
+ def get_authsub_token()
40
+ return self.get_config()['authsub_token']
41
+ end
42
+
43
+ def get_authsub_private_key()
44
+ return self.get_config()['authsub_private_key']
45
+ end
46
+
47
+ end
@@ -0,0 +1,31 @@
1
+ # Copyright (C) 2008 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ $:.unshift(File.dirname(__FILE__))
16
+ require 'test_helper'
17
+ require 'ts_gdata_http'
18
+ require 'ts_gdata_client'
19
+ require 'ts_gdata_auth'
20
+
21
+ class TS_GData
22
+ def self.suite
23
+ suite = Test::Unit::TestSuite.new("GData Test Suite")
24
+ suite << TS_GData_HTTP.suite
25
+ suite << TS_GData_Client.suite
26
+ suite << TS_GData_Auth.suite
27
+ return suite
28
+ end
29
+ end
30
+
31
+ Test::Unit::UI::Console::TestRunner.run(TS_GData)
@@ -0,0 +1,25 @@
1
+ # Copyright (C) 2008 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'tc_gdata_auth_clientlogin'
16
+ require 'tc_gdata_auth_authsub'
17
+
18
+ class TS_GData_Auth
19
+ def self.suite
20
+ suite = Test::Unit::TestSuite.new
21
+ suite << TC_GData_Auth_ClientLogin.suite
22
+ suite << TC_GData_Auth_AuthSub.suite
23
+ return suite
24
+ end
25
+ end