Class: QiitaExport::Config
- Inherits:
-
Object
- Object
- QiitaExport::Config
- Defined in:
- lib/qiita-export/config.rb
Constant Summary collapse
- HOME_CONFIG_FILE =
"~/.qiita-exportrc"
- LOCAL_CONFIG_FILE =
"./.qiita-exportrc"
- DEFAULT_HOST =
"qiita.com"
- DEFAULT_USER_AGENT =
"QiitaExport Gem #{QiitaExport::VERSION}"
- DEFAULT_HEADER =
{ "User-Agent" => DEFAULT_USER_AGENT }
- DEFAULT_KOBITO_DB =
"~/Library/Containers/com.qiita.Kobito/Data/Library/Kobito/Kobito.db"
- DOMAIN_PATTERN =
Regexp.new("https?://([^/]+)/")
- USER_ID_PATTERN =
Regexp.new("https?://[^/?&#]+/([^/]+)/")
Class Method Summary collapse
- .api? ⇒ Boolean
- .api_domain(url = nil) ⇒ Object
- .api_token ⇒ Object
- .article_key(url) ⇒ Object
- .article_urls ⇒ Object
- .auth_header ⇒ Object
- .comment_export? ⇒ Boolean
- .configure(argv) ⇒ Object
- .default_header ⇒ Object
- .empty? ⇒ Boolean
- .exclude_pattern ⇒ Object
- .export_dir_path(url) ⇒ Object
- .file_export? ⇒ Boolean
- .filename(title) ⇒ Object
- .has_api_token? ⇒ Boolean
- .help ⇒ Object
- .html_export? ⇒ Boolean
- .image_export? ⇒ Boolean
- .kobito? ⇒ Boolean
- .kobito_db ⇒ Object
- .original_image_filename? ⇒ Boolean
- .output_dir ⇒ Object
- .team? ⇒ Boolean
- .team_all? ⇒ Boolean
- .team_name(url = nil) ⇒ Object
- .team_url?(url) ⇒ Boolean
- .user? ⇒ Boolean
- .user_id(url = nil) ⇒ Object
- .validate! ⇒ Object
Class Method Details
.api? ⇒ Boolean
91 92 93 |
# File 'lib/qiita-export/config.rb', line 91 def api? present?(@option[:url]) || present?(@option[:'url-list']) || present?(@option[:'user-id']) || team_all? end |
.api_domain(url = nil) ⇒ Object
179 180 181 182 183 184 185 |
# File 'lib/qiita-export/config.rb', line 179 def api_domain(url = nil) if url.nil? team? ? "#{team_name}.#{DEFAULT_HOST}" : DEFAULT_HOST else URI.parse(url).host end end |
.api_token ⇒ Object
187 188 189 |
# File 'lib/qiita-export/config.rb', line 187 def api_token @option[:'api-token'] end |
.article_key(url) ⇒ Object
151 152 153 |
# File 'lib/qiita-export/config.rb', line 151 def article_key(url) File.basename(url) end |
.article_urls ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/qiita-export/config.rb', line 155 def article_urls urls = [] urls << @option[:url] if present?(@option[:url]) if present?(@option[:'url-list']) open(@option[:'url-list']) { |io| io.each_line { |line| url = line.chop next if blank?(url) urls << url } } end urls end |
.auth_header ⇒ Object
203 204 205 206 207 |
# File 'lib/qiita-export/config.rb', line 203 def auth_header header = default_header header["Authorization"] = "Bearer #{api_token}" header end |
.comment_export? ⇒ Boolean
119 120 121 |
# File 'lib/qiita-export/config.rb', line 119 def comment_export? @option[:comment] end |
.configure(argv) ⇒ Object
19 20 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 48 49 50 51 |
# File 'lib/qiita-export/config.rb', line 19 def configure(argv) @option = {} @parser = OptionParser.new do |opt| opt.version = QiitaExport::VERSION opt.on('-u', '--url=url', 'specify the URL for the Qiita(or Qiita Team).') { |v| @option[:url] = v } opt.on('-l', '--url-list=filepath', 'specify the file path of the URL list.') { |v| @option[:'url-list'] = v } opt.on('-U', '--user-id=user_id', 'specify the userid for the Qiita.') { |v| @option[:'user-id'] = v } opt.on('-k', '--kobito=[Kobito.db]', 'export Kobito.app database.') { |v| @option[:kobito] = v } opt.on('-t', '--team=teamname', 'export Qiita Team articles only.') { |v| @option[:team] = v } opt.on('-T', '--team-all', 'export Qiita Team all articles.') { |v| @option[:'team-all'] = v } opt.on('-i', '--image', 'export with images.') { |v| @option[:image] = v } opt.on('-c', '--comment', 'export with comments.') { |v| @option[:comment] = v } opt.on('-h', '--html', 'export in html format(experimental).') { |v| @option[:html] = v } opt.on('-o', '--output-dir=dirpath', 'specify the full path of destination directory.') { |v| @option[:'output-dir'] = v } opt.on('-a', '--api-token=token', 'specify API token for Qiita.') { |v| @option[:'api-token'] = v } opt.on('-e', '--exclude-pattern=regexp', 'specify the regexp pattern to exclude article title.') { |v| @option[:'exclude-pattern'] = v } opt.on('-g', '--original-image-filename','export by original image file name') { |v| @option[:'original-image-filename'] = v } opt.on('--no-rc', 'ignore .qiita-exportrc files.') { |v| @option[:'no-rc'] = v } end unless argv.include?("--no-rc") # load home config @parser.load(File.(HOME_CONFIG_FILE)) # load local config @parser.load(File.(LOCAL_CONFIG_FILE)) end # parse argv @parser.parse!(argv) self end |
.default_header ⇒ Object
199 200 201 |
# File 'lib/qiita-export/config.rb', line 199 def default_header DEFAULT_HEADER.clone end |
.empty? ⇒ Boolean
53 54 55 |
# File 'lib/qiita-export/config.rb', line 53 def empty? @option.empty? end |
.exclude_pattern ⇒ Object
191 192 193 |
# File 'lib/qiita-export/config.rb', line 191 def exclude_pattern @option[:'exclude-pattern'] end |
.export_dir_path(url) ⇒ Object
214 215 216 217 218 |
# File 'lib/qiita-export/config.rb', line 214 def export_dir_path(url) team = team_name(url) user = user_id(url) File.join(output_dir, team, user) end |
.file_export? ⇒ Boolean
107 108 109 |
# File 'lib/qiita-export/config.rb', line 107 def file_export? present?(@option[:'output-dir']) end |
.filename(title) ⇒ Object
220 221 222 223 224 225 226 227 |
# File 'lib/qiita-export/config.rb', line 220 def filename(title) return "" if !html_export? && blank?(title) if html_export? "index.html" else "#{title.gsub(/\//, ':')}.md" end end |
.has_api_token? ⇒ Boolean
195 196 197 |
# File 'lib/qiita-export/config.rb', line 195 def has_api_token? present?(@option[:'api-token']) end |
.help ⇒ Object
57 58 59 |
# File 'lib/qiita-export/config.rb', line 57 def help @parser.help end |
.html_export? ⇒ Boolean
111 112 113 |
# File 'lib/qiita-export/config.rb', line 111 def html_export? @option[:html] end |
.image_export? ⇒ Boolean
115 116 117 |
# File 'lib/qiita-export/config.rb', line 115 def image_export? @option[:image] end |
.kobito? ⇒ Boolean
87 88 89 |
# File 'lib/qiita-export/config.rb', line 87 def kobito? @option.key?(:kobito) end |
.kobito_db ⇒ Object
171 172 173 174 175 176 177 |
# File 'lib/qiita-export/config.rb', line 171 def kobito_db if blank?(@option[:kobito]) File.(DEFAULT_KOBITO_DB) else File.(@option[:kobito]) end end |
.original_image_filename? ⇒ Boolean
123 124 125 |
# File 'lib/qiita-export/config.rb', line 123 def original_image_filename? @option[:'original-image-filename'] end |
.output_dir ⇒ Object
209 210 211 212 |
# File 'lib/qiita-export/config.rb', line 209 def output_dir return "" unless file_export? File.(@option[:'output-dir'].strip) end |
.team? ⇒ Boolean
99 100 101 |
# File 'lib/qiita-export/config.rb', line 99 def team? present?(@option[:team]) end |
.team_all? ⇒ Boolean
103 104 105 |
# File 'lib/qiita-export/config.rb', line 103 def team_all? @option.key?(:'team-all') end |
.team_name(url = nil) ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/qiita-export/config.rb', line 132 def team_name(url = nil) if present?(url) url.match(DOMAIN_PATTERN)[1].split('.')[0] elsif team? @option[:team] else "" end end |
.team_url?(url) ⇒ Boolean
127 128 129 |
# File 'lib/qiita-export/config.rb', line 127 def team_url?(url) url !~ /^https?:\/\/qiita\.com/ end |
.user? ⇒ Boolean
95 96 97 |
# File 'lib/qiita-export/config.rb', line 95 def user? present?(@option[:'user-id']) end |
.user_id(url = nil) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/qiita-export/config.rb', line 143 def user_id(url = nil) if url.nil? @option[:'user-id'] else url.match(USER_ID_PATTERN)[1] end end |
.validate! ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/qiita-export/config.rb', line 61 def validate! if present?(@option[:'url-list']) && !File.exist?(@option[:'url-list']) fail ArgumentError.new("-l (#{@option[:'url-list']}) does not exist.") end if kobito? && !File.exist?(kobito_db) fail ArgumentError.new("#{kobito_db} does not exist.") end if kobito? && api? fail ArgumentError.new("if you specify option --kobito, you cannot specify option --url, --url-list and --user-id.") end if !kobito? && !api? fail ArgumentError.new("one of the required options --kobito, --url, --url-list and --user-id must be specified.") end if !kobito? && team? && !has_api_token? fail ArgumentError.new("if you specify option --team, option --api-token is required.") end if team_all? && !team? fail ArgumentError.new("if you specify option --team-all, option --team is required.") end end |