Parent

Included Modules

Class/Module Index [+]

Quicksearch

Fog::Storage::Google::Mock

Public Class Methods

acls(type) click to toggle source
# File lib/fog/google/storage.rb, line 69
def self.acls(type)
  case type
  when 'private'
    {
      "AccessControlList"=> [
        {
          "Permission" => "FULL_CONTROL",
          "Scope" => {"ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0", "type" => "UserById"}
        }
      ],
      "Owner" => {"ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
    }
  when 'public-read'
    {
      "AccessControlList"=> [
        {
          "Permission" => "FULL_CONTROL",
          "Scope" => {"ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0", "type" => "UserById"}
        },
        {
          "Permission" => "READ",
          "Scope" => {"type" => "AllUsers"}
        }
      ],
      "Owner" => {"ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
    }
  when 'public-read-write'
    {
      "AccessControlList"=> [
        {
          "Permission" => "FULL_CONTROL",
          "Scope" => {"ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0", "type" => "UserById"}
        },
        {
          "Permission" => "READ",
          "Scope" => {"type" => "AllUsers"}
        },
        {
          "Permission" => "WRITE",
          "Scope" => {"type" => "AllUsers"}
        }
      ],
      "Owner" => {"ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
    }
  when 'authenticated-read'
    {
      "AccessControlList"=> [
        {
          "Permission" => "FULL_CONTROL",
          "Scope" => {"ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0", "type" => "UserById"}
        },
        {
          "Permission" => "READ",
          "Scope" => {"type" => "AllAuthenticatedUsers"}
        }
      ],
      "Owner" => {"ID" => "2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0"}
    }
  end
end
data() click to toggle source
# File lib/fog/google/storage.rb, line 130
def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :acls => {
        :bucket => {},
        :object => {}
      },
      :buckets => {}
    }
  end
end
new(options={}) click to toggle source
# File lib/fog/google/storage.rb, line 146
def initialize(options={})
  require 'mime/types'
  @google_storage_access_key_id = options[:google_storage_access_key_id]
end
reset() click to toggle source
# File lib/fog/google/storage.rb, line 142
def self.reset
  @data = nil
end

Public Instance Methods

copy_object(source_bucket_name, source_object_name, target_bucket_name, target_object_name, options = {}) click to toggle source
# File lib/fog/google/requests/storage/copy_object.rb, line 44
def copy_object(source_bucket_name, source_object_name, target_bucket_name, target_object_name, options = {})
  response = Excon::Response.new
  source_bucket = self.data[:buckets][source_bucket_name]
  source_object = source_bucket && source_bucket[:objects][source_object_name]
  target_bucket = self.data[:buckets][target_bucket_name]

  if source_object && target_bucket
    response.status = 200
    target_object = source_object.dup
    target_object.merge!({
      'Name' => target_object_name
    })
    target_bucket[:objects][target_object_name] = target_object
    response.body = {
      'ETag'          => target_object['ETag'],
      'LastModified'  => Time.parse(target_object['Last-Modified'])
    }
  else
    response.status = 404
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end

  response
end
data() click to toggle source
# File lib/fog/google/storage.rb, line 151
def data
  self.class.data[@google_storage_access_key_id]
end
delete_bucket(bucket_name) click to toggle source
# File lib/fog/google/requests/storage/delete_bucket.rb, line 27
def delete_bucket(bucket_name)
  response = Excon::Response.new
  if self.data[:buckets][bucket_name].nil?
    response.status = 404
    raise(Excon::Errors.status_error({:expects => 204}, response))
  elsif self.data[:buckets][bucket_name] && !self.data[:buckets][bucket_name][:objects].empty?
    response.status = 409
    raise(Excon::Errors.status_error({:expects => 204}, response))
  else
    self.data[:buckets].delete(bucket_name)
    response.status = 204
  end
  response
end
delete_object(bucket_name, object_name) click to toggle source
# File lib/fog/google/requests/storage/delete_object.rb, line 30
def delete_object(bucket_name, object_name)
  response = Excon::Response.new
  if bucket = self.data[:buckets][bucket_name]
    if object = bucket[:objects][object_name]
      response.status = 204
      bucket[:objects].delete(object_name)
    else
      response.status = 404
      raise(Excon::Errors.status_error({:expects => 204}, response))
    end
  else
    response.status = 404
    raise(Excon::Errors.status_error({:expects => 204}, response))
  end
  response
end
get_bucket(bucket_name, options = {}) click to toggle source
# File lib/fog/google/requests/storage/get_bucket.rb, line 59
def get_bucket(bucket_name, options = {})
  unless bucket_name
    raise ArgumentError.new('bucket_name is required')
  end
  response = Excon::Response.new
  name = /(\w+\.?)*/.match(bucket_name)
  if bucket_name == name.to_s
    if bucket = self.data[:buckets][bucket_name]
      contents = bucket[:objects].values.sort {|x,y| x['Key'] <=> y['Key']}.reject do |object|
          (options['prefix'] && object['Key'][0...options['prefix'].length] != options['prefix']) ||
          (options['marker'] && object['Key'] <= options['marker'])
        end.map do |object|
          data = object.reject {|key, value| !['ETag', 'Key'].include?(key)}
          data.merge!({
            'LastModified' => Time.parse(object['Last-Modified']),
            'Owner'        => bucket['Owner'],
            'Size'         => object['Content-Length'].to_i
          })
        data
      end
      max_keys = options['max-keys'] || 1000
      size = [max_keys, 1000].min
      truncated_contents = contents[0...size]

      response.status = 200
      response.body = {
        'CommonPrefixes'  => [],
        'Contents'        => truncated_contents,
        'IsTruncated'     => truncated_contents.size != contents.size,
        'Marker'          => options['marker'],
        'Name'            => bucket['Name'],
        'Prefix'          => options['prefix']
      }
      if options['max-keys'] && options['max-keys'] < response.body['Contents'].length
          response.body['IsTruncated'] = true
          response.body['Contents'] = response.body['Contents'][0...options['max-keys']]
      end
    else
      response.status = 404
      raise(Excon::Errors.status_error({:expects => 200}, response))
    end
  else
      response.status = 400
      raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end
get_object(bucket_name, object_name, options = {}, &block) click to toggle source
# 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
get_object_acl(bucket_name, object_name) click to toggle source
# File lib/fog/google/requests/storage/get_object_acl.rb, line 59
def get_object_acl(bucket_name, object_name)
  response = Excon::Response.new
  if acl = self.data[:acls][:object][bucket_name] && self.data[:acls][:object][bucket_name][object_name]
    response.status = 200
    response.body = acl
  else
    response.status = 404
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end
get_service() click to toggle source
# File lib/fog/google/requests/storage/get_service.rb, line 35
def get_service
  response = Excon::Response.new
  response.headers['Status'] = 200
  buckets = self.data[:buckets].values.map do |bucket|
    bucket.reject do |key, value|
      !['CreationDate', 'Name'].include?(key)
    end
  end
  response.body = {
    'Buckets' => buckets,
    'Owner'   => { 'ID' => 'some_id'}
  }
  response
end
head_object(bucket_name, object_name, options = {}) click to toggle source
# File lib/fog/google/requests/storage/head_object.rb, line 55
def head_object(bucket_name, object_name, options = {})
  response = get_object(bucket_name, object_name, options)
  response.body = nil
  response
end
put_bucket(bucket_name, options = {}) click to toggle source
# File lib/fog/google/requests/storage/put_bucket.rb, line 42
def put_bucket(bucket_name, options = {})
  acl = options['x-goog-acl'] || 'private'
  if !['private', 'public-read', 'public-read-write', 'authenticated-read'].include?(acl)
    raise Excon::Errors::BadRequest.new('invalid x-goog-acl')
  else
    self.data[:acls][:bucket][bucket_name] = self.class.acls(options[acl])
  end
  response = Excon::Response.new
  response.status = 200
  bucket = {
    :objects        => {},
    'Name'          => bucket_name,
    'CreationDate'  => Time.now,
    'Owner'         => { 'DisplayName' => 'owner', 'ID' => 'some_id'},
    'Payer'         => 'BucketOwner'
  }
  if options['LocationConstraint']
    bucket['LocationConstraint'] = options['LocationConstraint']
  else
    bucket['LocationConstraint'] = ''
  end
  if self.data[:buckets][bucket_name].nil?
    self.data[:buckets][bucket_name] = bucket
  else
    response.status = 409
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end
put_object(bucket_name, object_name, data, options = {}) click to toggle source
# File lib/fog/google/requests/storage/put_object.rb, line 44
def put_object(bucket_name, object_name, data, options = {})
  acl = options['x-goog-acl'] || 'private'
  if !['private', 'public-read', 'public-read-write', 'authenticated-read'].include?(acl)
    raise Excon::Errors::BadRequest.new('invalid x-goog-acl')
  else
    self.data[:acls][:object][bucket_name] ||= {}
    self.data[:acls][:object][bucket_name][object_name] = self.class.acls(acl)
  end

  data = Fog::Storage.parse_data(data)
  unless data[:body].is_a?(String)
    data[:body] = data[:body].read
  end
  response = Excon::Response.new
  if (bucket = self.data[:buckets][bucket_name])
    response.status = 200
    object = {
      :body             => data[:body],
      'Content-Type'    => options['Content-Type'] || data[:headers]['Content-Type'],
      'ETag'            => Fog::Google::Mock.etag,
      'Key'             => object_name,
      'Last-Modified'   => Fog::Time.now.to_date_header,
      'Content-Length'  => options['Content-Length'] || data[:headers]['Content-Length']
    }

    for key, value in options
      case key
      when 'Cache-Control', 'Content-Disposition', 'Content-Encoding', 'Content-MD5', 'Expires', /^x-goog-meta-/
        object[key] = value
      end
    end

    bucket[:objects][object_name] = object
    response.headers = {
      'Content-Length'  => object['Content-Length'],
      'Content-Type'    => object['Content-Type'],
      'ETag'            => object['ETag'],
      'Last-Modified'   => object['Last-Modified']
    }
  else
    response.status = 404
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end
put_object_url(bucket_name, object_name, expires, headers = {}) click to toggle source
# File lib/fog/google/requests/storage/put_object_url.rb, line 36
def put_object_url(bucket_name, object_name, expires, headers = {})
  unless bucket_name
    raise ArgumentError.new('bucket_name is required')
  end
  unless object_name
    raise ArgumentError.new('object_name is required')
  end
  https_url({
    :headers  => headers,
    :host     => @host,
    :method   => 'PUT',
    :path     => "#{bucket_name}/#{object_name}"
  }, expires)
end
reset_data() click to toggle source
# File lib/fog/google/storage.rb, line 155
def reset_data
  self.class.data.delete(@google_storage_access_key_id)
end
signature(params) click to toggle source
# File lib/fog/google/storage.rb, line 159
def signature(params)
  "foo"
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.