rube_post 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/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source :rubygems
2
+
3
+ gem 'mechanize'
4
+ gem 'nokogiri'
5
+
6
+ group :dev do
7
+ gem 'rake'
8
+ gem 'rspec', '~>2'
9
+ gem 'jeweler'
10
+ end
@@ -0,0 +1,31 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ mechanize (1.0.0)
11
+ nokogiri (>= 1.2.1)
12
+ nokogiri (1.4.3.1)
13
+ rake (0.8.7)
14
+ rspec (2.3.0)
15
+ rspec-core (~> 2.3.0)
16
+ rspec-expectations (~> 2.3.0)
17
+ rspec-mocks (~> 2.3.0)
18
+ rspec-core (2.3.1)
19
+ rspec-expectations (2.3.0)
20
+ diff-lcs (~> 1.1.2)
21
+ rspec-mocks (2.3.0)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ jeweler
28
+ mechanize
29
+ nokogiri
30
+ rake
31
+ rspec (~> 2)
@@ -0,0 +1,20 @@
1
+ task :default => :spec
2
+ require "rspec/core/rake_task"
3
+ RSpec::Core::RakeTask.new(:spec) do |t|
4
+ t.rspec_opts = '--backtrace --color'
5
+ end
6
+
7
+ begin
8
+ require 'jeweler'
9
+ Jeweler::Tasks.new do |gem|
10
+ gem.name = 'rube_post'
11
+ gem.summary = "Receive and send emails with epost.de (rub-e-post)"
12
+ gem.email = "michael@grosser.it"
13
+ gem.homepage = "http://github.com/grosser/#{gem.name}"
14
+ gem.authors = ["Michael Grosser"]
15
+ end
16
+
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: gem install jeweler"
20
+ end
@@ -0,0 +1,36 @@
1
+ Receive and send(todo) emails with epost.de (pronounced rub-e-post)
2
+
3
+ Install
4
+ =======
5
+ sudo gem install rube_post
6
+
7
+ !! Set your account to list-view !!
8
+
9
+ Usage
10
+ =====
11
+ client = RubePost.new(username, password)
12
+ emails = client.emails_in_inbox
13
+
14
+ mail = emails.first
15
+ puts mail.sender
16
+ puts mail.content # triggers new request
17
+ puts mail.subject
18
+ puts mail.id
19
+
20
+ mail.move_to_trash
21
+
22
+ Move all mails to gmail.
23
+ gem install gmail
24
+ RubePost.new(username, password).move_to_gmail(gmail_username, gmail_password)
25
+
26
+ TODO
27
+ =====
28
+ - work for more than first page of inbox
29
+ - send mails ? <-> let user enter tan or receive tan via some service
30
+
31
+
32
+ Author
33
+ ======
34
+ [Michael Grosser](http://grosser.it)
35
+ grosser.michael@gmail.com
36
+ Hereby placed under public domain, do what you want, just do not hold me accountable...
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,110 @@
1
+ require 'mechanize'
2
+ require 'nokogiri'
3
+
4
+ class RubePost
5
+ VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
6
+
7
+ def initialize(username, password)
8
+ @grabber = Grabber.new(username, password)
9
+ end
10
+
11
+ def emails_in_inbox
12
+ @grabber.emails_in_inbox.map{|d| Email.new(d, @grabber) }
13
+ end
14
+
15
+ def move_to_gmail(username, password)
16
+ require 'gmail'
17
+ my_email = (username.include?('@') ? username : "#{username}@gmail.com")
18
+ Gmail.connect(username, password) do |gmail|
19
+ emails_in_inbox.each do |email|
20
+ gmail.deliver do
21
+ to my_email
22
+ subject email.subject
23
+ text_part do
24
+ body "#{email.sender}:\nepost-id:#{email.id}\n\n#{email.content}"
25
+ end
26
+ end
27
+ email.move_to_trash
28
+ end
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ class Grabber
35
+ URL = "https://portal.epost.de"
36
+ SHOW_URL = "#{URL}/mail/mailbox/view?msgStart=&messages="
37
+
38
+ def initialize(username, password)
39
+ @agent = Mechanize.new
40
+ page = @agent.get("#{URL}/login")
41
+ login_form = page.forms.first
42
+ login_form.field_with('username').value = username.sub(/@.*/,'')
43
+ login_form.field_with('password').value = password
44
+ @inbox = login_form.submit
45
+ @account = messages_form.action.match(/(\d{10,})/)[1]
46
+ @agent
47
+ end
48
+
49
+ def emails_in_inbox
50
+ doc = Nokogiri::HTML(@inbox.send(:html_body))
51
+ mails = doc.css('#messageList > tr')[1..-1] || raise('could not open inbox <-> login failed ?')
52
+ mails.map do |mail|
53
+ {
54
+ :id => mail.attr('id').split('/').last,
55
+ :sender => self.class.extract_sender(mail),
56
+ :subject => mail.css('td.subject').text.strip
57
+ }
58
+ end
59
+ end
60
+
61
+ def email_content(id)
62
+ body = @agent.get("#{SHOW_URL}#{full_id(id).gsub('/','%2F')}").send(:html_body)
63
+ Nokogiri::HTML(body).css('#e-post-body').first.text.strip
64
+ end
65
+
66
+ def move_to_trash(id)
67
+ form = messages_form
68
+ checkbox = form.checkboxes.detect{|c| c.value == full_id(id) }
69
+ checkbox.check
70
+ button = form.buttons.detect{|b| b.name == 'cmd[delete-message]'}
71
+ page = @agent.submit(form, button)
72
+ checkbox.uncheck # revert checking or something might o wrong later...
73
+ page
74
+ end
75
+
76
+ private
77
+
78
+ def full_id(id)
79
+ "/#{@account}/INBOX/#{id}"
80
+ end
81
+
82
+ def messages_form
83
+ @inbox.forms.detect{|f| f.name == 'messages' }
84
+ end
85
+
86
+ def self.extract_sender(mail)
87
+ name, email = mail.css('td.sender a').text.strip.match(/(.*)\s+<(.*)>/)[1..-1]
88
+ email
89
+ end
90
+ end
91
+
92
+ class Email
93
+ attr_reader :id, :sender, :subject
94
+
95
+ def initialize(data, grabber)
96
+ @id = data[:id]
97
+ @sender = data[:sender]
98
+ @subject = data[:subject]
99
+ @grabber = grabber
100
+ end
101
+
102
+ def content
103
+ @grabber.email_content(@id)
104
+ end
105
+
106
+ def move_to_trash
107
+ @grabber.move_to_trash(@id)
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,50 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rube_post}
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 = ["Michael Grosser"]
12
+ s.date = %q{2010-12-27}
13
+ s.email = %q{michael@grosser.it}
14
+ s.files = [
15
+ "Gemfile",
16
+ "Gemfile.lock",
17
+ "Rakefile",
18
+ "Readme.md",
19
+ "VERSION",
20
+ "lib/rube_post.rb",
21
+ "rube_post.gemspec",
22
+ "spec/rube_post_spec.rb",
23
+ "spec/spec_helper.rb"
24
+ ]
25
+ s.homepage = %q{http://github.com/grosser/rube_post}
26
+ s.require_paths = ["lib"]
27
+ s.rubygems_version = %q{1.3.7}
28
+ s.summary = %q{Receive and send emails with epost.de (rub-e-post)}
29
+ s.test_files = [
30
+ "spec/rube_post_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+
34
+ if s.respond_to? :specification_version then
35
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
36
+ s.specification_version = 3
37
+
38
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
39
+ s.add_runtime_dependency(%q<mechanize>, [">= 0"])
40
+ s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
41
+ else
42
+ s.add_dependency(%q<mechanize>, [">= 0"])
43
+ s.add_dependency(%q<nokogiri>, [">= 0"])
44
+ end
45
+ else
46
+ s.add_dependency(%q<mechanize>, [">= 0"])
47
+ s.add_dependency(%q<nokogiri>, [">= 0"])
48
+ end
49
+ end
50
+
@@ -0,0 +1,25 @@
1
+ require File.expand_path('spec/spec_helper')
2
+
3
+ describe RubePost do
4
+ it "has a VERSION" do
5
+ RubePost::VERSION.should =~ /^\d+\.\d+\.\d+$/
6
+ end
7
+
8
+ describe 'fetch' do
9
+ before :all do
10
+ @emails = RubePost.new(CFG['username'], CFG['password']).emails_in_inbox
11
+ end
12
+
13
+ # if you do not have mails, ask a question to support -> you get a mail
14
+ it "can fetch mail" do
15
+ @emails.size.should >= 0
16
+ @emails.first.id.should =~ %r{^\d+$}
17
+ @emails.first.subject.to_s .should_not == ''
18
+ @emails.first.sender.should include('@')
19
+ end
20
+
21
+ it "can get the content" do
22
+ @emails.first.content.size.should >= 100
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift 'lib'
2
+ require 'rube_post'
3
+
4
+ CFG = YAML.load(File.read('spec/config.yml'))
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rube_post
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Michael Grosser
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-27 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ name: mechanize
32
+ prerelease: false
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ hash: 3
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ name: nokogiri
46
+ prerelease: false
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ description:
50
+ email: michael@grosser.it
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - Gemfile
59
+ - Gemfile.lock
60
+ - Rakefile
61
+ - Readme.md
62
+ - VERSION
63
+ - lib/rube_post.rb
64
+ - rube_post.gemspec
65
+ - spec/rube_post_spec.rb
66
+ - spec/spec_helper.rb
67
+ has_rdoc: true
68
+ homepage: http://github.com/grosser/rube_post
69
+ licenses: []
70
+
71
+ post_install_message:
72
+ rdoc_options: []
73
+
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.3.7
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Receive and send emails with epost.de (rub-e-post)
101
+ test_files:
102
+ - spec/rube_post_spec.rb
103
+ - spec/spec_helper.rb