Class: GitHubRecordsArchiver::Issue

Inherits:
Object
  • Object
show all
Includes:
DataHelper
Defined in:
lib/github_records_archiver/issue.rb

Constant Summary collapse

KEYS =
i[title number state html_url created_at closed_at].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataHelper

#method_missing, #respond_to_missing?, #to_h, #to_json

Constructor Details

#initialize(repository: nil, number: nil) ⇒ Issue

Returns a new instance of Issue.



10
11
12
13
14
# File 'lib/github_records_archiver/issue.rb', line 10

def initialize(repository: nil, number: nil)
  repository = Repository.new(repository) if repository.is_a? String
  @repository = repository
  @number = number
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class GitHubRecordsArchiver::DataHelper

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



4
5
6
# File 'lib/github_records_archiver/issue.rb', line 4

def number
  @number
end

#repositoryObject (readonly)

Returns the value of attribute repository.



3
4
5
# File 'lib/github_records_archiver/issue.rb', line 3

def repository
  @repository
end

Class Method Details

.from_hash(repo, hash) ⇒ Object



16
17
18
19
20
# File 'lib/github_records_archiver/issue.rb', line 16

def self.from_hash(repo, hash)
  issue = Issue.new(repository: repo, number: hash[:number])
  issue.instance_variable_set('@data', hash.to_h)
  issue
end

Instance Method Details

#archiveObject



46
47
48
49
# File 'lib/github_records_archiver/issue.rb', line 46

def archive
  File.write(path('md'), to_s)
  File.write(path('json'), to_json)
end

#as_jsonObject



42
43
44
# File 'lib/github_records_archiver/issue.rb', line 42

def as_json
  data.to_h.merge('comments' => comments.map(&:as_json))
end

#commentsObject



26
27
28
29
30
31
32
33
# File 'lib/github_records_archiver/issue.rb', line 26

def comments
  @comments ||= begin
    return [] if data[:comments].nil? || data[:comments].zero?
    client = GitHubRecordsArchiver.client
    comments = client.issue_comments repository.full_name, number
    comments.map { |hash| Comment.from_hash(repository, hash) }
  end
end

#dataObject



22
23
24
# File 'lib/github_records_archiver/issue.rb', line 22

def data
  @data ||= GitHubRecordsArchiver.client.issue repository.name, number
end

#to_sObject



35
36
37
38
39
40
# File 'lib/github_records_archiver/issue.rb', line 35

def to_s
  md = meta_for_markdown.to_yaml + "---\n\n# #{title}\n\n"
  md << body unless body.to_s.empty?
  md << comments_string unless comments.nil?
  md
end