request_queue 0.1.0 → 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: 6a10f3af02e29a0ec1827add198e48d432d07995
4
- data.tar.gz: 871a34a4e00c631f4c7531cf4ae8371f8b0d5682
3
+ metadata.gz: 4b645c28623badcb0436259ca4400b1dad1c85d7
4
+ data.tar.gz: 318d9ec8bc059e9e3e08fedf761f58305622ebf5
5
5
  SHA512:
6
- metadata.gz: fd7018e8f2bda2e985ba37639e8f6a4f114a5b78c10420902e372a22e8b083e572618a19554f9be26a49d0b4217898438b64e93972ff6c2f245e3a09a2c339b1
7
- data.tar.gz: 98b22b6934c4a3a1e0847588fba47eef13596794a841ebc7bfb067b72a87428433e0ba33bf3b412eebf016d8ee15a16af32c1b47d0865f10d6f801b6873f76b8
6
+ metadata.gz: 0e3947b656877a088d25302444999e97562f47410b36a051452f1a9fa33a77a50d2cb7def7092ea6381c4dda6656376bab0ab8901b8785d9a3444da56847cafd
7
+ data.tar.gz: 946b80b4979423cae20fb8bf0b848511d7e6faea2e7b1320117b463ecc077d525937c8d389ef871aa9b3785e15d8178ea889b3342e81f6c6f249a4b8adae6ea4
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # RequestQueue
2
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/request_queue`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ This is a utility for deduping work on a per-request basis. It works like this:
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ 1. When the request starts, create a queue.
6
+ 2. Add some work to the queue by calling `RequestQueue.enqueue(some_job)`.
7
+ 3. Before the request ends, you'll have opportunity to dedupe the work.
6
8
 
7
9
  ## Installation
8
10
 
@@ -22,7 +24,79 @@ Or install it yourself as:
22
24
 
23
25
  ## Usage
24
26
 
25
- TODO: Write usage instructions here
27
+ A a job can be any callable, so for example:
28
+
29
+ ```
30
+ job = proc { puts 'Hello' }
31
+
32
+ RequestQueue.enqueue(job)
33
+ RequestQueue.enqueue(job)
34
+ ```
35
+
36
+ This would output 'Hello' once at the end of the request.
37
+
38
+ #### Using objects
39
+
40
+ Because `RequestQueue` accepts any callable, you can also use an object.
41
+
42
+ ```ruby
43
+ class Job
44
+ def initialize(message)
45
+ @message = message
46
+ end
47
+
48
+ def call
49
+ puts @message
50
+ end
51
+ end
52
+
53
+ job = Job.new('Hello')
54
+ RequestQueue.enqueue(job)
55
+ RequestQueue.enqueue(job)
56
+ ```
57
+
58
+ #### Advanced Deduping
59
+
60
+ Sometimes, you need a little more control over the deduping.
61
+
62
+ ```ruby
63
+ class Job
64
+ def self.filter(jobs)
65
+ jobs.uniq_by(&:message)
66
+ end
67
+
68
+ attr_reader :message
69
+
70
+ def initialize(message)
71
+ @message = message
72
+ end
73
+
74
+ def call
75
+ puts message
76
+ end
77
+ end
78
+
79
+ RequestQueue.enqueue Job.new('Hello')
80
+ RequestQueue.enqueue Job.new('Hello')
81
+ ```
82
+
83
+ In this example, the job instances are unique, so they wouldn't be automatically deduped. But, when the class of a message responds to `filter`, it will be called and give you the opportunity to provide custom dedupe logic.
84
+
85
+ ## Testing
86
+
87
+ When testing, sometimes you want a little more control. As such, `RequestQueue` offers multiple queue backends:
88
+
89
+ * `:default` - Runs jobs at the end of the request.
90
+ * `:fake` - Never runs jobs, and allows you to inspect the queue with `RequestQueue.queue.queue`.
91
+ * `:inline` - Runs jobs immediately and never adds them to the queue.
92
+
93
+ To change the queueing backend, you can just say `RequestQueue.use`:
94
+
95
+ ```ruby
96
+ RequestQueue.use :inline
97
+ ```
98
+
99
+ **NOTE:** Only use the `use` method in the testing environment. It is not thread-safe.
26
100
 
27
101
  ## Development
28
102
 
@@ -38,4 +112,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/Ray Za
38
112
  ## License
39
113
 
40
114
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
@@ -11,7 +11,7 @@ module RequestQueue
11
11
  end
12
12
 
13
13
  def process!
14
- filter.each(&:process)
14
+ filter.each(&:call)
15
15
  end
16
16
  alias process process!
17
17
 
@@ -1,3 +1,3 @@
1
1
  module RequestQueue
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: request_queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ray Zane