Class: PlanMyStuff::Attachment
- Inherits:
-
Object
- Object
- PlanMyStuff::Attachment
- Includes:
- ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Serializers::JSON
- Defined in:
- lib/plan_my_stuff/attachment.rb
Overview
Value object representing a single attachment record on a Comment. Persisted in CommentMetadata#attachments; the gem owns the upload (see PlanMyStuff::AttachmentUploader) and stores the structured location (owner/repo/sha/path) of the uploaded file so the blob remains addressable across later branch rewrites.
Mirrors PlanMyStuff::Link / PlanMyStuff::Approval: ActiveModel::Attributes-backed, with Serializers::JSON for round-trip through the metadata blob.
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
-
#download_to(dest = nil) ⇒ String
Downloads the file via the GitHub Contents API (so it works on private repos) and writes the bytes to disk.
-
#filename ⇒ String
Display filename (user-provided original name).
- #hash ⇒ Integer
-
#owner ⇒ String
Owner of the attachment repo (e.g. “BrandsInsurance”).
-
#path ⇒ String
Path within the attachment repo (no leading slash).
-
#repo ⇒ String
Attachment repo name.
-
#sha ⇒ String
Commit SHA pinning the file.
- #to_h ⇒ Hash
-
#url ⇒ String
Blob-viewer URL for the pinned file.
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
49 50 51 52 53 |
# File 'lib/plan_my_stuff/attachment.rb', line 49 def ==(other) return false unless other.is_a?(PlanMyStuff::Attachment) filename == other.filename && url == other.url end |
#download_to(dest = nil) ⇒ String
Downloads the file via the GitHub Contents API (so it works on private repos) and writes the bytes to disk. Defaults the destination to Dir.tmpdir joined with File.basename(filename) (directory components stripped to prevent path traversal).
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/plan_my_stuff/attachment.rb', line 70 def download_to(dest = nil) dest ||= File.join(Dir.tmpdir, File.basename(filename.to_s)) body = PlanMyStuff.client.rest( :contents, "#{owner}/#{repo}", path: path, ref: sha, accept: 'application/vnd.github.raw', ) File.binwrite(dest, body) dest end |
#filename ⇒ String
Returns display filename (user-provided original name).
23 |
# File 'lib/plan_my_stuff/attachment.rb', line 23 attribute :filename, :string |
#hash ⇒ Integer
58 59 60 |
# File 'lib/plan_my_stuff/attachment.rb', line 58 def hash [filename, url].hash end |
#owner ⇒ String
Returns owner of the attachment repo (e.g. “BrandsInsurance”).
25 |
# File 'lib/plan_my_stuff/attachment.rb', line 25 attribute :owner, :string |
#path ⇒ String
Returns path within the attachment repo (no leading slash).
31 |
# File 'lib/plan_my_stuff/attachment.rb', line 31 attribute :path, :string |
#repo ⇒ String
Returns attachment repo name.
27 |
# File 'lib/plan_my_stuff/attachment.rb', line 27 attribute :repo, :string |
#sha ⇒ String
Returns commit SHA pinning the file.
29 |
# File 'lib/plan_my_stuff/attachment.rb', line 29 attribute :sha, :string |
#to_h ⇒ Hash
41 42 43 |
# File 'lib/plan_my_stuff/attachment.rb', line 41 def to_h { filename: filename, owner: owner, repo: repo, sha: sha, path: path } end |
#url ⇒ String
Returns blob-viewer URL for the pinned file.
36 37 38 |
# File 'lib/plan_my_stuff/attachment.rb', line 36 def url "https://github.com/#{owner}/#{repo}/blob/#{sha}/#{path}" end |