Class: Blobby::HttpStore::StoredObject

Inherits:
Object
  • Object
show all
Defined in:
lib/blobby/http_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, key) ⇒ StoredObject



62
63
64
65
# File 'lib/blobby/http_store.rb', line 62

def initialize(store, key)
  @store = store
  @key = key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



67
68
69
# File 'lib/blobby/http_store.rb', line 67

def key
  @key
end

Instance Method Details

#deleteObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/blobby/http_store.rb', line 108

def delete
  with_http_connection do |http, path|
    delete = Net::HTTP::Delete.new(path)
    response = http.request(delete)
    case response
    when Net::HTTPSuccess then
      true
    when Net::HTTPNotFound then
      false
    else
      response.error!
    end
  end
end

#exists?Boolean



69
70
71
72
73
74
# File 'lib/blobby/http_store.rb', line 69

def exists?
  with_http_connection do |http, path|
    response = http.head(path)
    response.code == "200"
  end
end

#read(&block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/blobby/http_store.rb', line 76

def read(&block)
  with_http_connection do |http, path|
    http.request_get(path) do |response|
      case response
      when Net::HTTPNotFound then
        return nil
      when Net::HTTPSuccess then
        if block_given?
          response.read_body(&block)
          return nil
        else
          return response.read_body
        end
      end
      response.error!
    end
  end
end

#write(content) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/blobby/http_store.rb', line 95

def write(content)
  content = content.read if content.respond_to?(:read)
  with_http_connection do |http, path|
    put = Net::HTTP::Put.new(path)
    put.body = content
    put["Content-Type"] = "application/octet-stream"
    response = http.request(put)
    response.error! unless response.is_a?(Net::HTTPSuccess)
    true
  end
  nil
end