Class: QiitaPost

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

Instance Method Summary collapse

Constructor Details

#initialize(file, option, os) ⇒ QiitaPost

Returns a new instance of QiitaPost.



13
14
15
16
17
# File 'lib/qiita_org/post.rb', line 13

def initialize(file, option, os)
  @src = file
  @option = (option == "qiita" || option == "open")? "public" : option
  @os = os
end

Instance Method Details

#add_qiita_id_on_orgObject

add qiita_id on src.org



142
143
144
145
146
147
# File 'lib/qiita_org/post.rb', line 142

def add_qiita_id_on_org()
  @qiita_id = @res_body["id"]
  unless @patch
    File.write(@src, "#+qiita_#{@option}: #{@qiita_id}\n" + @conts)
  end
end

#add_source_path_in_mdObject

add source path in md



48
49
50
51
52
# File 'lib/qiita_org/post.rb', line 48

def add_source_path_in_md()
  @lines = File.readlines(@src.gsub(".org", ".md"))
  path = Dir.pwd.gsub(ENV["HOME"], "~")
  @lines << "\n\n------\n - **source** #{path}/#{@src}\n"
end

#convert_org_to_mdObject

src.org -> src.md



40
41
42
43
44
45
# File 'lib/qiita_org/post.rb', line 40

def convert_org_to_md()
  command = "emacs #{@src} --batch -l #{@ox_qmd_load_path} -f org-qmd-export-to-markdown --kill"
  res = command_line command
  #p res
  ErrorMessage.new().md_file_exists?(@src, res)
end

#get_and_print_qiita_returnObject

qiita return



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/qiita_org/post.rb', line 125

def get_and_print_qiita_return()
  p @res.message

  @res_body = JSON.parse(@res.body)
  @res_body.each do |key, cont|
    if key == "rendered_body" or key == "body"
      puts "%20s brabrabra..." % key
      next
    end
    print "%20s %s\n" % [key, cont]
  end
  #if @display == "suppress"
  #  puts @res_body["url"].green
  #end
end

#get_title_tags(conts) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/qiita_org/post.rb', line 20

def get_title_tags(conts)
  m = conts.match(/\#\+(TITLE|title|Title): (.+)/)
  @title = m ?  m[2] : "テスト"
  @tags = if m = conts.match(/\#\+(TAG|tag|Tag|tags|TAGS|Tags): (.+)/)
            #if m[2].count(",") >= 5
             # puts "The maximum number of tag is five. Please delete some tags.".red
              #exit
            #end
            ErrorMessage.new().many_tags_error(m[2])
            m[2].split(",").inject([]) do |l, c|
               l << { name: c.strip } #, versions: []}
            end
          else
            [{ name: "hoge" }] #, versions: [] }]
          end
  p @tags
  return @title, @tags
end

#qiita_postObject

qiita post



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/qiita_org/post.rb', line 88

def qiita_post()
  params = {
    "body": @lines.join.gsub("\\\\![", "!["), #.gsub("\\\\", ""), #.gsub("\\", ""), #"# テスト",
    "private": @private,
    "title": @title,
    "tags": @tags,
    "tweet": @twitter,
  }

  if @patch
    @path = "api/v2/items/#{@qiita_id}"
  else
    @path = "api/v2/items"
  end
  p ["qiita", @qiita]
  p ["path", @path]
  p @qiita + @path
  uri = URI.parse(@qiita + @path)

  http_req = Net::HTTP.new(uri.host, uri.port)
  http_req.use_ssl = uri.scheme === "https"

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

  if @patch
    res = http_req.patch(uri.path, params.to_json, headers)
  else
    res = http_req.post(uri.path, params.to_json, headers)
  end

  ErrorMessage.new().qiita_post_error(res, @src.gsub(".org", ".md"))

  return res
end

#runObject

def open_qiita() if @os == "mac" system "open # @res_body["url"]" elsif @os == "windows" system "explorer.exe # @res_body["url"]" else system "open # @res_body["url"]" system "xdg-open # @res_body["url"]" end end



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/qiita_org/post.rb', line 163

def run()
  @conts = File.read(@src)
  @title, @tags = get_title_tags(@conts)
  @access_token, @teams_url, @display, @ox_qmd_load_path = SetConfig.new().set_config()

  if @option == "teams"
    ErrorMessage.new().teams_url_error(@teams_url)
  end

  convert_org_to_md()
  add_source_path_in_md()
  @lines = MdConverter.new().convert_for_image(@lines)
  @qiita_id, @patch = select_patch_or_post(@conts, @option)
  @twitter = select_twitter(@conts, @option)
  @qiita, @private = select_option(@option)
  @res = qiita_post()
  get_and_print_qiita_return()

  #open_qiita()
  FileOpen.new(@os).file_open(@res_body["url"]) if @display != "suppress"

  add_qiita_id_on_org()

  system "rm #{@src.gsub(".org", ".md")}"
end

#select_option(option) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/qiita_org/post.rb', line 72

def select_option(option)
  qiita = (option == "teams")? @teams_url : "https://qiita.com/"
  #qiita = (option == "teams")? "https://nishitani.qiita.com/" :
   # "https://qiita.com/"
  case option
  when "teams", "qiita", "public", "open"
    private = false
  when nil, "private"
    private = true
  else
    raise "Unknown option: #{option}".red
  end
  return [qiita, private]
end

#select_patch_or_post(conts, option) ⇒ Object

patch or post selector by qiita_id



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/qiita_org/post.rb', line 55

def select_patch_or_post(conts, option)
  m = []
  patch = false
  if m = conts.match(/\#\+qiita_#{option}: (.+)/)
    qiita_id = m[1]
    patch = true
  else
    qiita_id = ""
  end
  return qiita_id, patch
end

#select_twitter(conts, option) ⇒ Object

check twitter post



68
69
70
# File 'lib/qiita_org/post.rb', line 68

def select_twitter(conts, option)
  option == "public" && conts.match?(/^\#\+twitter:\s*on$/i)
end