granola 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -9
- data/lib/granola/helper.rb +2 -1
- data/lib/granola/rack.rb +10 -10
- data/lib/granola/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d181cf15a32af62964d9d54b21589ed2bfaffcf
|
4
|
+
data.tar.gz: 2fe3d728942d5e4dbf68b92f80c49349bed8c10d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 984bc4b6f1f0a38dc57be7c2a40ba3f144d18adc40179c6eaee1dda7848ffae42582d4c36dbeae81988b9bb6622c115c8cbff654f2e91f3053de20a7b3d038ce
|
7
|
+
data.tar.gz: 892e590017554fad8a5e5bc375d23ecb67220d1b7261fcf37fdf5a3de3793911acc642c74881c5cbc1160663af1e1c7e5115066c92b7de6cbdccbe1da2723753
|
data/README.md
CHANGED
@@ -51,22 +51,33 @@ serializer.to_json #=> '[{"name":"John Doe",...},{...}]'
|
|
51
51
|
|
52
52
|
## Rack Helpers
|
53
53
|
|
54
|
-
If your application is based on
|
55
|
-
|
56
|
-
you can simply `include Granola::Rack` and you get access to the following
|
57
|
-
interface:
|
54
|
+
If your application is based on Rack, you can simply `include Granola::Rack` and
|
55
|
+
you get access to the following interface:
|
58
56
|
|
59
57
|
``` ruby
|
60
58
|
json(person) #=> This will try to infer PersonSerializer from a Person instance
|
61
59
|
json(person, with: AnotherSerializer)
|
62
60
|
```
|
63
61
|
|
64
|
-
*NOTE
|
65
|
-
|
66
|
-
|
62
|
+
*NOTE* The method relies on being an `env` method that returns the Rack
|
63
|
+
environment Hash in the same context where you call the method. See [the
|
64
|
+
documentation](./lib/granola/rack.rb) for further details.
|
67
65
|
|
68
|
-
|
69
|
-
[
|
66
|
+
This method returns a Rack response tuple that you can use like so (this example
|
67
|
+
uses [Cuba][], but similar code will work for other frameworks):
|
68
|
+
|
69
|
+
``` ruby
|
70
|
+
Cuba.plugin Granola::Rack
|
71
|
+
|
72
|
+
Cuba.define do
|
73
|
+
on get, "users/:id" do |id|
|
74
|
+
user = User[id]
|
75
|
+
halt json(user)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
[Cuba]: http://cuba.is
|
70
81
|
|
71
82
|
## Caching
|
72
83
|
|
data/lib/granola/helper.rb
CHANGED
@@ -10,12 +10,13 @@ module Granola::Helper
|
|
10
10
|
# Keywords
|
11
11
|
# with: A specific serializer class to use. If this is `nil`,
|
12
12
|
# `Helper.serializer_class_for` will be used to infer the serializer
|
13
|
-
# class.
|
13
|
+
# class. This is ignored if `object` is already a Granola::Serializer.
|
14
14
|
#
|
15
15
|
# Raises NameError if no specific serializer is provided and we fail to infer
|
16
16
|
# one for this object.
|
17
17
|
# Returns an instance of a Granola::Serializer subclass.
|
18
18
|
def serializer_for(object, with: nil)
|
19
|
+
return object if Granola::Serializer === object
|
19
20
|
serializer_class = with || Granola::Helper.serializer_class_for(object)
|
20
21
|
method = object.respond_to?(:to_ary) ? :list : :new
|
21
22
|
serializer_class.send(method, object)
|
data/lib/granola/rack.rb
CHANGED
@@ -16,9 +16,8 @@ module Granola::Rack
|
|
16
16
|
# `ETag` headers if appropriate, and of controlling whether the object should
|
17
17
|
# be rendered at all or not (issuing a 304 response in this case).
|
18
18
|
#
|
19
|
-
# This expects the class mixing in this module to implement
|
20
|
-
#
|
21
|
-
# environment hash.
|
19
|
+
# This expects the class mixing in this module to implement an `env` method,
|
20
|
+
# that should be a Rack environment Hash.
|
22
21
|
#
|
23
22
|
# object - An object to serialize into JSON.
|
24
23
|
#
|
@@ -31,16 +30,17 @@ module Granola::Rack
|
|
31
30
|
#
|
32
31
|
# Raises NameError if no specific serializer is provided and we fail to infer
|
33
32
|
# one for this object.
|
34
|
-
# Returns
|
33
|
+
# Returns a Rack response tuple.
|
35
34
|
def json(object, with: nil, **json_options)
|
36
35
|
serializer = serializer_for(object, with: with)
|
36
|
+
headers = {}
|
37
37
|
|
38
38
|
if serializer.last_modified
|
39
|
-
|
39
|
+
headers["Last-Modified".freeze] = serializer.last_modified.httpdate
|
40
40
|
end
|
41
41
|
|
42
42
|
if serializer.cache_key
|
43
|
-
|
43
|
+
headers["ETag".freeze] = Digest::MD5.hexdigest(serializer.cache_key)
|
44
44
|
end
|
45
45
|
|
46
46
|
stale_check = StaleCheck.new(
|
@@ -48,12 +48,12 @@ module Granola::Rack
|
|
48
48
|
)
|
49
49
|
|
50
50
|
if stale_check.fresh?
|
51
|
-
|
51
|
+
[304, headers, []]
|
52
52
|
else
|
53
53
|
json_string = serializer.to_json(json_options)
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
headers["Content-Type".freeze] = serializer.mime_type
|
55
|
+
headers["Content-Length".freeze] = json_string.length.to_s
|
56
|
+
[200, headers, [json_string]]
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
data/lib/granola/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: granola
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicolas Sanguinetti
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multi_json
|