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 a SHA-pinned raw.githubusercontent.com permalink so the file remains reachable across later branch rewrites.
Mirrors PlanMyStuff::Link / PlanMyStuff::Approval: ActiveModel::Attributes-backed, with Serializers::JSON for round-trip through the metadata blob.
Constant Summary collapse
- RAW_URL_REGEX =
%r{\Ahttps://raw\.githubusercontent\.com/(?<owner>[^/\s]+)/(?<repo>[^/\s]+)/(?<sha>[^/\s]+)/(?<path>.+)\z}
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
-
#download_to(path = 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
- #to_h ⇒ Hash
-
#url ⇒ String
SHA-pinned raw.githubusercontent.com permalink to the uploaded file.
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
42 43 44 45 46 |
# File 'lib/plan_my_stuff/attachment.rb', line 42 def ==(other) return false unless other.is_a?(PlanMyStuff::Attachment) filename == other.filename && url == other.url end |
#download_to(path = 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).
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/plan_my_stuff/attachment.rb', line 63 def download_to(path = nil) path ||= File.join(Dir.tmpdir, File.basename(filename.to_s)) match = RAW_URL_REGEX.match(url) body = PlanMyStuff.client.rest( :contents, "#{match[:owner]}/#{match[:repo]}", path: match[:path], ref: match[:sha], accept: 'application/vnd.github.raw', ) File.binwrite(path, body) path end |
#filename ⇒ String
Returns display filename (user-provided original name).
26 |
# File 'lib/plan_my_stuff/attachment.rb', line 26 attribute :filename, :string |
#hash ⇒ Integer
51 52 53 |
# File 'lib/plan_my_stuff/attachment.rb', line 51 def hash [filename, url].hash end |
#to_h ⇒ Hash
34 35 36 |
# File 'lib/plan_my_stuff/attachment.rb', line 34 def to_h { filename: filename, url: url } end |
#url ⇒ String
Returns SHA-pinned raw.githubusercontent.com permalink to the uploaded file.
28 |
# File 'lib/plan_my_stuff/attachment.rb', line 28 attribute :url, :string |