virginia 0.3.0 → 0.4.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: 03c11488de9354f94fb4fddaafc9eea6edb2ea2a
4
- data.tar.gz: ef033861cbc75a27ab882d791609966120516376
3
+ metadata.gz: 07e0d2abf4237b76b1f56162e819f4fd997d811b
4
+ data.tar.gz: ebd38ebbd3ffb59194c3d5c89ba60be8f66b5241
5
5
  SHA512:
6
- metadata.gz: 45abb469c15e4dff5e26d2ea886ebe0c147ed404292db70de9466cc4f7fa656d2d4227712e518cde954f8417c924862d7acfa5e689ebd8bdf7133d8eed81d54f
7
- data.tar.gz: fa7668f83f4194cf48c618337a17201866bb060580efb4ab4113c0882859a6fffe83d7e0c703d324e85f705a0e5c1688c2c85e43085411b5d0508c89e3c3cec9
6
+ metadata.gz: 2edaea0ae04deae86d67912d3169c3d502b6dc9c1cb85b436065c39eb6d06d9ae105d18b6d5eecd1ee0b9c2236d5a262cec16e171dcf0a33b80259396611e0b9
7
+ data.tar.gz: b2f943bda56a7a176448eb6c9e64e66ef2d027bcc97a8baf80736629a96e49eb37bd17cc1d62e66274d5951c38b8479103e426cac1506838e6186b2fc34c0b89
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - jruby
7
+ - rbx-2.1.1
8
+ - ruby-head
9
+ matrix:
10
+ allow_failures:
11
+ - rvm: rbx-2.1.1
12
+ - rvm: ruby-head
@@ -1,3 +1,6 @@
1
+ # Version 0.4.0
2
+ * Add document cache
3
+
1
4
  # Version 0.3.0
2
5
  * Switch to Rackup-style app management
3
6
 
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
@@ -1,3 +1,3 @@
1
1
  module Virginia
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
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.3.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: 2014-10-31 00:00:00.000000000 Z
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.2.2
171
+ rubygems_version: 2.4.6
170
172
  signing_key:
171
173
  specification_version: 4
172
174
  summary: A Reel interface to Adhearsion