tuenti 0.1.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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Sergio Cambra
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = tuenti
2
+
3
+ A non-official Ruby API for Tuenti
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Sergio Cambra. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "tuenti"
8
+ gem.summary = %Q{A non-official Ruby API for Tuenti}
9
+ gem.description = %Q{A non-official Ruby API for Tuenti}
10
+ gem.email = "sergio@enpijama.es"
11
+ gem.homepage = "http://github.com/scambra/tuenti"
12
+ gem.authors = ["Sergio Cambra"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ gem.add_dependency "mechanize", ">= 0.9.3"
15
+ gem.add_dependency "json", ">= 1.2.0"
16
+ gem.add_dependency "activesupport", ">= 2.3.4"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
22
+ end
23
+
24
+ require 'rake/testtask'
25
+ Rake::TestTask.new(:test) do |test|
26
+ test.libs << 'lib' << 'test'
27
+ test.pattern = 'test/**/test_*.rb'
28
+ test.verbose = true
29
+ end
30
+
31
+ begin
32
+ require 'rcov/rcovtask'
33
+ Rcov::RcovTask.new do |test|
34
+ test.libs << 'test'
35
+ test.pattern = 'test/**/test_*.rb'
36
+ test.verbose = true
37
+ end
38
+ rescue LoadError
39
+ task :rcov do
40
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
41
+ end
42
+ end
43
+
44
+ task :test => :check_dependencies
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/rdoctask'
49
+ Rake::RDocTask.new do |rdoc|
50
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "tuenti #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/tuenti.rb ADDED
@@ -0,0 +1,80 @@
1
+ require 'active_support'
2
+ require 'mechanize'
3
+ require 'json'
4
+
5
+ class Tuenti
6
+ extend ActiveSupport::Memoizable
7
+
8
+ LOGIN_URL = 'https://www.tuenti.com/?m=Login&func=do_login'
9
+ HOME_URL = 'http://www.tuenti.com/?m=home&func=view_home'
10
+ UPLOAD_URL = 'http://fotos.tuenti.com/?upload=1&iframe=1'
11
+ ALBUMS_URL = 'http://www.tuenti.com/?m=Search&func=get_user_custom_albums_for_data_source&ajax=1'
12
+ EDIT_PHOTO_URL = 'http://www.tuenti.com/?m=Photoedit&func=process_edit_photo&ajax=1'
13
+
14
+ CSRF_REGEXP = /csrf(&quot;)?[:=](&quot;)?([0-9a-zA-Z]+)\b/
15
+ CSRF_REGEXP_INDEX = 3
16
+
17
+ def initialize(user, password, timezone = Time.now.utc_offset/3600)
18
+ @agent = WWW::Mechanize.new
19
+ @agent.post LOGIN_URL, :email => user, :input_password => password, :timezone => timezone
20
+ @csrf = @agent.get(HOME_URL).body.match(CSRF_REGEXP)[CSRF_REGEXP_INDEX]
21
+ end
22
+
23
+ def upload_photo(file, attributes = nil)
24
+ qid = @agent.submit(upload_photo_form(file)).search('#request_data').text
25
+ id = nil
26
+ # el formato del id es album_id-user_id-photo_id-user_id
27
+ while id.nil? || id =~ /-0-/ do # hasta que no se procesa photo_id es 0
28
+ sleep(0.5)
29
+ id = @agent.post(UPLOAD_URL, :func => 'checkq', :qid => qid).search('#request_data').text
30
+ end
31
+ if attributes
32
+ options = {:csrf => @csrf, :collection_key => id, :photo_title => attributes[:title] || ''}
33
+ if attributes[:album]
34
+ options[:'add_albums_collection_keys[]'] = album_id(attributes[:album])
35
+ @new_albums_count = 0
36
+ end
37
+ @agent.post EDIT_PHOTO_URL, options
38
+ end
39
+ id
40
+ end
41
+
42
+
43
+ def albums
44
+ JSON.parse(@agent.get(ALBUMS_URL).search('#request_data').text)["results"].inject({}) do |albums, result|
45
+ albums.update(result["string"] => result["id"])
46
+ end
47
+ end
48
+ memoize :albums
49
+
50
+ def album_id(album)
51
+ albums[album] || new_album_id(album)
52
+ end
53
+
54
+ def set_album(*photos)
55
+ end
56
+
57
+ private
58
+ def new_album_id(album)
59
+ flush_cache :albums
60
+ @new_albums_count ||= 0
61
+ "__NEW_ALBUM__#{album}__NEW_ALBUM_END__#{@new_albums_count+=1}"
62
+ end
63
+
64
+ def upload_photo_form(file)
65
+ form = build_form(UPLOAD_URL, 'POST', true)
66
+ form.file_uploads << WWW::Mechanize::Form::FileUpload.new("name", file)
67
+ form.fields << WWW::Mechanize::Form::Field.new("func",'addq')
68
+ form.fields << WWW::Mechanize::Form::Field.new("rotate",'0')
69
+ form
70
+ end
71
+
72
+ def build_form(action, method = 'POST', multipart = false)
73
+ node = Nokogiri::XML::Element.new('form', Nokogiri::XML::Document.new) do |form|
74
+ form['action'] = action
75
+ form['method'] = method
76
+ form['enctype'] = 'multipart/form-data' if multipart
77
+ end
78
+ form = WWW::Mechanize::Form.new(node)
79
+ end
80
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'tuenti'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestTuenti < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
data/tuenti.gemspec ADDED
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{tuenti}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Sergio Cambra"]
12
+ s.date = %q{2009-11-14}
13
+ s.description = %q{A non-official Ruby API for Tuenti}
14
+ s.email = %q{sergio@enpijama.es}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/tuenti.rb",
27
+ "test/helper.rb",
28
+ "test/test_tuenti.rb",
29
+ "tuenti.gemspec"
30
+ ]
31
+ s.homepage = %q{http://github.com/scambra/tuenti}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{A non-official Ruby API for Tuenti}
36
+ s.test_files = [
37
+ "test/test_tuenti.rb",
38
+ "test/helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
47
+ s.add_runtime_dependency(%q<mechanize>, [">= 0.9.3"])
48
+ s.add_runtime_dependency(%q<json>, [">= 1.2.0"])
49
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
50
+ else
51
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
52
+ s.add_dependency(%q<mechanize>, [">= 0.9.3"])
53
+ s.add_dependency(%q<json>, [">= 1.2.0"])
54
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
58
+ s.add_dependency(%q<mechanize>, [">= 0.9.3"])
59
+ s.add_dependency(%q<json>, [">= 1.2.0"])
60
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
61
+ end
62
+ end
63
+
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tuenti
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergio Cambra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-14 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: mechanize
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.3
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: json
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.2.0
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: activesupport
47
+ type: :runtime
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.3.4
54
+ version:
55
+ description: A non-official Ruby API for Tuenti
56
+ email: sergio@enpijama.es
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - LICENSE
63
+ - README.rdoc
64
+ files:
65
+ - .document
66
+ - .gitignore
67
+ - LICENSE
68
+ - README.rdoc
69
+ - Rakefile
70
+ - VERSION
71
+ - lib/tuenti.rb
72
+ - test/helper.rb
73
+ - test/test_tuenti.rb
74
+ - tuenti.gemspec
75
+ has_rdoc: true
76
+ homepage: http://github.com/scambra/tuenti
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --charset=UTF-8
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ requirements: []
97
+
98
+ rubyforge_project:
99
+ rubygems_version: 1.3.5
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: A non-official Ruby API for Tuenti
103
+ test_files:
104
+ - test/test_tuenti.rb
105
+ - test/helper.rb