Class: QiitaExport::Config

Inherits:
Object
  • Object
show all
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

Class Method Details

.api? ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/qiita-export/config.rb', line 87

def api?
  present?(@option[:url]) || present?(@option[:'url-list']) || present?(@option[:'user-id']) || team_all?
end

.api_domain(url = nil) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/qiita-export/config.rb', line 171

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



179
180
181
# File 'lib/qiita-export/config.rb', line 179

def api_token
  @option[:'api-token']
end

.article_key(url) ⇒ Object



143
144
145
# File 'lib/qiita-export/config.rb', line 143

def article_key(url)
  File.basename(url)
end

.article_urls ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/qiita-export/config.rb', line 147

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



195
196
197
198
199
# File 'lib/qiita-export/config.rb', line 195

def auth_header
  header = default_header
  header["Authorization"] = "Bearer #{api_token}"
  header
end

.comment_export? ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/qiita-export/config.rb', line 115

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
# 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 }
  end

  # load home config
  @parser.load(File.expand_path(HOME_CONFIG_FILE))

  # load local config
  @parser.load(File.expand_path(LOCAL_CONFIG_FILE))

  # parse argv
  @parser.parse!(argv)

  self
end

.default_header ⇒ Object



191
192
193
# File 'lib/qiita-export/config.rb', line 191

def default_header
  DEFAULT_HEADER.clone
end

.empty? ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/qiita-export/config.rb', line 49

def empty?
  @option.empty?
end

.exclude_pattern ⇒ Object



183
184
185
# File 'lib/qiita-export/config.rb', line 183

def exclude_pattern
  @option[:'exclude-pattern']
end

.export_dir_path(url) ⇒ Object



206
207
208
209
210
# File 'lib/qiita-export/config.rb', line 206

def export_dir_path(url)
  team = team_name(url)
  user = user_id(url)
  File.join(output_dir, team, user)
end

.file_export? ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/qiita-export/config.rb', line 103

def file_export?
  present?(@option[:'output-dir'])
end

.filename(title) ⇒ Object



212
213
214
215
216
217
218
219
# File 'lib/qiita-export/config.rb', line 212

def filename(title)
  return "" if !html_export? && blank?(title)
  if html_export?
    "index.html"
  else
    "#{title.gsub(/\//, ':')}.md"
  end
end

.has_api_token? ⇒ Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/qiita-export/config.rb', line 187

def has_api_token?
  present?(@option[:'api-token'])
end

.help ⇒ Object



53
54
55
# File 'lib/qiita-export/config.rb', line 53

def help
  @parser.help
end

.html_export? ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/qiita-export/config.rb', line 107

def html_export?
  @option[:html]
end

.image_export? ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/qiita-export/config.rb', line 111

def image_export?
  @option[:image]
end

.kobito? ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/qiita-export/config.rb', line 83

def kobito?
  @option.key?(:kobito)
end

.kobito_db ⇒ Object



163
164
165
166
167
168
169
# File 'lib/qiita-export/config.rb', line 163

def kobito_db
  if blank?(@option[:kobito])
    File.expand_path(DEFAULT_KOBITO_DB)
  else
    File.expand_path(@option[:kobito])
  end
end

.output_dir ⇒ Object



201
202
203
204
# File 'lib/qiita-export/config.rb', line 201

def output_dir
  return "" unless file_export?
  File.expand_path(@option[:'output-dir'].strip)
end

.team? ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/qiita-export/config.rb', line 95

def team?
  present?(@option[:team])
end

.team_all? ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/qiita-export/config.rb', line 99

def team_all?
  @option.key?(:'team-all')
end

.team_name(url = nil) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/qiita-export/config.rb', line 124

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

Returns:

  • (Boolean)


119
120
121
# File 'lib/qiita-export/config.rb', line 119

def team_url?(url)
  url !~ /^https?:\/\/qiita\.com/
end

.user? ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/qiita-export/config.rb', line 91

def user?
  present?(@option[:'user-id'])
end

.user_id(url = nil) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/qiita-export/config.rb', line 135

def user_id(url = nil)
  if url.nil?
    @option[:'user-id']
  else
    url.match(USER_ID_PATTERN)[1]
  end
end

.validate! ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/qiita-export/config.rb', line 57

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