Class: QiitaPost
- Inherits:
-
Object
- Object
- QiitaPost
- Defined in:
- lib/qiita_org/post.rb
Instance Method Summary collapse
-
#add_qiita_id_on_org ⇒ Object
add qiita_id on src.org.
-
#add_source_path_in_md ⇒ Object
add source path in md.
-
#convert_org_to_md ⇒ Object
src.org -> src.md.
-
#get_and_print_qiita_return ⇒ Object
qiita return.
- #get_title_tags ⇒ Object
-
#initialize(file, option) ⇒ QiitaPost
constructor
A new instance of QiitaPost.
-
#qiita_post ⇒ Object
qiita post.
- #run ⇒ Object
- #select_option(option) ⇒ Object
-
#select_patch_or_post ⇒ Object
patch or post selector by qiita_id.
- #set_config ⇒ Object
Constructor Details
#initialize(file, option) ⇒ QiitaPost
Returns a new instance of QiitaPost.
9 10 11 12 |
# File 'lib/qiita_org/post.rb', line 9 def initialize(file, option) @src = file @option = (option == "qiita" || option == "open")? "public" : option end |
Instance Method Details
#add_qiita_id_on_org ⇒ Object
add qiita_id on src.org
123 124 125 126 127 128 |
# File 'lib/qiita_org/post.rb', line 123 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_md ⇒ Object
add source path in md
45 46 47 48 49 |
# File 'lib/qiita_org/post.rb', line 45 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_md ⇒ Object
src.org -> src.md
38 39 40 41 42 |
# File 'lib/qiita_org/post.rb', line 38 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 end |
#get_and_print_qiita_return ⇒ Object
qiita return
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/qiita_org/post.rb', line 109 def get_and_print_qiita_return() p @res. @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 end |
#get_title_tags ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/qiita_org/post.rb', line 15 def () @conts = File.read(@src) @title = @conts.match(/\#\+(TITLE|title|Title): (.+)/)[2] || "テスト" m = [] = if m = @conts.match(/\#\+(TAG|tag|Tag|tags|TAGS|Tags): (.+)/) m[2].split(",").inject([]) do |l, c| l << { name: c.strip } #, versions: []} end else [{ name: "hoge" }] #, versions: [] }] end p end |
#qiita_post ⇒ Object
qiita post
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/qiita_org/post.rb', line 78 def qiita_post() params = { "body": @lines.join.gsub("\\\\", "").gsub("\\", ""), #"# テスト", "private": @private, "title": @title, "tags": , } 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 end |
#run ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/qiita_org/post.rb', line 130 def run() () set_config() convert_org_to_md() add_source_path_in_md() select_patch_or_post() @qiita, @private = select_option(@option) qiita_post() get_and_print_qiita_return() # open qiitta system "open #{@res_body["url"]}" add_qiita_id_on_org() end |
#select_option(option) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/qiita_org/post.rb', line 63 def select_option(option) 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 ⇒ Object
patch or post selector by qiita_id
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/qiita_org/post.rb', line 52 def select_patch_or_post() m = [] @patch = false if m = @conts.match(/\#\+qiita_#{@option}: (.+)/) @qiita_id = m[1] @patch = true else @qiita_id = "" end end |
#set_config ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/qiita_org/post.rb', line 29 def set_config() conf_path = File.join(ENV["HOME"], ".qiita.conf") @conf = JSON.load(File.read(conf_path)) @access_token = @conf["access_token"] @teams_url = @conf["teams_url"] @ox_qmd_load_path = @conf["ox_qmd_load_path"] end |