Class: QiitaGet
- Inherits:
-
Object
- Object
- QiitaGet
- Defined in:
- lib/qiita_org/get.rb
Instance Method Summary collapse
-
#access_qiita ⇒ Object
access qiita.
-
#check_mode ⇒ Object
check mode.
-
#convert_md_to_org ⇒ Object
id.md -> id.org.
- #get_id_report ⇒ Object
-
#initialize(mode, id) ⇒ QiitaGet
constructor
A new instance of QiitaGet.
-
#puts_massage_and_delete_md ⇒ Object
see massage and delete id.md.
- #run ⇒ Object
-
#select_path ⇒ Object
select path.
-
#select_report ⇒ Object
select report.
-
#set_config ⇒ Object
set config.
-
#set_header ⇒ Object
set header.
-
#write_header_on_org ⇒ Object
write header.
Constructor Details
#initialize(mode, id) ⇒ QiitaGet
Returns a new instance of QiitaGet.
9 10 11 12 13 14 |
# File 'lib/qiita_org/get.rb', line 9 def initialize(mode, id) @mode = mode @get_id = id search = SearchConfPath.new(Dir.pwd, Dir.home) @conf_dir = search.search_conf_path() end |
Instance Method Details
#access_qiita ⇒ Object
access qiita
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/qiita_org/get.rb', line 37 def access_qiita() uri = URI.parse(@qiita + @path) headers = { "Authorization" => "Bearer #{@access_token}", "Content-Type" => "application/json" } begin response = URI.open( "#{uri}", "Authorization" => "#{headers["Authorization"]}", ) #raise "NOT FOUND: # {@get_id} report".red rescue => e puts "#{$!}".red exit else @items = JSON.parse(response.read) end end |
#check_mode ⇒ Object
check mode
110 111 112 113 114 115 116 |
# File 'lib/qiita_org/get.rb', line 110 def check_mode() if @private @mode = "private" else @mode = "public" end end |
#convert_md_to_org ⇒ Object
id.md -> id.org
87 88 89 |
# File 'lib/qiita_org/get.rb', line 87 def convert_md_to_org() system "pandoc #{@id}.md -o #{@id}.org" end |
#get_id_report ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/qiita_org/get.rb', line 135 def get_id_report() case @mode when "teams" @qiita = @teams_url else @qiita = "https://qiita.com/" end @path = "api/v2/items/#{@get_id}" access_qiita() @title = @items["title"] @id = @items["id"] @author = @items["user"]["id"] @tags = [] @private = @items["private"] @items["tags"].each do |tag| @tags << tag["name"] end p filename = "#{@id}.md" File.write(filename, @items["body"]) convert_md_to_org() write_header_on_org() puts_massage_and_delete_md() end |
#puts_massage_and_delete_md ⇒ Object
see massage and delete id.md
129 130 131 132 133 |
# File 'lib/qiita_org/get.rb', line 129 def puts_massage_and_delete_md() puts "created #{@id}.org".green puts "URL: #{@items["url"]}" system "rm -f #{@id}.md" end |
#run ⇒ Object
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/qiita_org/get.rb', line 161 def run() set_config() if @get_id == nil select_path() access_qiita() select_report() else get_id_report() end end |
#select_path ⇒ Object
select path
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/qiita_org/get.rb', line 25 def select_path() case @mode when "teams" @qiita = @teams_url @path = "api/v2/items?page=1&per_page=100" else @qiita = "https://qiita.com/" @path = "api/v2/authenticated_user/items?page=1&per_page=100" end end |
#select_report ⇒ Object
select report
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/qiita_org/get.rb', line 58 def select_report() @items.each do |item| p "title: #{item["title"]}" p "Do you gets it?(y/n), ('e' to exit)" p ans = STDIN.getch next if ans == "n" if ans == "e" break end if ans == "y" p @title = item["title"] #.gsub(/ |\(|\)/, " " => "_", "(" => "", ")" => "") @id = item["id"] @author = item["user"]["id"] @tags = [] @private = item["private"] item["tags"].each do |tag| @tags << tag["name"] end p filename = "#{@id}.md" File.write(filename, item["body"]) convert_md_to_org() write_header_on_org() puts_massage_and_delete_md() end end end |
#set_config ⇒ Object
set config
17 18 19 20 21 22 |
# File 'lib/qiita_org/get.rb', line 17 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 |
#set_header ⇒ Object
set header
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/qiita_org/get.rb', line 92 def set_header() head = <<"EOS" #+OPTIONS: ^:{} #+STARTUP: indent nolineimages #+TITLE: #{@title} #+AUTHOR: #{@author} #+EMAIL: #+LANGUAGE: jp # +OPTIONS: H:4 toc:t num:2 #+OPTIONS: toc:nil #+TAG: #{@tags.join(", ")} #+SETUPFILE: ~/.emacs.d/org-mode/theme-readtheorg.setup\n EOS return head end |
#write_header_on_org ⇒ Object
write header
119 120 121 122 123 124 125 126 |
# File 'lib/qiita_org/get.rb', line 119 def write_header_on_org() head = set_header() check_mode() if @mode != "teams" conts = File.read("#{@id}.org") File.write("#{@id}.org", "#+qiita_#{@mode}: #{@id}\n" + head + conts) end |