Class: PlanMyStuff::Attachment

Inherits:
Object
  • Object
show all
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

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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).

Parameters:

  • path (String, nil) (defaults to: nil)

    destination path; defaults to File.join(Dir.tmpdir, File.basename(filename))

Returns:

  • (String)

    the path the bytes were written to



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

#filenameString

Returns display filename (user-provided original name).

Returns:

  • (String)

    display filename (user-provided original name)



26
# File 'lib/plan_my_stuff/attachment.rb', line 26

attribute :filename, :string

#hashInteger

Returns:

  • (Integer)


51
52
53
# File 'lib/plan_my_stuff/attachment.rb', line 51

def hash
  [filename, url].hash
end

#to_hHash

Returns:

  • (Hash)


34
35
36
# File 'lib/plan_my_stuff/attachment.rb', line 34

def to_h
  { filename: filename, url: url }
end

#urlString

Returns SHA-pinned raw.githubusercontent.com permalink to the uploaded file.

Returns:

  • (String)

    SHA-pinned raw.githubusercontent.com permalink to the uploaded file



28
# File 'lib/plan_my_stuff/attachment.rb', line 28

attribute :url, :string