Class: GStore::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gstore/bucket.rb,
lib/gstore/client.rb,
lib/gstore/object.rb,
lib/gstore/request.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
# File 'lib/gstore/client.rb', line 8

def initialize(options = {})
  @access_key = options[:access_key]
  @secret_key = options[:secret_key]
  @debug = options[:debug] and options[:debug] == true
  @host = options[:host] || 'commondatastorage.googleapis.com'
end

Instance Method Details

#create_bucket(bucket, options = {}) ⇒ Object



8
9
10
# File 'lib/gstore/bucket.rb', line 8

def create_bucket(bucket, options={})
  put(bucket, '/', options)
end

#delete_bucket(bucket, options = {}) ⇒ Object



16
17
18
# File 'lib/gstore/bucket.rb', line 16

def delete_bucket(bucket, options={})
  delete(bucket, '/', options)
end

#delete_object(bucket, filename, options = {}) ⇒ Object



17
18
19
# File 'lib/gstore/object.rb', line 17

def delete_object(bucket, filename, options={})
  delete(bucket, "/#{filename}", options)
end

#get_bucket(bucket, options = {}) ⇒ Object



12
13
14
# File 'lib/gstore/bucket.rb', line 12

def get_bucket(bucket, options={})
  get(bucket, '/', options)
end

#get_object(bucket, filename, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/gstore/object.rb', line 7

def get_object(bucket, filename, options={})
  outfile = options.delete(:outfile)
  res = get(bucket, "/#{filename}", options)
  if outfile
    File.open(outfile, 'w') {|f| f.write(res) }
  else
    res
  end
end

#get_object_metadata(bucket, filename, options = {}) ⇒ Object



21
22
23
# File 'lib/gstore/object.rb', line 21

def (bucket, filename, options={})
  head(bucket, "/#{filename}", options)
end

#list_buckets(options = {}) ⇒ Object



4
5
6
# File 'lib/gstore/bucket.rb', line 4

def list_buckets(options={})
  get(nil, '/', options)
end

#put_object(bucket, filename, options = {}) ⇒ Object



3
4
5
# File 'lib/gstore/object.rb', line 3

def put_object(bucket, filename, options={})
  put(bucket, "/#{filename}", options)
end

#signed_request(method, host, path, params = {}, headers = {}, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gstore/request.rb', line 12

def signed_request(method, host, path, params={}, headers={}, options={})
  
  if @debug
    puts
    puts "***** METHOD: #{method}"
    puts "***** HOST: #{host}"
    puts "***** PATH: #{path}"
    puts "***** PARAMS: #{params.inspect}"
    puts "***** HEADERS: #{headers.inspect}"
    puts "***** OPTIONS: #{options.inspect}"
    puts
  end
  
  headers.merge!({
    :Host => host,
    :Date => Time.now.utc.strftime('%a, %d %b %Y %H:%M:%S -0000')
    #:"Content-MD5" => ''
  })
  
  if options[:data]
    headers = headers.merge(:"Content-Length" => options[:data].size)
  else
    headers = headers.merge(:"Content-Length" => 0) 
  end
  headers[:"Content-Type"] ||= 'text/plain'
  
  
  bucket = nil
  if host =~ /(\S+).#{@host}/
    bucket = $1
  end
  
  canonical_headers = method.name.gsub('Net::HTTP::', '').upcase + "\n" +
    '' + "\n" + # Content-MD5
    headers[:"Content-Type"] + "\n" + # Content-Type
    headers[:Date] + "\n" # Date
  headers.each do |key,value|
    if key.to_s =~ /^x-goog/
      canonical_headers += "#{key}:#{value}\n"
    end
  end
  
  canonical_resource = ""
  canonical_resource += "/#{bucket}" if bucket
  canonical_resource += path
  canonical_resource += '?acl' if params[:acl]
  
  authorization = 'GOOG1 ' + @access_key + ':' + sign((canonical_headers + canonical_resource).toutf8)
  
  if @debug
    puts
    puts "+++++ BUCKET: #{bucket}"
    puts "+++++ HEADERS: #{headers.inspect}"
    puts "+++++ CANONICAL_HEADERS: #{canonical_headers}"
    puts "+++++ CANONICAL_RESOURCE: #{canonical_resource}"
    puts "+++++ AUTHORIZATION: #{authorization}"
    puts
  end
  
  _http_do(method, host, path, params_to_request_string(params), headers.merge(:Authorization => authorization), options[:data])
end