Module: Garner::Strategies::ETags::Grape

Defined in:
lib/garner/strategies/etags/grape_strategy.rb

Class Method Summary collapse

Class Method Details

.apply(object) ⇒ Object

This method is abstract.

Serialize in a way such that the ETag matches that which would be returned by Grape + Rack::ETag at response time.



9
10
11
12
# File 'lib/garner/strategies/etags/grape_strategy.rb', line 9

def apply(object)
  serialization = encode_json(object || "")
  %("#{Digest::MD5.hexdigest(serialization)}")
end

.encode_json(object) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/garner/strategies/etags/grape_strategy.rb', line 15

def encode_json(object)
  return object if object.is_a?(String)

  if object.respond_to? :serializable_hash
    MultiJson.dump(object.serializable_hash)
  elsif object.kind_of?(Array) && !object.map {|o| o.respond_to? :serializable_hash }.include?(false)
    MultiJson.dump(object.map {|o| o.serializable_hash })
  elsif object.respond_to? :to_json
    object.to_json
  else
    MultiJson.dump(object)
  end
end