Class: QiitaFileUpLoad

Inherits:
Object
  • Object
show all
Defined in:
lib/qiita_org/upload.rb

Instance Method Summary collapse

Constructor Details

#initialize(src, option, os) ⇒ QiitaFileUpLoad

Returns a new instance of QiitaFileUpLoad.



6
7
8
9
10
11
12
13
# File 'lib/qiita_org/upload.rb', line 6

def initialize(src, option, os)
  @src = src
  @option = (option == "qiita" || option == "open")? "public" : option
  @os = os
  @base = QiitaBase.new()
  @access_token, @teams_url, @display, @ox_qmd_load_path = QiitaBase.new().set_config()
  ErrorMessage.new().teams_url_error(@teams_url) if @option == "teams"
end

Instance Method Details

#get_file_path(src) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/qiita_org/upload.rb', line 30

def get_file_path(src)
  lines = File.readlines(src)
  files = []
  lines.each do |line|
    if path = line.match(/\[\[(.+)\]\[file:(.+)\]\]/) || line.match(/\[\[file:(.+)\]\]/)
      if path[2] == nil
        files << path[1]
      else
        files << path[2]
      end
    end
  end

  return files
end

#get_file_url(id, file_name) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/qiita_org/upload.rb', line 95

def get_file_url(id, file_name)
  qiita = (@option == "teams")? @teams_url : "https://qiita.com/"
  path = "api/v2/items/#{@id}"

  items = @access.access_qiita()

  if items["body"].match?(/\!\[#{file_name}\]\(((.+))\)/)
    @file_url = items["body"].match(/\!\[#{file_name}\]\(((.+))\)/)[2]
    puts "Wrote #{file_name}'s URL".green
    return true
  else
    puts "Can not find #{file_name}'s URL".red
    return false
  end
end

#input_url_to_org(paths) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/qiita_org/upload.rb', line 72

def input_url_to_org(paths)
  lines = File.readlines(@src)
  conts = File.read(@src)
  id = conts.match(/\#\+qiita_#{@option}: (.+)/)[1]

  paths.each do |path|
    file_name = File.basename(path).strip
    #url = (get_file_url(id, file_name)) ? @file_url : next
    if get_file_url(id, file_name)
      url = @file_url
    else
      next
    end
    lines.each_with_index do |line, i|
      if line.match(/\[\[file:#{path}\]\]/)
        lines[i] = "[[#{url}][file:#{path}]]\n"
      end
    end
  end

  File.write(@src, lines.join)
end

#open_file_dir(paths) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/qiita_org/upload.rb', line 46

def open_file_dir(paths)
  previous_paths = []
  previous_paths << File.join(paths[0].split("/")[0..-2])
  @base.file_open(@os, File.join(paths[0].split("/")[0..-2]))

  paths.each do |path|
    dir_path = File.join(path.split("/")[0..-2])
    unless previous_paths.include?(dir_path)
      previous_paths << dir_path
      @base.file_open(@os, dir_path)
    end
  end
end

#open_qiitaObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/qiita_org/upload.rb', line 60

def open_qiita()
  id = QiitaBase.new().get_report_id(@src, @option)

  qiita = (@option == "teams") ? @teams_url : "https://qiita.com/"
  path = "api/v2/items/#{id}"

  @access = AccessQiita.new(@access_token, qiita, path)
  items = @access.access_qiita()

  @base.file_open(@os, items["url"])
end

#uploadObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/qiita_org/upload.rb', line 15

def upload()
  paths = get_file_path(@src)
  unless paths.empty?
    open_file_dir(paths)
    open_qiita()

    puts "Overwrite file URL's on #{@src}? (y/n)".green
    ans = STDIN.getch

    input_url_to_org(paths) if ans == "y"
  else
    puts "file path is empty.".red
  end
end