Class: CouchClient::Attachment
- Inherits:
-
ConsistentHash
- Object
- Hash
- ConsistentHash
- CouchClient::Attachment
- Defined in:
- lib/couch-client/attachment.rb
Overview
Attachment is an extended Hash that provides additional methods to interact with attached files saved within a document.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#data ⇒ Object
Returns a string that contains attachment data.
-
#initialize(id, name, stub, connection) ⇒ Attachment
constructor
Attachment is constructed with the id of the document it is attached to, the filename, file stub and connection object.
-
#path ⇒ Object
Returns the path for the attachment.
-
#uri ⇒ Object
Returns the uri for the attachment.
Methods inherited from ConsistentHash
#[]=, #default, #delete, #dup, #fetch, #key?, #merge, #regular_update, #regular_writer, #to_hash, #update, #values_at
Constructor Details
#initialize(id, name, stub, connection) ⇒ Attachment
Attachment is constructed with the id of the document it is attached to, the filename, file stub and connection object.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/couch-client/attachment.rb', line 11 def initialize(id, name, stub, connection) # Ensure that `revpos` and `length` fields are numbers, not strings, or couchdb will thow an error. ["revpos", "length"].each do |field| stub[field] &&= stub[field].to_i end # Ensure that `stub` is a boolean, not a string, or couchdb will throw an error stub["stub"] &&= true self.merge!(stub) @id = id @name = name @connection = connection end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/couch-client/attachment.rb', line 7 def name @name end |
Instance Method Details
#data ⇒ Object
Returns a string that contains attachment data
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/couch-client/attachment.rb', line 38 def data return @data if @data # Return data if it has been memoized code, @data = @connection.hookup.get([@id, @name], nil, self["content_type"]) case code when 200 @data # Return data if the attachment is found when 404 # Raise an error if the attachment is not found raise AttachmentNotFound.new("attachment '#{@name}' could not be found for document '#{@id}'") else # Also raise an error if something else happens raise Error.new("code: #{code}, error: #{@data["error"]}, reason: #{@data["reason"]}") end end |
#path ⇒ Object
Returns the path for the attachment
28 29 30 |
# File 'lib/couch-client/attachment.rb', line 28 def path @connection.hookup.handler.path([@id, @name]) end |
#uri ⇒ Object
Returns the uri for the attachment
33 34 35 |
# File 'lib/couch-client/attachment.rb', line 33 def uri @connection.hookup.handler.uri([@id, @name]) end |