Class: Blobby::HttpStore::StoredObject

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

Overview

Represents an object in the store.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, key) ⇒ StoredObject

Returns a new instance of StoredObject.



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

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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

Instance Method Details

#deleteObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/blobby/http_store.rb', line 116

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

#exists?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
# File 'lib/blobby/http_store.rb', line 74

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

#read(&block) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/blobby/http_store.rb', line 81

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

#write(content) ⇒ Object



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

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