redimap 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8cea1f30a4c9e919e8fc0d9d3864f11d6bc9f3b4
4
- data.tar.gz: ea39aa22e91e477c542d1caf7ed6c3ea3d22f2be
3
+ metadata.gz: b6528b99d1b6dcd904c2c60b2e2a292cc6c88210
4
+ data.tar.gz: 1937d909847c35c540556241434fac179782280d
5
5
  SHA512:
6
- metadata.gz: 0dd4648e0b0e08b2471dcbaf818262bd659edb03da51da87629c63bfcd463a6cd8db4ba2f5fadda1321cdddcd3693a5491085745fce328dbbad06709ac25e461
7
- data.tar.gz: 69b0f8c35ad1d38fd679b031dd8243866c9f7323b0d5c5eafb98960ad8fa9d1e91284510919205d65adba9f6ec8cb2e1b55e80953018d293ec3335746e7597b9
6
+ metadata.gz: 5138560b0e28a1b78f7cd0abfefd95d19bca1fa73a7ade2ca86cf925f08576daca78e9169e06e024f851c922594bc139275c864dc1b2838c9165a12d66a248b3
7
+ data.tar.gz: 4279f416c647a13cad78b80e149e4974e1aca47e8ba17fb75413d5ea697f3ce744939b356ff19f110f77df460e69e06a3efd0252a9a05c7e605a36d1107bb1cb
data/.gitignore CHANGED
@@ -1,17 +1,16 @@
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
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/version_tmp/
16
+ /tmp/
data/README.md CHANGED
@@ -29,9 +29,9 @@ Check and queue new messages and quit:
29
29
 
30
30
  $ bundle exec redimap
31
31
 
32
- Check and queue new messages but run for eternity using crude polling:
32
+ Check and queue new messages but run for eternity:
33
33
 
34
- $ bundle exec redimap 1
34
+ $ bundle exec redimap --eternal
35
35
 
36
36
 
37
37
  ## Contributions
@@ -1,16 +1,29 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ require 'optparse'
4
+
3
5
  require File.expand_path(File.dirname(__FILE__) + '/../lib/redimap')
4
6
 
5
7
 
6
- running = true
8
+ # = Parse opts
9
+
10
+ options = {}
7
11
 
8
- while running
9
- Redimap.read_mailboxes
12
+ OptionParser.new do |opts|
13
+ opts.banner = "Usage: redimap [options]"
10
14
 
11
- if ARGV[0].to_i == 1 # Oh so crude.
12
- sleep Redimap.config.polling_interval # Sleep awhile.
13
- else
14
- running = false
15
+ opts.on("-e", "--eternal", "Run eternally") do |e|
16
+ options[:eternal] = true
15
17
  end
18
+ end.parse!
19
+
20
+
21
+ # = Main loop
22
+
23
+ loop do
24
+ Redimap.queue_new_mailboxes_uids
25
+
26
+ break unless options[:eternal]
27
+
28
+ sleep Redimap.config.polling_interval # Sleep awhile.
16
29
  end
@@ -1,4 +1,4 @@
1
1
  require_relative 'redimap/config'
2
- require_relative 'redimap/core'
2
+ require_relative 'redimap/base'
3
3
  require_relative 'redimap/imap_conn'
4
4
  require_relative 'redimap/redis_conn'
@@ -5,10 +5,10 @@ module Redimap
5
5
  end
6
6
 
7
7
  def self.configure
8
- yield config if block_given?
8
+ yield @config
9
9
  end
10
10
 
11
- def self.read_mailboxes
11
+ def self.queue_new_mailboxes_uids
12
12
  Redimap::ImapConn.new do |imap|
13
13
  Redimap::RedisConn.new do |redis|
14
14
  Redimap.config.imap_mailboxes.each do |mailbox|
@@ -4,18 +4,18 @@ require 'json'
4
4
  module Redimap
5
5
  class Config
6
6
 
7
- attr_reader :imap_host
8
- attr_reader :imap_port
9
- attr_reader :imap_username
10
- attr_reader :imap_password
7
+ attr_accessor :imap_host
8
+ attr_accessor :imap_port
9
+ attr_accessor :imap_username
10
+ attr_accessor :imap_password
11
11
 
12
- attr_reader :imap_mailboxes
12
+ attr_accessor :imap_mailboxes
13
13
 
14
- attr_reader :redis_url
15
- attr_reader :redis_ns_redimap
16
- attr_reader :redis_ns_queue
14
+ attr_accessor :redis_url
15
+ attr_accessor :redis_ns_redimap
16
+ attr_accessor :redis_ns_queue
17
17
 
18
- attr_reader :polling_interval
18
+ attr_accessor :polling_interval
19
19
 
20
20
  def initialize
21
21
  @imap_host = ENV['IMAP_HOST']
@@ -4,8 +4,6 @@ require 'net/imap'
4
4
  module Redimap
5
5
  class ImapConn
6
6
 
7
- attr_accessor :imap
8
-
9
7
  def initialize
