email_bills 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c76a499be98d53d33935d804ae5634b37d773e9c
4
+ data.tar.gz: b892452b0c17d6d228aede15fe4cec8f474cfe9a
5
+ SHA512:
6
+ metadata.gz: bc3579dcf6df5cc0c866ca7f644888ac868f0004ce2c59bf52d0ee9fe76044703047e30d60c0a48995e8364e5621f863761ded54513e1a4d6abffa2382bc64ab
7
+ data.tar.gz: a3571393c88510dac1cf47be93da5cdf4e10a120777a3d11434b1d6692912f5ae2ca6d0c72781c83a194fcc97c958a6f0c5b44758ec4fc40ad138a50122c3468
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .ruby-gemset
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.2.5
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in email-bills.gemspec
4
+ gemspec
5
+
6
+ gem 'mail'
7
+
8
+ group :development do
9
+ gem 'rspec'
10
+ end
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Email::Bills
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/email/bills`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'email-bills'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install email-bills
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/email-bills.
36
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "email_bills"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/build/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/build/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in build.gemspec
4
+ gemspec
data/build/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Build
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/build`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'build'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install build
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/build.
36
+
data/build/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/build/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "build"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/build/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'build/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "build"
8
+ spec.version = Build::VERSION
9
+ spec.authors = ["Michael Wheeler"]
10
+ spec.email = ["mwheeler@g2crowd.com"]
11
+
12
+ spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}
13
+ spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "TODO: Put your gem's website or public repo URL here."
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.13"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ end
@@ -0,0 +1,3 @@
1
+ module Build
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "build/version"
2
+
3
+ module Build
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'email_bills/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "email_bills"
8
+ spec.version = EmailBills::VERSION
9
+ spec.authors = ["Michael Wheeler"]
10
+ spec.email = ["mwheeler@g2crowd.com"]
11
+
12
+ spec.summary = %q{Check an email inbox, to find and split bills}
13
+ spec.homepage = "https://github.com/wheeyls/email-bills"
14
+
15
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
16
+ # delete this section to allow pushing this gem to any host.
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
@@ -0,0 +1,62 @@
1
+ require 'net/imap'
2
+ require 'mail'
3
+
4
+ module EmailBills
5
+ class GmailChecker
6
+ def initialize(box = '', user = ENV['GMAIL_USER'], pass = ENV['GMAIL_PASSWORD'])
7
+ @user = user
8
+ @pass = pass
9
+ @mailbox = box
10
+ end
11
+
12
+ def login
13
+ return if @logged_in
14
+ @imap = Net::IMAP.new 'imap.gmail.com', 993, true
15
+ @imap.login(@user, @pass)
16
+ @logged_in = true
17
+ end
18
+
19
+ def get_since(date, box = @mailbox)
20
+ login
21
+ @imap.select(box)
22
+ seq_ids = @imap.search(['SINCE', date.strftime('%d-%b-%Y')])
23
+ uids = @imap.fetch(seq_ids, 'UID').map { |item| item.attr['UID'] }
24
+
25
+ get_from_server uids
26
+ end
27
+
28
+ def get_from_server(uids)
29
+ uids.map do |uid|
30
+ items = @imap.fetch(uid, 'RFC822')
31
+ item = items.first.attr['RFC822'] if items && items.count > 0
32
+
33
+ extract_and_build uid, item if item
34
+ end
35
+ end
36
+
37
+ def extract_and_build(uid, imap_message)
38
+ mail = Mail.new imap_message
39
+
40
+ Message.new uid: uid,
41
+ body: get_body(mail),
42
+ received_date: mail.date,
43
+ subject: mail.subject,
44
+ message_id: mail.message_id,
45
+ raw: mail.encoded
46
+ end
47
+
48
+ def get_body(mail)
49
+ GmailChecker.html_part(mail) || GmailChecker.plain_part(mail)
50
+ end
51
+
52
+ def self.plain_part(mail)
53
+ mail.multipart? ?
54
+ (mail.text_part ? mail.text_part.body.encoded : nil) :
55
+ mail.body.encoded
56
+ end
57
+
58
+ def self.html_part(mail)
59
+ mail.html_part ? mail.html_part.body.encoded : nil
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,14 @@
1
+ module EmailBills
2
+ class Message
3
+ attr_reader :uid, :body, :received_date, :subject, :message_id, :raw
4
+
5
+ def initialize(uid:, body:, received_date:, subject:, message_id:, raw:)
6
+ @uid = uid
7
+ @body = body
8
+ @received_date = received_date
9
+ @subject = subject
10
+ @message_id = message_id
11
+ @raw = raw
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,35 @@
1
+ module EmailBills
2
+ module Receipts
3
+ class Base
4
+ attr_reader :message
5
+
6
+ def initialize(message)
7
+ @message = message
8
+ end
9
+
10
+ def value
11
+ parsed[0].to_f
12
+ end
13
+
14
+ def date
15
+ message.received_date
16
+ end
17
+
18
+ def value?
19
+ parsed
20
+ end
21
+
22
+ def parsed
23
+ @parsed ||= content.match(number_finder)
24
+ end
25
+
26
+ def content
27
+ message.body
28
+ end
29
+
30
+ def number_finder
31
+ fail NotImplementedError
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,13 @@
1
+ module EmailBills
2
+ module Receipts
3
+ class Comcast < Base
4
+ def value
5
+ parsed[1].to_f
6
+ end
7
+
8
+ def number_finder
9
+ /\$([0-9]+\.[0-9]{2})/
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module EmailBills
2
+ module Receipts
3
+ class Comed < Base
4
+ def value
5
+ parsed[1].to_f
6
+ end
7
+
8
+ def number_finder
9
+ /\$([0-9]+\.[0-9]{2})/
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module EmailBills
2
+ module Receipts
3
+ class PeoplesGas < Base
4
+ def value
5
+ parsed[1].to_f
6
+ end
7
+
8
+ def number_finder
9
+ /\$([0-9]+\.[0-9]{2})/
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,34 @@
1
+ module EmailBills
2
+ module Recurring
3
+ class Monthly
4
+ attr_reader :title, :value, :bill_day, :from, :to
5
+
6
+ def initialize(title, value, bill_day, from:, to: DateTime.now)
7
+ @title = title
8
+ @value = value
9
+ @bill_day = bill_day
10
+ @from = from
11
+ @to = to
12
+ end
13
+
14
+ def receipts
15
+ receipts ||= billed_days.map { |day| EmailBills::Recurring::Payment.new(value, day) }
16
+ end
17
+
18
+ def total
19
+ receipts.reduce(0.0) do |memo, receipt|
20
+ memo += receipt.value if receipt.value?
21
+ memo
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def billed_days
28
+ from.upto(to).select do |date|
29
+ date.day == bill_day
30
+ end.reverse
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,16 @@
1
+ module EmailBills
2
+ module Recurring
3
+ class Payment
4
+ attr_reader :value, :date
5
+
6
+ def initialize(value, date)
7
+ @value = value
8
+ @date = date
9
+ end
10
+
11
+ def value?
12
+ !value.nil?
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,42 @@
1
+ module EmailBills
2
+ module Vendors
3
+ class Base
4
+ attr_reader :from, :checker
5
+
6
+ def initialize(from:, checker: EmailBills::GmailChecker.new)
7
+ @from = from
8
+ @checker = checker
9
+ end
10
+
11
+ def receipts
12
+ @messages ||= messages.map { |i| receipt_klass.new(i) }
13
+ end
14
+
15
+ def total
16
+ receipts.reduce(0.0) do |memo, receipt|
17
+ memo += receipt.value if receipt.value?
18
+ memo
19
+ end
20
+ end
21
+
22
+ def title
23
+ mailbox
24
+ end
25
+
26
+ private
27
+
28
+ def messages
29
+ @messages ||= checker.get_since(from, mailbox)
30
+ end
31
+
32
+ def mailbox
33
+ fail NotImplementedError
34
+ end
35
+
36
+ def receipt_klass
37
+ name = self.class.name.match(/::([^:]+)$/)[1]
38
+ EmailBills::Receipts.const_get name
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,9 @@
1
+ module EmailBills
2
+ module Vendors
3
+ class Comcast < Base
4
+ def mailbox
5
+ 'Comcast'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module EmailBills
2
+ module Vendors
3
+ class Comed < Base
4
+ def mailbox
5
+ 'ComEd'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module EmailBills
2
+ module Vendors
3
+ class PeoplesGas < Base
4
+ def mailbox
5
+ 'PeoplesGas'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module EmailBills
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ module EmailBills
2
+ class View
3
+ attr_reader :vendor, :split
4
+
5
+ def initialize(vendor, split = 3)
6
+ @vendor = vendor
7
+ @split = split
8
+ end
9
+
10
+ def render
11
+ puts "#{vendor.title}"
12
+ puts "-----------------"
13
+
14
+ puts "Billed Date Amount"
15
+ vendor.receipts.each do |receipt|
16
+ puts "#{receipt.date.strftime('%b %d %Y')} #{receipt.value.round(2)}" if receipt.value?
17
+ end
18
+
19
+ puts ""
20
+ puts "#{vendor.title} total: #{vendor.total.round(2)}"
21
+ puts "#{vendor.title} total each: #{(vendor.total / 3).round(2)}"
22
+ puts ""
23
+ puts ""
24
+ puts ""
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ require 'email_bills/version'
2
+ require 'email_bills/gmail_checker'
3
+ require 'email_bills/message'
4
+ require 'email_bills/view'
5
+
6
+ require 'email_bills/vendors/base'
7
+ require 'email_bills/vendors/comcast'
8
+ require 'email_bills/vendors/comed'
9
+ require 'email_bills/vendors/peoples_gas'
10
+
11
+ require 'email_bills/receipts/base'
12
+ require 'email_bills/receipts/comcast'
13
+ require 'email_bills/receipts/comed'
14
+ require 'email_bills/receipts/peoples_gas'
15
+
16
+ require 'email_bills/recurring/monthly'
17
+ require 'email_bills/recurring/payment'
18
+
19
+ module EmailBills
20
+ # Your code goes here...
21
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: email_bills
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Wheeler
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-09 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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
+ description:
42
+ email:
43
+ - mwheeler@g2crowd.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".ruby-version"
50
+ - Gemfile
51
+ - README.md
52
+ - Rakefile
53
+ - bin/console
54
+ - bin/setup
55
+ - build/.gitignore
56
+ - build/Gemfile
57
+ - build/README.md
58
+ - build/Rakefile
59
+ - build/bin/console
60
+ - build/bin/setup
61
+ - build/build.gemspec
62
+ - build/lib/build.rb
63
+ - build/lib/build/version.rb
64
+ - email_bills.gemspec
65
+ - lib/email_bills.rb
66
+ - lib/email_bills/gmail_checker.rb
67
+ - lib/email_bills/message.rb
68
+ - lib/email_bills/receipts/base.rb
69
+ - lib/email_bills/receipts/comcast.rb
70
+ - lib/email_bills/receipts/comed.rb
71
+ - lib/email_bills/receipts/peoples_gas.rb
72
+ - lib/email_bills/recurring/monthly.rb
73
+ - lib/email_bills/recurring/payment.rb
74
+ - lib/email_bills/vendors/base.rb
75
+ - lib/email_bills/vendors/comcast.rb
76
+ - lib/email_bills/vendors/comed.rb
77
+ - lib/email_bills/vendors/peoples_gas.rb
78
+ - lib/email_bills/version.rb
79
+ - lib/email_bills/view.rb
80
+ homepage: https://github.com/wheeyls/email-bills
81
+ licenses: []
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.4.8
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Check an email inbox, to find and split bills
103
+ test_files: []