Module: Blodsband::Riak::Response::InstanceMethods

Defined in:
lib/blodsband/riak/response.rb

Instance Method Summary collapse

Instance Method Details

#apply(status, headers, bucket, key) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/blodsband/riak/response.rb', line 157

def apply(status, headers, bucket, key)
  @bucket = bucket
  @key = key
  @status = status
  @vclock = headers["X-Riak-Vclock"]
  @last_modified = DateTime.parse(headers["Last-Modified"].to_s) rescue nil
  @meta = {}
  if headers.is_a?(Mail::Part)
    headers.header_fields.each do |field|
      if match = field.name.match(/^X-Riak-Meta-(.*)$/)
        @meta[match[1].downcase] = field.value.to_s
      end
    end
  else 
    headers.each do |key, value|
      if match = key.to_s.match(/^X_RIAK_META_(.*)$/)
        @meta[match[1].gsub(/_/, "-").downcase] = value.to_s
      end
    end
  end
  link_header = headers["Link"]
  if link_header.nil?
    @links = []
  else
    @links = link_header.to_s.split(/,/).inject({}) do |sum, link|
      parts = link.split(/;/)
      if parts.size > 0 && keymatch = parts.shift.match(/<\/buckets\/(.*)\/keys\/(.*)>/)
        key = [keymatch[1], keymatch[2]]
        type = ""
        parts.each do |part|
          if match = part.match(/riaktag="(.*)"/)
            type = match[1]
          end
        end
        sum[type.strip] ||= []
        sum[type.strip] << key
      end
      sum
    end
  end
end

#copy_to(other, options = {}) ⇒ Object

Copies the response-specific parts of this Blodsband::Riak::Response to other after having made other include Blodsband::Riak::Response.

Does not copy links or meta since they are value specific.

Parameters:

  • other (Object)

    what we want to copy our response data to.

  • options (Hash) (defaults to: {})
    :except
    Array<:status, :vclock, :last_modified, :key, :bucket>

    an array of data not to copy.

Returns:



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/blodsband/riak/response.rb', line 141

def copy_to(other, options = {})
  except = options.delete(:except) || []
  raise "Unknown option to #copy_to: #{options.inspect}" unless options.empty?
  class << other
    include Response
  end
  response = self
  other.instance_eval do
    @status = response.status unless except.include?(:status)
    @vclock = response.vclock unless except.include?(:vclock)
    @last_modified = response.last_modified unless except.include?(:last_modified)
    @key = response.key unless except.include?(:key)
    @bucket = response.bucket unless except.include?(:bucket)
  end
  other
end