Class: ShowFile

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

Instance Method Summary collapse

Constructor Details

#initialize(paths, src, mode, os) ⇒ ShowFile

Returns a new instance of ShowFile.



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

def initialize(paths, src, mode, os)
  @paths = paths
  @src = src
  @mode = (mode == "qiita" || mode == "open")? "public" : mode
  @os = os
  search = SearchConfPath.new(Dir.pwd, Dir.home)
  @conf_dir = search.search_conf_path()
end

Instance Method Details

#access_qiita(access_token, qiita, path) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/qiita_org/show_file_and_url.rb', line 103

def access_qiita(access_token, qiita, path)
  uri = URI.parse(qiita + path)

  headers = { "Authorization" => "Bearer #{access_token}",
              "Content-Type" => "application/json" }

  response = URI.open(
    "#{uri}",
    "Authorization" => "#{headers["Authorization"]}",
  )
  items = JSON.parse(response.read)
  return items
end

#input_url_to_orgObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/qiita_org/show_file_and_url.rb', line 83

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

  @paths.each do |path|
    file_name = File.basename(path).strip
    geturl = GetFileUrl.new(id, file_name, @mode)
    url = geturl.get_file_url()
    lines.each_with_index do |line, i|
      if line.match(/\[\[file:#{path}\]\]/)
        #lines[i] = "[[file:# {path}][# {url}]]\n"
        lines[i] = "[[#{url}][file:#{path}]]\n"
      end
    end
  end
  #p lines
  File.write(@src, lines.join)
end

#open_file_dirObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/qiita_org/show_file_and_url.rb', line 21

def open_file_dir()
  previous_paths = []
  previous_paths << File.join(@paths[0].split("/")[0..-2])
  if @os == "mac"
    system "open #{File.join(@paths[0].split("/")[0..-2])}"
  elsif @os == "windows"
    system "explorer.exe #{File.join(@paths[0].split("/")[0..-2])}"
  else
    system "open #{File.join(@paths[0].split("/")[0..-2])}"
    system "xdg-open #{File.join(@paths[0].split("/")[0..-2])}"
  end
  @paths.each do |path|
    dir_path = File.join(path.split("/")[0..-2])
    unless previous_paths.include?(dir_path)
      previous_paths << dir_path
      #system "open #{dir_path}"
      if @os == "mac"
        system "open #{dir_path}"
      elsif @os == "windows"
        system "explorer.exe #{dir_path}"
      else
        system "open #{dir_path}"
        system "xdg-open #{dir_path}"
      end
    end
  end
end

#open_qiitaObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/qiita_org/show_file_and_url.rb', line 49

def open_qiita()
  conts = File.read(@src)
  id = conts.match(/\#\+qiita_#{@mode}: (.+)/)[1]

  set_config()

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

  items = access_qiita(@access_token, qiita, path)

  if @os == "mac"
    system "open #{items["url"]}"
  elsif @os == "windows"
    system "explorer.exe #{items["url"]}"
  else
    system "open #{items["url"]}"
    system "xdg-open #{items["url"]}"
  end
end

#set_configObject



14
15
16
17
18
19
# File 'lib/qiita_org/show_file_and_url.rb', line 14

def set_config()
  conf_path = File.join(@conf_dir, ".qiita.conf")
  @conf = JSON.load(File.read(conf_path))
  @access_token = @conf["access_token"]
  @teams_url = @conf["teams_url"]
end

#show_file_urlObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/qiita_org/show_file_and_url.rb', line 70

def show_file_url()
  conts = File.read(@src)
  id = conts.match(/\#\+qiita_#{@mode}: (.+)/)[1]

  @paths.each do |path|
    file_name = File.basename(path).strip
    geturl = GetFileUrl.new(id, file_name, @mode)
    url = geturl.get_file_url()
    puts "#{file_name}'s URL".green
    puts url
  end
end