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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d89e36c1e6cf872c3e3fafff7ddabd364af6a20
4
- data.tar.gz: 292659322f51f57ba16ad7a47cda7aa366ce2362
3
+ metadata.gz: 0d181cf15a32af62964d9d54b21589ed2bfaffcf
4
+ data.tar.gz: 2fe3d728942d5e4dbf68b92f80c49349bed8c10d
5
5
  SHA512:
6
- metadata.gz: 6a5f68618ec9682f837db0a45b033bf850dc256875d3b26fcad3b187a26401bcd0bc569fc3512ab63e29781a7e2215838670de4695b31fbc568a3251276d761a
7
- data.tar.gz: e64ee844b50f5ee5b2081988406bf408f9b3bd51e35e406795b3c188b078c32a3a0b2dd8059df701471de60b9e094fa436f54b4fee87d2d787f50dbb4cc17f11
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 rack, you have a `Rack::Response` called `res`
55
- in your context, and you have an `env` method that returns the Rack env hash,
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*: This works out of the box in frameworks like [Cuba][] or [Roda][]. You
65
- might need to provide glue code to make `res` and/or `env` available in other
66
- frameworks.
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
- [Cuba]: https://github.com/soveran/cuba
69
- [Roda]: https://github.com/jeremyevans/roda
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
 
@@ -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 two methods:
20
- # `res`, that should be a Rack::Response, and `env`, that should be a Rack
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 an instance of a Granola::Serializer subclass.
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
- res["Last-Modified".freeze] = serializer.last_modified.httpdate
39
+ headers["Last-Modified".freeze] = serializer.last_modified.httpdate
40
40
  end
41
41
 
42
42
  if serializer.cache_key
43
- res["ETag".freeze] = Digest::MD5.hexdigest(serializer.cache_key)
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
- res.status = 304
51
+ [304, headers, []]
52
52
  else
53
53
  json_string = serializer.to_json(json_options)
54
- res["Content-Type".freeze] = serializer.mime_type
55
- res["Content-Length".freeze] = json_string.length.to_s
56
- res.write(json_string)
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
 
@@ -1,3 +1,3 @@
1
1
  module Granola
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
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.1
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-09-26 00:00:00.000000000 Z
11
+ date: 2014-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json