@@ -31,10 +31,9 @@ def initialize(content:, header:, mode:, path:)
3131 def publish
3232 connection = Faraday . new ( API_BASE_URL )
3333
34- response = case @mode
35- when 'create'
34+ response = if create?
3635 connection . post ( &request_params )
37- when ' update'
36+ elsif update?
3837 connection . patch ( &request_params )
3938 end
4039
@@ -48,9 +47,14 @@ def publish
4847 )
4948 end
5049
51- JSON . parse ( response . body )
50+ new_item_id = JSON . parse ( response . body ) [ 'id' ]
51+ update_mapping_file ( new_item_id ) if new_item_id && create?
52+
53+ true
5254 end
5355
56+ private
57+
5458 # Update a mapping file
5559 def update_mapping_file ( item_id )
5660 raise CannotGetQiitaItemIDError if item_id . nil? || item_id . empty?
@@ -61,8 +65,6 @@ def update_mapping_file(item_id)
6165 end
6266 end
6367
64- private
65-
6668 def request_params
6769 Proc . new do |request |
6870 request . url ( request_url )
@@ -75,7 +77,11 @@ def request_params
7577 end
7678
7779 def request_url
78- id = @mode == 'update' ? "/#{ item_id } " : nil
80+ id = if create?
81+ nil
82+ elsif update?
83+ "/#{ item_id } "
84+ end
7985
8086 "#{ API_ITEM_ENDPOINT } #{ id } "
8187 end
@@ -90,11 +96,27 @@ def request_body
9096 title : @header [ 'title' ]
9197 } . freeze
9298
93- body = body . merge ( tweet : public? ) if @mode == ' create'
99+ body = body . merge ( tweet : public? ) if create?
94100
95101 body . to_json
96102 end
97103
104+ def create?
105+ return true if @mode == 'create'
106+
107+ # Publish as a new article if mapping information is missing but
108+ # ENV['STRICT'] is set to 'false'
109+ return true if @mode == 'update' && item_id . nil? && ENV [ 'STRICT' ] == 'false'
110+
111+ false
112+ end
113+
114+ def update?
115+ return true if @mode == 'update' && item_id
116+
117+ false
118+ end
119+
98120 def public?
99121 @header [ 'published' ]
100122 end
@@ -117,10 +139,21 @@ def tags
117139
118140 # Get a Qiita item ID corresponding to an article path
119141 def item_id
120- # An error handling
121- raise QiitaItemIDNotFoundError if mappings . grep ( /\A ^#{ Regexp . escape ( @path ) } / ) . empty?
142+ if mappings . grep ( /\A ^#{ Regexp . escape ( @path ) } / ) . empty?
143+ # If mapping information is missing, and ENV['STRICT'] is set to
144+ # 'true', then raise an error
145+ #
146+ # If mapping information is missing, but ENV['STRICT'] is set to
147+ # 'false', then return nil instead
148+ #
149+ raise QiitaItemIDNotFoundError if ENV [ 'STRICT' ] == 'true'
150+ return nil
151+ end
152+
122153 raise QiitaItemIDDuplicationError if mappings . grep ( /\A ^#{ Regexp . escape ( @path ) } / ) . length != 1
123154 raise QiitaItemIDNotMatchedError if mappings . grep ( /\A ^#{ Regexp . escape ( @path ) } / ) . first . split . length != 2
155+
156+ # TODO: Use Validator.item_id
124157 if mappings . grep ( /\A ^#{ Regexp . escape ( @path ) } / ) . first . split . last . match ( /\A [0-9a-f]{20}\z / ) . nil?
125158 raise InvalidQiitaItemIDError
126159 end
0 commit comments