Class: QiitaExport::Comment

Inherits:
Object
  • Object
show all
Defined in:
lib/qiita-export/comment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Comment

Returns a new instance of Comment.



9
10
11
12
# File 'lib/qiita-export/comment.rb', line 9

def initialize(url)
  @endpoint = Fetcher::ApiEndPoint.instance(:comment)
  @url = url
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/qiita-export/comment.rb', line 7

def key
  @key
end

Instance Method Details

#filenameObject



18
19
20
# File 'lib/qiita-export/comment.rb', line 18

def filename
  "comments.json"
end

#find_commentsObject



32
33
34
35
36
37
38
39
40
# File 'lib/qiita-export/comment.rb', line 32

def find_comments
  url = @endpoint.url(article_url: @url)
  open(url, request_header) do |io|
    io.read
  end
rescue => e
  $stderr.puts "#{e} : #{url}"
  raise
end

#request_headerObject



14
15
16
# File 'lib/qiita-export/comment.rb', line 14

def request_header
  Config.has_api_token? ? Config.auth_header : Config.default_header
end

#save(path) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/qiita-export/comment.rb', line 22

def save(path)
  comments = find_comments
  return if comments =~ /^\[\]$/i

  file_path = File.join(File.expand_path(path), filename)
  open(file_path, 'wb') do |out|
    out.write(comments)
  end
end