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_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?://([^/]+)/")
Class Method Summary collapse
- .api? ⇒ Boolean
- .api_token ⇒ Object
- .article_urls ⇒ Object
- .auth_header ⇒ Object
- .configure(argv) ⇒ Object
- .default_header ⇒ Object
- .empty? ⇒ Boolean
- .export_dir_path ⇒ Object
- .file_export? ⇒ Boolean
- .filename(title) ⇒ Object
- .has_api_token? ⇒ Boolean
- .help ⇒ Object
- .html_export? ⇒ Boolean
- .image_export? ⇒ Boolean
- .kobito? ⇒ Boolean
- .kobito_db ⇒ Object
- .team? ⇒ Boolean
- .team_name(url = nil) ⇒ Object
- .team_url?(url) ⇒ Boolean
- .user? ⇒ Boolean
- .user_id ⇒ Object
- .validate! ⇒ Object
Class Method Details
.api? ⇒ Boolean
71 72 73 |
# File 'lib/qiita-export/config.rb', line 71 def api? present?(@option[:url]) || present?(@option[:'url-list']) || present?(@option[:'user-id']) end |
.api_token ⇒ Object
134 135 136 |
# File 'lib/qiita-export/config.rb', line 134 def api_token @option[:'api-token'] end |
.article_urls ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/qiita-export/config.rb', line 110 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
146 147 148 149 150 |
# File 'lib/qiita-export/config.rb', line 146 def auth_header header = default_header header["Authorization"] = "Bearer #{api_token}" header end |
.configure(argv) ⇒ Object
18 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 |
# File 'lib/qiita-export/config.rb', line 18 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('-i', '--image', 'export with images.') { |v| @option[:image] = 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 } end # load home config @parser.load(File.(HOME_CONFIG_FILE)) # load local config @parser.load(File.(LOCAL_CONFIG_FILE)) # parse argv @parser.parse!(argv) self end |
.default_header ⇒ Object
142 143 144 |
# File 'lib/qiita-export/config.rb', line 142 def default_header DEFAULT_HEADER.clone end |
.empty? ⇒ Boolean
45 46 47 |
# File 'lib/qiita-export/config.rb', line 45 def empty? @option.empty? end |
.export_dir_path ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/qiita-export/config.rb', line 156 def export_dir_path if user? File.join(File.(@option[:'output-dir'].strip), user_id) else File.(@option[:'output-dir'].strip) end end |
.file_export? ⇒ Boolean
83 84 85 |
# File 'lib/qiita-export/config.rb', line 83 def file_export? present?(@option[:'output-dir']) end |
.filename(title) ⇒ Object
164 165 166 167 168 169 170 171 |
# File 'lib/qiita-export/config.rb', line 164 def filename(title) return "" if !html_export? && blank?(title) if html_export? "index.html" else "#{title.gsub(/\//, ':')}.md" end end |
.has_api_token? ⇒ Boolean
138 139 140 |
# File 'lib/qiita-export/config.rb', line 138 def has_api_token? present?(@option[:'api-token']) end |
.help ⇒ Object
49 50 51 |
# File 'lib/qiita-export/config.rb', line 49 def help @parser.help end |
.html_export? ⇒ Boolean
87 88 89 |
# File 'lib/qiita-export/config.rb', line 87 def html_export? @option[:html] end |
.image_export? ⇒ Boolean
91 92 93 |
# File 'lib/qiita-export/config.rb', line 91 def image_export? @option[:image] end |
.kobito? ⇒ Boolean
67 68 69 |
# File 'lib/qiita-export/config.rb', line 67 def kobito? @option.key?(:kobito) end |
.kobito_db ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/qiita-export/config.rb', line 126 def kobito_db if blank?(@option[:kobito]) File.(DEFAULT_KOBITO_DB) else File.(@option[:kobito]) end end |
.team? ⇒ Boolean
79 80 81 |
# File 'lib/qiita-export/config.rb', line 79 def team? present?(@option[:team]) end |
.team_name(url = nil) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/qiita-export/config.rb', line 100 def team_name(url = nil) if (kobito? || user?) && team? @option[:team] elsif api? && team_url?(url) url.match(DOMAIN_PATTERN)[1].split('.')[0] else "" end end |
.team_url?(url) ⇒ Boolean
95 96 97 |
# File 'lib/qiita-export/config.rb', line 95 def team_url?(url) url !~ /^https?:\/\/qiita\.com/ end |
.user? ⇒ Boolean
75 76 77 |
# File 'lib/qiita-export/config.rb', line 75 def user? present?(@option[:'user-id']) end |
.user_id ⇒ Object
152 153 154 |
# File 'lib/qiita-export/config.rb', line 152 def user_id @option[:'user-id'] end |
.validate! ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/qiita-export/config.rb', line 53 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 end |