Method: Fog::Storage::Google::Mock#get_object

Defined in:
lib/fog/google/requests/storage/get_object.rb

#get_object(bucket_name, object_name, options = {}, &block) ⇒ Object


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fog/google/requests/storage/get_object.rb', line 65

def get_object(bucket_name, object_name, options = {}, &block)
  unless bucket_name
    raise ArgumentError.new('bucket_name is required')
  end
  unless object_name
    raise ArgumentError.new('object_name is required')
  end
  response = Excon::Response.new
  if (bucket = self.data[:buckets][bucket_name]) && (object = bucket[:objects][object_name])
    if options['If-Match'] && options['If-Match'] != object['ETag']
      response.status = 412
    elsif options['If-Modified-Since'] && options['If-Modified-Since'] > Time.parse(object['Last-Modified'])
      response.status = 304
    elsif options['If-None-Match'] && options['If-None-Match'] == object['ETag']
      response.status = 304
    elsif options['If-Unmodified-Since'] && options['If-Unmodified-Since'] < Time.parse(object['Last-Modified'])
      response.status = 412
    else
      response.status = 200
      for key, value in object
        case key
        when 'Cache-Control', 'Content-Disposition', 'Content-Encoding', 'Content-Length', 'Content-MD5', 'Content-Type', 'ETag', 'Expires', 'Last-Modified', /^x-goog-meta-/
          response.headers[key] = value
        end
      end
      unless block_given?
        response.body = object[:body]
      else
        data = StringIO.new(object[:body])
        remaining = data.length
        while remaining > 0
          chunk = data.read([remaining, Excon::CHUNK_SIZE].min)
          block.call(chunk)
          remaining -= Excon::CHUNK_SIZE
        end
      end
    end
  else
    response.status = 404
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end