10
8
  @imap = Net::IMAP.new(Redimap.config.imap_host, {
11
9
  :port => Redimap.config.imap_port,
@@ -1,18 +1,23 @@
1
1
  require 'redis'
2
2
  require 'json'
3
+ require 'securerandom'
3
4
 
4
5
 
5
6
  module Redimap
6
7
  class RedisConn
7
8
 
8
- QUEUE_QUEUE = 'redimap'
9
- QUEUE_CLASS = 'RedimapJob'
10
-
11
- attr_accessor :redis
9
+ @@RESCUE_QUEUE = 'redimap'
10
+ @@RESCUE_CLASS = 'RedimapJob'
12
11
 
13
12
  def initialize
14
13
  @redis = Redis.connect(:url => Redimap.config.redis_url)
15
14
 
15
+ @KEYS = {
16
+ :redimap_mailboxes => "#{Redimap.config.redis_ns_redimap}:mailboxes",
17
+ :rescue_queues => "#{Redimap.config.redis_ns_queue}:queues",
18
+ :rescue_queue_redimap => "#{Redimap.config.redis_ns_queue}:queue:#{@@RESCUE_QUEUE}",
19
+ }.freeze
20
+
16
21
  if block_given?
17
22
  yield self
18
23
 
@@ -27,35 +32,31 @@ module Redimap
27
32
  end
28
33
 
29
34
  def get_mailbox_uid(mailbox)
30
- @redis.hget(
31
- "#{Redimap.config.redis_ns_redimap}:mailboxes",
32
- mailbox
33
- ).to_i # Also handles nil.
35
+ @redis.hget(@KEYS[:redimap_mailboxes], mailbox).to_i # Also handles nil.
34
36
  end
35
37
 
36
38
  def set_mailbox_uid(mailbox, uid)
37
- @redis.hset(
38
- "#{Redimap.config.redis_ns_redimap}:mailboxes",
39
- mailbox,
40
- uid
41
- )
39
+ @redis.hset(@KEYS[:redimap_mailboxes], mailbox, uid)
42
40
  end
43
41
 
44
42
  def queue_mailbox_uid(mailbox, uid)
45
- @redis.sadd(
46
- "#{Redimap.config.redis_ns_queue}:queues",
47
- QUEUE_QUEUE
48
- )
43
+ @redis.sadd(@KEYS[:rescue_queues], @@RESCUE_QUEUE)
49
44
 
50
- payload = {
51
- :class => QUEUE_CLASS,
52
- :args => [mailbox, uid]
45
+ @redis.rpush(@KEYS[:rescue_queue_redimap], payload(mailbox, uid))
46
+ end
47
+
48
+ private
49
+
50
+ def payload(mailbox, uid)
51
+ {
52
+ # resque
53
+ :class => @@RESCUE_CLASS,
54
+ :args => [mailbox, uid],
55
+ # sidekiq (extra)
56
+ :queue => @@RESCUE_QUEUE,
57
+ :retry => true,
58
+ :jid => SecureRandom.hex(12),
53
59
  }.to_json
54
-
55
- @redis.rpush(
56
- "#{Redimap.config.redis_ns_queue}:queue:#{QUEUE_QUEUE}",
57
- payload
58
- )
59
60
  end
60
61
 
61
62
  end
@@ -1,5 +1,5 @@
1
1
  module Redimap
2
2
 
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
 
5
5
  end
@@ -1,5 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
+
3
4
  describe Redimap::Config do
4
5
 
5
6
  context "Defaults" do
@@ -29,7 +30,7 @@ describe Redimap::Config do
29
30
  @config.redis_ns_queue.should == "resque"
30
31
  end
31
32
 
32
- it "redis_ns_queue should default to 60 seconds" do
33
+ it "polling_interval should default to 60 seconds" do
33
34
  @config.polling_interval.should == 60
34
35
  end
35
36
  end
@@ -1,5 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
+
3
4
  describe Redimap do
4
5
 
5
6
  context "Constants" do
@@ -2,6 +2,7 @@ require 'net/imap'
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
+
5
6
  describe Redimap::ImapConn do
6
7
 
7
8
  before(:each) do
@@ -14,7 +15,7 @@ describe Redimap::ImapConn do
14
15
 
15
16
  context "#initialize" do
16
17
  it "should set imap as Net::IMAP" do
17
- Redimap::ImapConn.new.imap.should == @fake_net_imap
18
+ Redimap::ImapConn.new.instance_variable_get('@imap').should == @fake_net_imap
18
19
  end
19
20
 
20
21
  it "should #close when block" do
@@ -27,20 +28,22 @@ describe Redimap::ImapConn do
27
28
  context "#close" do
28
29
  before(:each) do
29
30
  @imap = Redimap::ImapConn.new
31
+
32
+ @imap_imap = @imap.instance_variable_get('@imap')
30
33
  end
31
34
 
32
35
  it "should disconnect from IMAP" do
33
- @imap.imap.stub(:logout)
36
+ @imap_imap.stub(:logout)
34
37
 
35
- @imap.imap.should_receive(:disconnect)
38
+ @imap_imap.should_receive(:disconnect)
36
39
 
37
40
  @imap.close
38
41
  end
39
42
 
40
43
  it "should logout from IMAP" do
41
- @imap.imap.stub(:disconnect)
44
+ @imap_imap.stub(:disconnect)
42
45
 
43
- @imap.imap.should_receive(:logout)
46
+ @imap_imap.should_receive(:logout)
44
47
 
45
48
  @imap.close
46
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redimap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tiredpixel
@@ -87,8 +87,8 @@ files:
87
87
  - Rakefile
88
88
  - bin/redimap
89
89
  - lib/redimap.rb
90
+ - lib/redimap/base.rb
90
91
  - lib/redimap/config.rb
91
- - lib/redimap/core.rb
92
92
  - lib/redimap/imap_conn.rb
93
93
  - lib/redimap/redis_conn.rb
94
94
  - lib/redimap/version.rb