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

#load_object(robject, response) ⇒ Object

Load object data from an HTTP response

Parameters:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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, "content-type", :content_type)
  extract_header(robject, response, "x-riak-vclock", :vclock)
  extract_header(robject, response, "link", :links) {|v| Set.new(Link.parse(v)) }
  extract_header(robject, response, "etag", :etag)
  extract_header(robject, response, "last-modified", :last_modified) {|v| Time.httpdate(v) }
  robject.meta = response[:headers].inject({}) do |h,(k,v)|
    if k =~ /x-riak-meta-(.*)/i
      h[$1] = v
    end
    h
  end
  robject.indexes = response[:headers].inject(Hash.new {|h,k| h[k] = Set.new }) do |h,(k,v)|
    if k =~ /x-riak-index-((?:.*)_(?:int|bin))$/i
      key = $1
      h[key].merge Array(v).map {|vals| vals.split(/,\s*/).map {|i| key =~ /int$/ ? i.to_i : i } }.flatten
    end
    h
  end
  robject.conflict = (response[:code] && response[:code].to_i == 300 && robject.content_type =~ /multipart\/mixed/)
  robject.siblings = robject.conflict? ? extract_siblings(robject, response[:body]) : nil
  robject.raw_data = response[:body] if response[:body].present? && !robject.conflict?

  robject.conflict? ? robject.attempt_conflict_resolution : robject
end

#reload_headers(robject) ⇒ Hash

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

#store_headers(robject) ⇒ Hash

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