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

Returns a new instance of StoredObject.



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

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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

Instance Method Details

#deleteObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/blobby/http_store.rb', line 113

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

Returns:

  • (Boolean)


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

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

#read(&block) ⇒ Object



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

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/blobby/http_store.rb', line 96

def write(content)
  if content.respond_to?(:read)
    content = content.read
  else
    content = content.dup
  end
  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