@@ -31,10 +31,9 @@ def initialize(content:, header:, mode:, path:)
31
31
def publish
32
32
connection = Faraday . new ( API_BASE_URL )
33
33
34
- response = case @mode
35
- when 'create'
34
+ response = if create?
36
35
connection . post ( &request_params )
37
- when ' update'
36
+ elsif update?
38
37
connection . patch ( &request_params )
39
38
end
40
39
@@ -48,9 +47,14 @@ def publish
48
47
)
49
48
end
50
49
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
52
54
end
53
55
56
+ private
57
+
54
58
# Update a mapping file
55
59
def update_mapping_file ( item_id )
56
60
raise CannotGetQiitaItemIDError if item_id . nil? || item_id . empty?
@@ -61,8 +65,6 @@ def update_mapping_file(item_id)
61
65
end
62
66
end
63
67
64
- private
65
-
66
68
def request_params
67
69
Proc . new do |request |
68
70
request . url ( request_url )
@@ -75,7 +77,11 @@ def request_params
75
77
end
76
78
77
79
def request_url
78
- id = @mode == 'update' ? "/#{ item_id } " : nil
80
+ id = if create?
81
+ nil
82
+ elsif update?
83
+ "/#{ item_id } "
84
+ end
79
85
80
86
"#{ API_ITEM_ENDPOINT } #{ id } "
81
87
end
@@ -90,11 +96,27 @@ def request_body
90
96
title : @header [ 'title' ]
91
97
} . freeze
92
98
93
- body = body . merge ( tweet : public? ) if @mode == ' create'
99
+ body = body . merge ( tweet : public? ) if create?
94
100
95
101
body . to_json
96
102
end
97
103
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
+
98
120
def public?
99
121
@header [ 'published' ]
100
122
end
@@ -117,10 +139,21 @@ def tags
117
139
118
140
# Get a Qiita item ID corresponding to an article path
119
141
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
+
122
153
raise QiitaItemIDDuplicationError if mappings . grep ( /\A ^#{ Regexp . escape ( @path ) } / ) . length != 1
123
154
raise QiitaItemIDNotMatchedError if mappings . grep ( /\A ^#{ Regexp . escape ( @path ) } / ) . first . split . length != 2
155
+
156
+ # TODO: Use Validator.item_id
124
157
if mappings . grep ( /\A ^#{ Regexp . escape ( @path ) } / ) . first . split . last . match ( /\A [0-9a-f]{20}\z / ) . nil?
125
158
raise InvalidQiitaItemIDError
126
159
end
0 commit comments