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

Instance Method Details

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

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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

Parameters:

  • dest (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



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

#filenameString

Returns display filename (user-provided original name).

Returns:

  • (String)

    display filename (user-provided original name)



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

attribute :filename, :string

#hashInteger

Returns:

  • (Integer)


58
59
60
# File 'lib/plan_my_stuff/attachment.rb', line 58

def hash
  [filename, url].hash
end

#ownerString

Returns owner of the attachment repo (e.g. “BrandsInsurance”).

Returns:

  • (String)

    owner of the attachment repo (e.g. “BrandsInsurance”)



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

attribute :owner, :string

#pathString

Returns path within the attachment repo (no leading slash).

Returns:

  • (String)

    path within the attachment repo (no leading slash)



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

attribute :path, :string

#repoString

Returns attachment repo name.

Returns:

  • (String)

    attachment repo name



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

attribute :repo, :string

#shaString

Returns commit SHA pinning the file.

Returns:

  • (String)

    commit SHA pinning the file



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

attribute :sha, :string

#to_hHash

Returns:

  • (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

#urlString

Returns blob-viewer URL for the pinned file.

Returns:

  • (String)

    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