Module: Riak::Client::HTTPBackend::ObjectMethods

Included in:
Riak::Client::HTTPBackend
Defined in:
lib/riak/client/http_backend/object_methods.rb

Overview

Methods for assisting with the handling of RObjects present in HTTP requests and responses.

Instance Method Summary (collapse)

Instance Method Details

- (Object) load_object(robject, response)

Load object data from an HTTP response

Parameters:



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/riak/client/http_backend/object_methods.rb', line 53

def load_object(robject, response)
  extract_header(robject, response, "location", :key) {|v| URI.unescape(v.match(%r{.*/(.*?)(\?.*)?$})[1]) }
  extract_header(robject, response, "x-riak-vclock", :vclock)
  case response[:code] && response[:code].to_i
  when 304
    # Resulted from a reload, don't modify anything
  when 300
    robject.siblings = extract_siblings(robject, response[:body], response[:headers]['content-type'].first)
  else
    robject.siblings = [ load_content(response, RContent.new(robject)) ]
  end
  robject.conflict? ? robject.attempt_conflict_resolution : robject
end

- (Hash) reload_headers(robject)

HTTP header hash that will be sent along when reloading the object

Returns:

  • (Hash)

    hash of HTTP headers



17
18
19
20
21
22
# File 'lib/riak/client/http_backend/object_methods.rb', line 17

def reload_headers(robject)
  {}.tap do |h|
    h['If-None-Match'] = robject.etag if robject.etag.present?
    h['If-Modified-Since'] = robject.last_modified.httpdate if robject.last_modified.present?
  end
end

- (Hash) store_headers(robject)

HTTP header hash that will be sent along when storing the object

Returns:

  • (Hash)

    hash of HTTP Headers



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/riak/client/http_backend/object_methods.rb', line 26

def store_headers(robject)
  {}.tap do |hash|
    hash["Content-Type"] = robject.content_type
    hash["X-Riak-Vclock"] = robject.vclock if robject.vclock
    if robject.prevent_stale_writes && robject.etag.present?
      hash["If-Match"] = robject.etag
    elsif robject.prevent_stale_writes
      hash["If-None-Match"] = "*"
    end
    unless robject.links.blank?
      hash["Link"] = robject.links.reject {|l| l.rel == "up" }.map {|l| l.to_s(new_scheme?) }.join(", ")
    end
    unless robject.meta.blank?
      robject.meta.each do |k,v|
        hash["X-Riak-Meta-#{k}"] = v.to_s
      end
    end
    unless robject.indexes.blank?
      robject.indexes.each do |k,v|
        hash["X-Riak-Index-#{k}"] = v.to_a.sort.map {|i| i.to_s }.join(", ")
      end
    end
  end
end