pollter_geist 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 05f056b84468423bb3113a3bc2e844e3c3dabb7e
4
+ data.tar.gz: 78cc138e11dd795c7cc106c81eff536c98117175
5
+ SHA512:
6
+ metadata.gz: 8c08c5c6b54cd65e0ab5ebff4013fae453f2cccfdb2552091475744cdc421e6f16764c7b5583784f3b5d5758116f6c2779c764783e28dac6804878cf93f7219d
7
+ data.tar.gz: a1184580b5972401a0e31e18a31015dd1a56c04bc38b250bd0369195963ea5486ce4443ca4c53f1c272cd21d98fabdd273eda21723a57c6341e91feb450836ff
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pollter_geist.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 kwando
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # PollterGeist
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pollter_geist'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pollter_geist
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,47 @@
1
+ module PollterGeist
2
+ class IMAPCommands
3
+ def initialize imap, logger
4
+ @imap = imap
5
+ @logger = logger
6
+
7
+ @idler = ImapIdler.new(self)
8
+ end
9
+
10
+ def select_mailbox mailbox
11
+ logger.debug("selecting mailbox #{mailbox}")
12
+ @imap.select(mailbox)
13
+ end
14
+
15
+ # Issues IDLE command for the imap server, it takes a list of events to listen for and returns once one of
16
+ # events occured or if there was a timeout.
17
+ #
18
+ # @param events [events] a list of events to listen for, EXPUNGE, RECENT
19
+ # @param timeout [Numeric] a timeout, if no event has ocoured for this time
20
+ # @return will return an object which responds to #success?, #timeout? and #event
21
+ def wait_for events, timeout = 29*60
22
+ @idler.idle(events, timeout)
23
+ end
24
+
25
+ def message_uids
26
+ @logger.debug('fetching uids')
27
+ # Searching for all messages where TO include @ is a workaround, since the imap#status method
28
+ # does not work for some reason.
29
+ message_ids = @imap.search(['TO', '@'])
30
+ result = @imap.fetch(message_ids, ['UID'])
31
+ result.map { |r| r.attr['UID'].to_i }
32
+ end
33
+
34
+ def fetch uids
35
+ logger.debug "fetching: #{uids}"
36
+ @imap.uid_fetch(uids, ['RFC822']).map { |r| r.attr['RFC822'] }
37
+ end
38
+
39
+ def logger
40
+ @logger
41
+ end
42
+
43
+ def imap
44
+ @imap
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,73 @@
1
+ require 'timeout'
2
+ module PollterGeist
3
+ class ImapIdler
4
+ class IdleResult
5
+ def initialize success, event
6
+ @success = success
7
+ @event = event
8
+ end
9
+
10
+ def success?
11
+ @success
12
+ end
13
+
14
+ def timeout?
15
+ @event == 'TIMEOUT'
16
+ end
17
+
18
+ def event
19
+ @event
20
+ end
21
+ end
22
+
23
+ def initialize poller
24
+ @imap = poller.imap
25
+ @logger = poller.logger
26
+ end
27
+
28
+ def idle listen_for = ['RECENT'], timeout = false
29
+ begin
30
+ Timeout::timeout(timeout) do
31
+ loop do
32
+ event = do_idle
33
+ if listen_for.include? event
34
+ return IdleResult.new(true, event)
35
+ else
36
+ logger.debug "nope, we are not listening for #{event}"
37
+ end
38
+ end
39
+ end
40
+ rescue Timeout::Error
41
+ imap.idle_done
42
+ logger.debug 'idle timed out'
43
+ return IdleResult.new(false, 'TIMEOUT')
44
+ end
45
+ end
46
+
47
+ private
48
+ def imap
49
+ @imap
50
+ end
51
+
52
+ def logger
53
+ @logger
54
+ end
55
+
56
+ def do_idle
57
+ event = ''
58
+ imap.idle do |message|
59
+ case message
60
+ when Net::IMAP::UntaggedResponse
61
+ logger.debug "event: #{message.name}, data: #{message.data}"
62
+ event = message.name
63
+ imap.idle_done
64
+ when Net::IMAP::ContinuationRequest
65
+ logger.debug 'IDLE accepted by the server'
66
+ else
67
+ logger.warn "Unhandled IDLE response: #{message.class}, #{message.inspect}"
68
+ end
69
+ end
70
+ event
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,47 @@
1
+ require 'logger'
2
+ require 'net/imap'
3
+
4
+ module PollterGeist
5
+ class Poller
6
+ LOGGER = Logger.new(STDOUT)
7
+
8
+ def initialize options
9
+ @options = options
10
+ end
11
+
12
+ def session &block
13
+ start(&block)
14
+ end
15
+
16
+ private
17
+ def start &block
18
+ logger.debug "connecting to #{@options[:host]}"
19
+ imap = Net::IMAP.new(@options[:host])
20
+ logger.debug 'connected to imap server'
21
+
22
+ imap.login(@options[:username], @options[:password])
23
+ logger.debug 'authenticated to the server'
24
+
25
+ begin
26
+ commands = IMAPCommands.new(imap, logger)
27
+
28
+ # preselecting mailbox if present in the options
29
+ commands.select_mailbox(@options[:mailbox]) unless @options[:mailbox].nil?
30
+
31
+ # let client code work their magic
32
+ yield(commands)
33
+ rescue Exception => e
34
+ logger.error "#{e.class} : #{e.message} \n #{e.backtrace}"
35
+ raise e
36
+ ensure
37
+ imap.logout
38
+ logger.debug 'logoff'
39
+ end
40
+ end
41
+
42
+ private
43
+ def logger
44
+ LOGGER
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module PollterGeist
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "pollter_geist/version"
2
+
3
+ module PollterGeist
4
+ end
5
+
6
+ require_relative 'pollter_geist/imap_commands'
7
+ require_relative 'pollter_geist/imap_idler'
8
+ require_relative 'pollter_geist/poller'
@@ -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 'pollter_geist/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pollter_geist"
8
+ spec.version = PollterGeist::VERSION
9
+ spec.authors = ["kwando"]
10
+ spec.email = ["kwando@merciless.me"]
11
+ spec.description = %q{A small abstraction layer for polling IMAP servers for changes.}
12
+ spec.summary = %q{A small abstraction layer for polling IMAP servers for changes.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "minitest", "~> 5.0.6"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,9 @@
1
+ require 'pollter_geist'
2
+ require 'minitest/autorun'
3
+ require 'yaml'
4
+
5
+ # This little dance is needed in order to symbolize keys in the hash
6
+ config = YAML.load_file('config.yml')
7
+ CONFIG = {}
8
+ config.each { |k, v| CONFIG[k.to_sym] = v }
9
+ CONFIG.freeze
@@ -0,0 +1,13 @@
1
+ require 'test_helper'
2
+
3
+ describe PollterGeist::Poller do
4
+ describe '#session' do
5
+ it 'should create a session' do
6
+ poller = PollterGeist::Poller.new(CONFIG)
7
+ poller.session do |imap|
8
+ imap.select_mailbox('INBOX')
9
+ imap.wait_for('RECENT', 1)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+
4
+ describe PollterGeist do
5
+ it 'should fail' do
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pollter_geist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - kwando
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-23 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 5.0.6
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 5.0.6
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A small abstraction layer for polling IMAP servers for changes.
56
+ email:
57
+ - kwando@merciless.me
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/pollter_geist.rb
68
+ - lib/pollter_geist/imap_commands.rb
69
+ - lib/pollter_geist/imap_idler.rb
70
+ - lib/pollter_geist/poller.rb
71
+ - lib/pollter_geist/version.rb
72
+ - pollter_geist.gemspec
73
+ - test/test_helper.rb
74
+ - test/unit/pollter_geist/poller_test.rb
75
+ - test/unit/pollter_geist_test.rb
76
+ homepage: ''
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
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
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.0.4
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A small abstraction layer for polling IMAP servers for changes.
100
+ test_files:
101
+ - test/test_helper.rb
102
+ - test/unit/pollter_geist/poller_test.rb
103
+ - test/unit/pollter_geist_test.rb