restaurant 0.1.3 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7b71b7b9e2f1badd422562dbd949f97726687e86
4
- data.tar.gz: a062a4d399caec20cc4632c4b18944202b8d1fda
3
+ metadata.gz: 3c1d430a1159169aec6aa667513e2d6f642f6746
4
+ data.tar.gz: e70ce1d3a20d05b25152c8318a93855b400f5f78
5
5
  SHA512:
6
- metadata.gz: 9f2b51e1b30de2786cb3ce4578073b6832a4e21863e84caf0e96886f65bd8eda8b42c7c980db443c5f6d8bde9ed69696d36a9437548d53f6b7800ddf5ab724b3
7
- data.tar.gz: 04dbbaa38de272c411809c1a4d887feb24fe5b6a7fb0f4b80c62b2cb5b2148ba043bacedbaeb2e9ff6ca36ae5a84ce44241ca8b67eb7a651c0e6efd5b02a6e3a
6
+ metadata.gz: 673a81a243eae127b1dfd786ce369cc00df6454d8935657a2322e11c9dd0583071efbe7da23c5ae66706b683031cc3141d6ce6c00085fd778b056264bdabe068
7
+ data.tar.gz: a7751b9eca273868f3cbf249274d86a379c1a4e1b2a16623302513cbb215b8cf360104e9e50f9dcab56986ccdc12f8db46e7bb456fbfeba6b778792aace49812
data/README.md CHANGED
@@ -54,16 +54,20 @@ While Restaurant automagically defines what RESTful API needs, you can do them o
54
54
  # config/routes.rb
55
55
  # 1. V1::ResourcesController < ApplicationController are defined if not defined
56
56
  # 2. The following routes are defined
57
- # GET /v1/:resources -> V1::ReosurcesController#index
58
- # GET /v1/:resources/:id -> V1::ResourcesController#show
59
- # POST /v1/:resources -> V1::ResourcesController#create
60
- # PUT /v1/:resources/:id -> V1::ResourcesController#update
61
- # DELETE /v1/:resources/:id -> V1::ResourcesController#destroy
57
+ # GET /v1/:resource -> V1::ReosurcesController#index
58
+ # GET /v1/:resource/:id -> V1::ResourcesController#show
59
+ # POST /v1/:resource -> V1::ResourcesController#create
60
+ # PUT /v1/:resource/:id -> V1::ResourcesController#update
61
+ # DELETE /v1/:resource/:id -> V1::ResourcesController#destroy
62
62
  namespace :v1 do
63
63
  Restaurant::Router.route(self)
64
64
  end
65
65
 
66
66
  # Or customize what you want (e.g. only provides Read API)
67
+ # 1. V2::ResourcesController < ApplicationController are defined if not defined
68
+ # 2. The following routes are defined
69
+ # GET /v2/:resource -> V1::ReosurcesController#index
70
+ # GET /v2/:resource/:id -> V1::ResourcesController#show
67
71
  namespace :v2 do
68
72
  scope ":resource" do
69
73
  controller :resources do
@@ -4,14 +4,16 @@ module Restaurant
4
4
  base.before_filter :require_valid_id, :require_resource, :only => [:show, :update, :destroy]
5
5
  base.before_filter :add_created_at, :only => :create
6
6
  base.before_filter :add_updated_at, :only => :update
7
+ base.after_filter :expire_resource_cache, :only => [:update, :destroy]
8
+ base.after_filter :update_resources_version_cache, :only => [:create, :update, :destroy]
7
9
  end
8
10
 
9
11
  def index
10
- respond_with collection.find(filter_params).sort(sort_params).skip(skip_params).limit(limit_params)
12
+ respond_with resources
11
13
  end
12
14
 
13
15
  def show
14
- respond_with @resource
16
+ respond_with resource
15
17
  end
16
18
 
17
19
  def create
@@ -34,13 +36,43 @@ module Restaurant
34
36
  end
35
37
 
36
38
  def require_resource
37
- @resource = collection.find(:_id => resource_id).first || head(404)
39
+ head 404 unless resource
38
40
  end
39
41
 
40
42
  def collection
41
43
  Mongoid.default_session[resources_name]
42
44
  end
43
45
 
46
+ def resource
47
+ @resource ||= collection.find(:_id => resource_id).first
48
+ end
49
+
50
+ def resources
51
+ collection.find(filter_params).sort(sort_params).skip(skip_params).limit(limit_params).to_a
52
+ end
53
+
54
+ def resources_with_cache
55
+ if cache_configured?
56
+ cache_store.fetch(resources_cache_key) do
57
+ resources_without_cache
58
+ end
59
+ else
60
+ resources_without_cache
61
+ end
62
+ end
63
+ alias_method_chain :resources, :cache
64
+
65
+ def resource_with_cache
66
+ if cache_configured?
67
+ cache_store.fetch(resource_cache_key) do
68
+ resource_without_cache
69
+ end
70
+ else
71
+ resource_without_cache
72
+ end
73
+ end
74
+ alias_method_chain :resource, :cache
75
+
44
76
  def resource_name
45
77
  resources_name.singularize
46
78
  end
@@ -90,5 +122,31 @@ module Restaurant
90
122
  def add_updated_at
91
123
  resource_params[:updated_at] = Time.now
92
124
  end
125
+
126
+ def expire_resource_cache
127
+ cache_store.delete(resource_cache_key) if cache_configured?
128
+ end
129
+
130
+ def resource_cache_key
131
+ params.slice(:resource, :id)
132
+ end
133
+
134
+ def resources_version_cache_key
135
+ { :resource => params[:resource] }
136
+ end
137
+
138
+ def update_resources_version_cache
139
+ if cache_configured?
140
+ cache_store.write(resources_version_cache_key, Time.now.to_f)
141
+ end
142
+ end
143
+
144
+ def resources_version
145
+ cache_store.read(resources_version_cache_key)
146
+ end
147
+
148
+ def resources_cache_key
149
+ params.slice(:resource, :filter, :sort, :page).merge(:version => resources_version)
150
+ end
93
151
  end
94
152
  end
@@ -1,3 +1,3 @@
1
1
  module Restaurant
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restaurant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-18 00:00:00.000000000 Z
11
+ date: 2013-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid