cryptkeeper 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -37,3 +37,16 @@ Creates 3 new picture files on my Desktop:
37
37
  Picture 1.png,
38
38
  Picture 2.png,
39
39
  Picture 3.png
40
+
41
+ Add a new object to a bucket
42
+ new_bucket = keeper.create_bucket({:name => "adam_new_bucket3"})
43
+
44
+ data = File.open("/Users/user/Desktop/Picture 3.png", "rb")
45
+ new_bucket.add_object('Picture 5.png', data, 'image/png')
46
+
47
+ data = File.open("/Users/user/Desktop/text_file.txt", 'r')
48
+ new_bucket.add_object("myTextFile.txt", data) # if you don't specify a content type, binary/octet-stream is used
49
+
50
+ Delete an object
51
+ keeper.bucket_meta_objects('adam_new_bucket3', {:prefix => 'Picture%205.png'}).first.delete
52
+
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cryptkeeper}
8
- s.version = "0.2.4"
8
+ s.version = "0.2.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Adam Medeiros"]
12
- s.date = %q{2011-02-15}
12
+ s.date = %q{2011-02-19}
13
13
  s.description = %q{Use cryptkeeper to talk to Google Storage}
14
14
  s.email = %q{adammede@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -1,16 +1,37 @@
1
1
  require 'hpricot'
2
+ require 'digest/md5'
2
3
  module Buckets
4
+
5
+ # Bucket stuff
3
6
  # GET Service
4
7
  # lists all of the buckets that you own
5
8
  # return an array of Bucket objects
6
9
  def buckets
7
- Hpricot(CryptKeeper::Connection.http_instance.get("/")).search("bucket").map do |el|
8
- hsh = {
9
- :name => el.search("name").inner_html,
10
- :created_at => el.search("creationdate").inner_html
11
- }
12
- Bucket.new(hsh)
10
+ find_buckets
11
+ end
12
+
13
+ def connection
14
+ CryptKeeper::Connection.http_instance
15
+ end
16
+
17
+ def create_bucket(*args)
18
+ options = args.last
19
+ connection.put("/", options[:name])
20
+ find_buckets(options)
21
+ end
22
+
23
+ def find_buckets(*args)
24
+ options = args.last
25
+ name = options[:name] if options.is_a? Hash
26
+ buckets = Hpricot(connection.get("/")).search("bucket").map do |el|
27
+ Bucket.new(
28
+ {
29
+ :name => el.search("name").inner_html,
30
+ :created_at => el.search("creationdate").inner_html
31
+ }
32
+ )
13
33
  end
34
+ name.nil? ? buckets : buckets.reject { |bucket| bucket.name != name }[0]
14
35
  end
15
36
 
16
37
  # GET Bucket
@@ -33,7 +54,7 @@ module Buckets
33
54
  path << "prefix=#{options[:prefix]}&" if !options[:prefix].nil?
34
55
  path.gsub!(/[&]$/, '')
35
56
 
36
- hpr = Hpricot(CryptKeeper::Connection.http_instance.get(path, bucket_name))
57
+ hpr = Hpricot(connection.get(path, bucket_name))
37
58
  obj_hashes = hpr.search("contents").map do |el|
38
59
  {
39
60
  :bucket_name => bucket_name,
@@ -49,13 +70,13 @@ module Buckets
49
70
  obj_hashes.map { |obj| BucketMetaObject.new(obj) }
50
71
  end
51
72
 
52
- def create_bucket(*args)
53
-
73
+ class Utils
74
+ def connection
75
+ CryptKeeper::Connection.http_instance
76
+ end
54
77
  end
55
78
 
56
-
57
-
58
- class Bucket
79
+ class Bucket < Utils
59
80
  attr_accessor :name, :created_at
60
81
 
61
82
  def initialize(*args)
@@ -65,16 +86,26 @@ module Buckets
65
86
  @created_at = options[:created_at]
66
87
  end
67
88
 
68
- def create(*args)
69
-
89
+ def delete(options={})
90
+ connection.delete("/", @name)
70
91
  end
71
92
 
72
93
  def update(*args)
73
94
 
74
95
  end
96
+
97
+ def add_object(name, data, content_type='binary/octet-stream', *args)
98
+ data.rewind
99
+ additional_headers = {
100
+ 'Content-MD5' => Base64.encode64(Digest::MD5.digest(data.read)).strip,
101
+ 'Content-Type' => content_type
102
+ }
103
+ additional_headers.merge!(args.pop) if args.last.is_a? Hash
104
+ connection.put("/#{URI.escape(name)}", @name, data, additional_headers)
105
+ end
75
106
  end
76
107
 
77
- class BucketMetaObject
108
+ class BucketMetaObject < Utils
78
109
  attr_accessor :properties,
79
110
  :key, :last_modified, :e_tag, :size, :storage_class,
80
111
  :owner_id, :owner_display_name,
@@ -97,7 +128,11 @@ module Buckets
97
128
  end
98
129
 
99
130
  def object!
100
- CryptKeeper::Connection.http_instance.get("/#{URI.escape(@key)}", @bucket_name)
131
+ connection.get("/#{URI.escape(@key)}", @bucket_name)
132
+ end
133
+
134
+ def delete
135
+ connection.delete("/#{URI.escape(@key)}", @bucket_name)
101
136
  end
102
137
  end
103
138
  end
@@ -4,7 +4,7 @@ require ck_path + '/crypt_keeper_http.rb'
4
4
  require 'uri'
5
5
 
6
6
  module CryptKeeper
7
- VERSION = "0.2.4"
7
+ VERSION = "0.2.5"
8
8
  class Connection
9
9
  @@http_instance = nil
10
10
  def self.http_instance
@@ -3,41 +3,88 @@ require 'iconv'
3
3
  require 'hmac-sha1'
4
4
  require 'base64'
5
5
  class CryptKeeperHttp
6
+
7
+ # Request Types
8
+ GET = "GET"
9
+ PUT = "PUT"
10
+ POST = "POST"
11
+ HEAD = "HEAD"
12
+ DELETE = "DELETE"
13
+
6
14
  def initialize(address, access_key, secret_key, signature_id)
7
- @address = address
8
- @access_key = access_key
9
- @secret_key = secret_key
15
+ @address = address
16
+ @access_key = access_key
17
+ @secret_key = secret_key
10
18
  @signature_id = signature_id
11
19
  end
12
20
 
13
21
  def signature(message)
14
- ic_utf8 = Iconv.new('ascii//TRANSLIT//IGNORE', 'UTF-8')
22
+ ic_utf8 = Iconv.new('ascii//TRANSLIT//IGNORE', 'UTF-8')
15
23
  utf8_message = ic_utf8.iconv(message)
16
- utf8_key = ic_utf8.iconv(@secret_key)
17
- sha1 = HMAC::SHA1.digest(utf8_key, utf8_message)
24
+ utf8_key = ic_utf8.iconv(@secret_key)
25
+ sha1 = HMAC::SHA1.digest(utf8_key, utf8_message)
18
26
  Base64.encode64(sha1).strip
19
27
  end
20
28
 
21
29
  def get(path, bucket_name='', additional_headers={})
30
+ request(GET, path, bucket_name, nil, additional_headers)
31
+ end
32
+
33
+ def put(path, bucket_name='', body=nil, additional_headers={})
34
+ request(PUT, path, bucket_name, body, additional_headers)
35
+ end
36
+
37
+ def delete(path, bucket_name, additional_headers={})
38
+ request(DELETE, path, bucket_name, nil, additional_headers)
39
+ end
40
+
41
+ def request(type, path, bucket_name='', body=nil, additional_headers={})
42
+ body.rewind if body && body.respond_to?(:rewind)
22
43
  bucket_name = "#{bucket_name}." if !bucket_name.empty?
23
- res = nil
24
- time = Time.now.strftime("%a, %d %b %Y %H:%M:%S %z")
44
+
45
+ res = nil
25
46
  Net::HTTP.start(@address) do |http|
47
+ req = Net::HTTP.const_get(type.capitalize).new(path)
26
48
 
27
- req = Net::HTTP::Get.new(path)
28
- req.add_field('Host', "#{bucket_name}#{@address}")
49
+ content_md5 = ''
50
+ content_type = ''
29
51
 
30
52
  if !additional_headers.empty?
31
- additional_headers.each { |k, v| req.add_field(k, v) }
53
+ additional_headers.each do |k, v|
54
+ req.add_field(k, v)
55
+ if type==PUT
56
+ case k
57
+ when 'Content-MD5'
58
+ content_md5 = v
59
+ when 'Content-Type'
60
+ content_type = v
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ content_length = 0
67
+ if body
68
+ if body.respond_to? :lstat
69
+ content_length = body.stat.size
70
+ req.body_stream = body
71
+ else
72
+ content_length = body.size
73
+ req.body = body
74
+ end
32
75
  end
33
76
 
77
+ time = Time.now.strftime("%a, %d %b %Y %H:%M:%S %z")
78
+
79
+ sig_path = !bucket_name.empty? ? "/#{bucket_name.gsub(/[\.]$/, path.gsub(/\?.*$/, ''))}" : "/"
80
+ req.add_field('Host', "#{bucket_name}#{@address}")
34
81
  req.add_field('Date', time)
35
- req.add_field('Content-Length', 0)
36
- sig = signature("GET\n\n\n#{time}\n#{!bucket_name.empty? ? "/#{bucket_name.gsub(/[\.]$/, path.gsub(/\?.*$/,''))}" : "/"}")
37
- req.add_field('Authorization', "#{@signature_id} #{@access_key}:#{sig}")
82
+ req.add_field('Content-length', content_length)
83
+ sig = "#{type}\n#{content_md5}\n#{content_type}\n#{time}\n#{sig_path}"
84
+ req.add_field('Authorization', "#{@signature_id} #{@access_key}:#{signature(sig)}")
85
+
38
86
  res = http.request(req)
39
87
  end
40
- return res.body if res
41
- nil
88
+ res ? res.body : nil
42
89
  end
43
90
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 4
9
- version: 0.2.4
8
+ - 5
9
+ version: 0.2.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Adam Medeiros
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-15 00:00:00 -08:00
17
+ date: 2011-02-19 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency