virginia 0.3.0 → 0.4.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 +4 -4
- data/.travis.yml +12 -0
- data/CHANGELOG.md +3 -0
- data/README.md +30 -0
- data/lib/virginia/document_cache.rb +93 -0
- data/lib/virginia/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 07e0d2abf4237b76b1f56162e819f4fd997d811b
|
4
|
+
data.tar.gz: ebd38ebbd3ffb59194c3d5c89ba60be8f66b5241
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2edaea0ae04deae86d67912d3169c3d502b6dc9c1cb85b436065c39eb6d06d9ae105d18b6d5eecd1ee0b9c2236d5a262cec16e171dcf0a33b80259396611e0b9
|
7
|
+
data.tar.gz: b2f943bda56a7a176448eb6c9e64e66ef2d027bcc97a8baf80736629a96e49eb37bd17cc1d62e66274d5951c38b8479103e426cac1506838e6186b2fc34c0b89
|
data/.travis.yml
ADDED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -51,6 +51,36 @@ run Sinatra::Application
|
|
51
51
|
|
52
52
|
Start Adhearsion. You should be able to visit `http://localhost:8080/` in your browser and see "Hello world!"
|
53
53
|
|
54
|
+
## Using the built-in document cache
|
55
|
+
|
56
|
+
Assuming you are using Sinatra like in the above examples, put this in your `lib/sinatra_app.rb`:
|
57
|
+
|
58
|
+
```Ruby
|
59
|
+
require 'sinatra'
|
60
|
+
require 'virginia/document_cache'
|
61
|
+
|
62
|
+
get '/documents/:id' do
|
63
|
+
begin
|
64
|
+
grammar = Virginia::DocumentCache.fetch params[:id]
|
65
|
+
rescue Virginia::DocumentCache::NotFound
|
66
|
+
raise Sinatra::NotFound
|
67
|
+
end
|
68
|
+
|
69
|
+
grammar.to_s
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
Then, to store a document in the cache, do this in your CallController:
|
74
|
+
|
75
|
+
```Ruby
|
76
|
+
cache_id = Virginia::DocumentCache.store 'Hello World!'
|
77
|
+
virginia_config = Adhearsion.config.virginia
|
78
|
+
logger.info "Document has been cached to http://#{virginia_config.host}:#{virginia_config.port}/documents/#{cache_id}"
|
79
|
+
```
|
80
|
+
|
81
|
+
You should be able to retrieve the document at the logged URL.
|
82
|
+
|
83
|
+
|
54
84
|
### Contributors
|
55
85
|
|
56
86
|
Original author: [Luca Pradovera](https://github.com/polysics)
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'singleton'
|
3
|
+
|
4
|
+
module Virginia
|
5
|
+
class DocumentCache
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
NotFound = Class.new StandardError
|
9
|
+
|
10
|
+
attr_reader :mutex
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def method_missing(m, *args, &block)
|
14
|
+
instance.mutex.synchronize do
|
15
|
+
instance.send m, *args, &block
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize
|
21
|
+
@mutex = Mutex.new
|
22
|
+
@documents = {}
|
23
|
+
|
24
|
+
supervisor = Housekeeping.supervise_as :document_cache_housekeeping
|
25
|
+
Adhearsion::Events.register_callback :shutdown do
|
26
|
+
supervisor.terminate
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Registers a new document with the cache
|
31
|
+
# @param [Object] document The document to be stored in the cache
|
32
|
+
# @param [Fixnum, Nil] lifetime The amount of time in seconds the document should be kept. If nil, document will be kept indefinitely.
|
33
|
+
# @param [String, Nil] id The ID to use to store the document. If nil, one will be generated.
|
34
|
+
# @return [String] ID of the stored document
|
35
|
+
def store(document, lifetime = 10, id = nil)
|
36
|
+
id ||= generate_id
|
37
|
+
@documents[id] = {
|
38
|
+
document: document,
|
39
|
+
expires: lifetime ? Time.now + lifetime : nil
|
40
|
+
}
|
41
|
+
id
|
42
|
+
end
|
43
|
+
|
44
|
+
# Deletes a document from the cache
|
45
|
+
# @param [Object] id ID of the document to be removed from the cache
|
46
|
+
# @return [Object, Nil] document Returns the document if found in the cache, nil otherwise
|
47
|
+
def delete(id)
|
48
|
+
data = @documents.delete id
|
49
|
+
data[:document]
|
50
|
+
end
|
51
|
+
|
52
|
+
# Retrieves a document from the cache
|
53
|
+
# @param [String] id ID of the document to be retrieved from the cache
|
54
|
+
# @param [Fixnum, Nil] lifetime The amount of time in seconds the document should be kept. If nil, document will be kept indefinitely.
|
55
|
+
# @yield If given, will be used to generate the document, store it, and then return.
|
56
|
+
# @return [Object] document Returns the document if found in the cache
|
57
|
+
# @raises [NotFound] If the document is not found in the cache
|
58
|
+
def fetch(id, lifetime = 10)
|
59
|
+
unless @documents.has_key? id
|
60
|
+
if block_given?
|
61
|
+
store yield, lifetime, id
|
62
|
+
else
|
63
|
+
raise NotFound
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
@documents[id][:document]
|
68
|
+
end
|
69
|
+
|
70
|
+
def reap_expired!
|
71
|
+
@documents.each_pair do |id, data|
|
72
|
+
@documents.delete(id) if data[:expires] < Time.now
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class Housekeeping
|
77
|
+
include Celluloid
|
78
|
+
|
79
|
+
def initialize
|
80
|
+
every(1.minute) do
|
81
|
+
logger.debug "Reaping expired cached document"
|
82
|
+
DocumentCache.reap_expired!
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def generate_id
|
90
|
+
SecureRandom.hex(16)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
data/lib/virginia/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: virginia
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luca Pradovera
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: adhearsion
|
@@ -131,6 +131,7 @@ extensions: []
|
|
131
131
|
extra_rdoc_files: []
|
132
132
|
files:
|
133
133
|
- ".gitignore"
|
134
|
+
- ".travis.yml"
|
134
135
|
- CHANGELOG.md
|
135
136
|
- Gemfile
|
136
137
|
- Guardfile
|
@@ -138,6 +139,7 @@ files:
|
|
138
139
|
- README.md
|
139
140
|
- Rakefile
|
140
141
|
- lib/virginia.rb
|
142
|
+
- lib/virginia/document_cache.rb
|
141
143
|
- lib/virginia/plugin.rb
|
142
144
|
- lib/virginia/service.rb
|
143
145
|
- lib/virginia/version.rb
|
@@ -166,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
166
168
|
version: '0'
|
167
169
|
requirements: []
|
168
170
|
rubyforge_project: virginia
|
169
|
-
rubygems_version: 2.
|
171
|
+
rubygems_version: 2.4.6
|
170
172
|
signing_key:
|
171
173
|
specification_version: 4
|
172
174
|
summary: A Reel interface to Adhearsion
|