granola 0.0.2 → 0.0.3
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/README.md +3 -0
- data/lib/granola/helper.rb +4 -1
- data/lib/granola/rack.rb +8 -5
- data/lib/granola/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61316a397f01835165e7961ce698174bbf8c3c26
|
4
|
+
data.tar.gz: 9287b8229f817fbe765051a4f3b066601740d6af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfeacf1ba025637fa153c3e437a22128a355d8d948b34c638aed537dd3b7d136fde490fcac0c1e87c2dd0faa1b056d2363855ed5cf28e2f1125183f91db92a84
|
7
|
+
data.tar.gz: c13007b46a27490727db0ceb9fbb5cc9ed7ca8ff246ce9115e4a913467c47f76952a7fd72ec470128c82107415d16f44b345339c669c3a4a600187c6811cdf41
|
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# Granola, a JSON serializer
|
2
2
|
|
3
|
+

|
4
|
+
|
5
|
+
|
3
6
|
Granola aims to provide a simple interface to generate JSON responses based on
|
4
7
|
your application's domain models. It doesn't make assumptions about anything and
|
5
8
|
gets out of your way. You just write plain ruby.
|
data/lib/granola/helper.rb
CHANGED
@@ -32,7 +32,10 @@ module Granola::Helper
|
|
32
32
|
# Returns a Class.
|
33
33
|
def self.serializer_class_for(object)
|
34
34
|
object = object.respond_to?(:to_ary) ? object.to_ary.fetch(0, nil) : object
|
35
|
-
|
35
|
+
name = object.class.name
|
36
|
+
Object.const_get("%sSerializer" % name)
|
37
|
+
rescue NameError
|
38
|
+
const_get("%sSerializer" % name)
|
36
39
|
end
|
37
40
|
|
38
41
|
# Internal: Null serializer that transparently handles rendering `nil` in case
|
data/lib/granola/rack.rb
CHANGED
@@ -25,13 +25,15 @@ module Granola::Rack
|
|
25
25
|
# with: A specific serializer class to use. If this is `nil`,
|
26
26
|
# `Helper.serializer_class_for` will be used to infer the
|
27
27
|
# serializer class.
|
28
|
+
# status: The HTTP status to return on stale responses. Defaults to
|
29
|
+
# `200`.
|
28
30
|
# **json_options: Any other keywords passed will be forwarded to the
|
29
31
|
# serializer's `#to_json` call.
|
30
32
|
#
|
31
33
|
# Raises NameError if no specific serializer is provided and we fail to infer
|
32
34
|
# one for this object.
|
33
35
|
# Returns a Rack response tuple.
|
34
|
-
def json(object, with: nil, **json_options)
|
36
|
+
def json(object, with: nil, status: 200, **json_options)
|
35
37
|
serializer = serializer_for(object, with: with)
|
36
38
|
headers = {}
|
37
39
|
|
@@ -44,7 +46,9 @@ module Granola::Rack
|
|
44
46
|
end
|
45
47
|
|
46
48
|
stale_check = StaleCheck.new(
|
47
|
-
env,
|
49
|
+
env,
|
50
|
+
last_modified: headers["Last-Modified".freeze],
|
51
|
+
etag: headers["ETag".freeze]
|
48
52
|
)
|
49
53
|
|
50
54
|
if stale_check.fresh?
|
@@ -53,7 +57,7 @@ module Granola::Rack
|
|
53
57
|
json_string = serializer.to_json(json_options)
|
54
58
|
headers["Content-Type".freeze] = serializer.mime_type
|
55
59
|
headers["Content-Length".freeze] = json_string.length.to_s
|
56
|
-
[
|
60
|
+
[status, headers, [json_string]]
|
57
61
|
end
|
58
62
|
end
|
59
63
|
|
@@ -109,8 +113,7 @@ module Granola::Rack
|
|
109
113
|
# Returns Boolean.
|
110
114
|
def fresh_by_time?
|
111
115
|
return false unless env.key?(IF_MODIFIED_SINCE) && !last_modified.nil?
|
112
|
-
|
113
|
-
last_modified <= if_modified_since
|
116
|
+
Time.parse(last_modified) <= Time.parse(env.fetch(IF_MODIFIED_SINCE))
|
114
117
|
end
|
115
118
|
|
116
119
|
# Internal: Checks if a request is fresh by etag, if applicable, by
|
data/lib/granola/version.rb
CHANGED