granola 0.0.2 → 0.0.3

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: 0d181cf15a32af62964d9d54b21589ed2bfaffcf
4
- data.tar.gz: 2fe3d728942d5e4dbf68b92f80c49349bed8c10d
3
+ metadata.gz: 61316a397f01835165e7961ce698174bbf8c3c26
4
+ data.tar.gz: 9287b8229f817fbe765051a4f3b066601740d6af
5
5
  SHA512:
6
- metadata.gz: 984bc4b6f1f0a38dc57be7c2a40ba3f144d18adc40179c6eaee1dda7848ffae42582d4c36dbeae81988b9bb6622c115c8cbff654f2e91f3053de20a7b3d038ce
7
- data.tar.gz: 892e590017554fad8a5e5bc375d23ecb67220d1b7261fcf37fdf5a3de3793911acc642c74881c5cbc1160663af1e1c7e5115066c92b7de6cbdccbe1da2723753
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
+ ![A tasty bowl of Granola](https://cloud.githubusercontent.com/assets/437/4827156/9e8d33da-5f76-11e4-8574-7803e84845f2.JPG)
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.
@@ -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
- const_get("#{object.class.name}Serializer")
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, last_modified: serializer.last_modified, etag: serializer.cache_key
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
- [200, headers, [json_string]]
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
- if_modified_since = Time.parse(env.fetch(IF_MODIFIED_SINCE))
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
@@ -1,3 +1,3 @@
1
1
  module Granola
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: granola
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Sanguinetti