netposti 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ea8a4313f81485e7c420407ea244747fdab6fb34
4
+ data.tar.gz: eab2ac45e6192bb667dfee414b82d38de17d2e96
5
+ SHA512:
6
+ metadata.gz: 86f02c00e18814264c1f58d675ac53f158e2f802094adad1f4bf1e382f541f38d57c112832e83b5a3a96ecf7b7f6d7a7807083b0fbf0d367c3006b7ec04d2cec
7
+ data.tar.gz: 82d7eb6d432832a1d8d4b17e5475813a62df33c48d0117a49d9940b46deb10ac25ed1187e2246e384666a66de4a605bf3001fe07183ab2c318e6d11da2080ba8
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in netposti.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Christoffer Hamberg
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ # Netposti
2
+
3
+ The mailman netposti forgot.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'netposti'
11
+ ```
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install netposti
16
+
17
+ ## Usage
18
+
19
+ inbox = Netposti.login( username: "dolan", password: "duck" )
20
+ inbox.number_of_unread_messages
21
+ => 1
22
+ inbox.last_message
23
+ => 19.10.2015 09:15: Bank Oy - Invoice
24
+ inbox.last_message.download_attachment
25
+ => Invoice.pdf
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/netposti. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
30
+
31
+
32
+ ## License
33
+
34
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,18 @@
1
+ require "bundler/gem_tasks"
2
+ require "./lib/netposti"
3
+
4
+ task default: %w[last_message]
5
+
6
+ INBOX = Netposti.login( username: ENV['NETPOSTI_USERNAME'], password: ENV['NETPOSTI_PASSWORD'] )
7
+
8
+ task :unread_messages do
9
+ puts "Number of unread messages: #{INBOX.number_of_unread_messages}"
10
+ end
11
+
12
+ task :last_message do
13
+ puts INBOX.last_message.to_hash
14
+ end
15
+
16
+ task :download_last_attachment do
17
+ INBOX.last_message.download_attachment
18
+ end
@@ -0,0 +1,25 @@
1
+ require "netposti/version"
2
+ require "netposti/inbox"
3
+
4
+ require "mechanize"
5
+
6
+ module Netposti
7
+ class LoginError < StandardError; end
8
+
9
+ LOGIN_URL = 'https://www.posti.fi/omatpalvelut/posti/login'.freeze
10
+
11
+ def self.login(opts)
12
+ agent = Mechanize.new
13
+ agent.pluggable_parser.default = Mechanize::Download
14
+
15
+ page = agent.get(LOGIN_URL)
16
+ inbox = page.form_with(:name => 'loginForm') do |f|
17
+ f['login.username'] = opts[:username]
18
+ f['login.password'] = opts[:password]
19
+ end.click_button
20
+
21
+ raise LoginError unless inbox.uri.path.match(/messageListPage/)
22
+
23
+ Inbox.new(inbox)
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ require "netposti/message"
2
+
3
+ module Netposti
4
+ class Inbox
5
+
6
+ def initialize(inbox)
7
+ @inbox = inbox
8
+ end
9
+
10
+ def number_of_unread_messages
11
+ @inbox.search('.unread').count
12
+ end
13
+
14
+ def messages
15
+ @_messages ||= @inbox.links_with(:href => /(.*?)listview(.*?)openMessage$/).lazy.map { |message| Message.new(message.click) }
16
+ end
17
+
18
+ def unread_messages
19
+ messages.first(number_of_unread_messages)
20
+ end
21
+
22
+ def last_message
23
+ messages.first(1).first
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,44 @@
1
+ module Netposti
2
+ class Message
3
+
4
+ def initialize(message)
5
+ @message = message
6
+ end
7
+
8
+ def sender
9
+ @message.search('#message-detail-header-content .sender').text
10
+ end
11
+
12
+ def date
13
+ @message.search('#message-detail-header-content .date').text
14
+ end
15
+
16
+ def subject
17
+ @message.form.field_with(:name => 'messageSubjectTextField').value
18
+ end
19
+
20
+ def attachment_url
21
+ pdf_icon_value = @message.search('.small-pdf-icon').first.attributes['onclick'].value
22
+ download_path = pdf_icon_value.match(/window.location.href='.(.*?)'/)[1].split('-', 2).last
23
+ @message.uri.to_s + '-' + download_path
24
+ end
25
+
26
+ def download_attachment(filename = nil)
27
+ @message.mech.get(attachment_url).save(filename)
28
+ end
29
+
30
+ def to_hash
31
+ {
32
+ sender: sender,
33
+ date: date,
34
+ subject: subject,
35
+ attachment_url: attachment_url
36
+ }
37
+ end
38
+
39
+ def to_s
40
+ "#{date}: #{sender} - #{subject}"
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module Netposti
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'netposti/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "netposti"
8
+ spec.version = Netposti::VERSION
9
+ spec.authors = ["Christoffer Hamberg"]
10
+ spec.email = ["christoffer.hamberg@gmail.com"]
11
+
12
+ spec.summary = %q{The mailman netposti forgot.}
13
+ spec.homepage = "https://github.com/christoffer/netposti"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_runtime_dependency "mechanize", "~> 2.7.3"
24
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: netposti
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Christoffer Hamberg
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mechanize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.7.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.7.3
55
+ description:
56
+ email:
57
+ - christoffer.hamberg@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - CODE_OF_CONDUCT.md
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/netposti.rb
69
+ - lib/netposti/inbox.rb
70
+ - lib/netposti/message.rb
71
+ - lib/netposti/version.rb
72
+ - netposti.gemspec
73
+ homepage: https://github.com/christoffer/netposti
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.6
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: The mailman netposti forgot.
97
+ test_files: []