@@ -58,19 +58,11 @@ def parse(body, signature)
5858
5959 data = JSON . parse ( body . chomp , symbolize_names : true )
6060 data = Line ::Bot ::V2 ::Utils . deep_underscore ( data )
61+ data = Line ::Bot ::V2 ::Utils . deep_convert_reserved_words ( data )
62+ data = Line ::Bot ::V2 ::Utils . deep_symbolize ( data )
6163
6264 data [ :events ] . map do |event |
63- event_class_name = determine_class_name ( :events , event )
64- event_class = begin
65- Object . const_get ( event_class_name )
66- rescue StandardError
67- nil
68- end
69-
70- # If there is no specific webhook class, leave the value as is
71- event_instance = event_class ? create_instance ( event_class , event ) : event
72-
73- deep_hash_to_struct ( event_instance )
65+ Line ::Bot ::V2 ::Webhook ::Event . create ( **event )
7466 end
7567 end
7668
@@ -96,133 +88,6 @@ def secure_compare(a, b)
9688 b . each_byte { |byte | res |= byte ^ l . shift }
9789 res == 0
9890 end
99-
100- def create_instance ( klass , attributes )
101- keyword_args = { }
102-
103- attributes . each do |key , value |
104- if value . is_a? ( Hash )
105- nested_class_name = determine_class_name ( key , value )
106- if nested_class_name
107- nested_klass = Object . const_get ( nested_class_name )
108- value = create_instance ( nested_klass , value )
109- end
110- elsif value . is_a? ( Array )
111- value = value . map do |item |
112- nested_class_name = determine_class_name ( key , item )
113- if nested_class_name
114- nested_klass = Object . const_get ( nested_class_name )
115- create_instance ( nested_klass , item )
116- else
117- item
118- end
119- end
120- end
121-
122- # CodeGen add _ prefix to reserved words
123- # see: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractRubyCodegen.java
124- if Line ::Bot ::V2 ::RESERVED_WORDS . include? ( key )
125- keyword_args [ "_#{ key } " . to_sym ] = value
126- else
127- keyword_args [ key ] = value
128- end
129- end
130-
131- begin
132- klass . new ( **keyword_args )
133- rescue ArgumentError => e
134- attributes # Return the original hash if unknown keyword error occurs
135- end
136- end
137-
138- # TODO: Derive classes from rbs information or define attribute/class mappings for each class and derive classes from them.
139- def determine_class_name ( key , value )
140- class_name = if key == :events && value . is_a? ( Hash ) && value [ :type ] == 'delivery'
141- 'PnpDeliveryCompletionEvent'
142- elsif key == :message && value . is_a? ( Hash ) && value [ :type ]
143- pascalize ( value [ :type ] ) + 'MessageContent'
144- elsif key == :mentionees && value . is_a? ( Hash ) && value [ :type ]
145- pascalize ( value [ :type ] ) + 'Mentionee'
146- elsif key == :module && value . is_a? ( Hash ) && value [ :type ]
147- pascalize ( value [ :type ] ) + 'ModuleContent'
148- elsif key == :things && value . is_a? ( Hash ) && value [ :type ]
149- pascalize ( value [ :type ] ) + 'ThingsContent'
150- elsif value . is_a? ( Hash ) && value [ :type ] && [ 'user' , 'group' , 'room' ] . include? ( value [ :type ] )
151- pascalize ( value [ :type ] ) + 'Source'
152- elsif key == :result && value . is_a? ( Hash ) && value [ :result_code ]
153- 'ScenarioResult'
154- elsif key == :content_provider # This has type, but there is no typed ContentProvider
155- 'ContentProvider'
156- elsif key == :_module # This has type, but there is no typed ModuleContent
157- 'ModuleContent'
158- elsif key == :unsend
159- 'UnsendDetail'
160- elsif key == :follow
161- 'FollowDetail'
162- elsif key == :joined
163- 'JoinedMembers'
164- elsif key == :left
165- 'LeftMembers'
166- elsif key == :postback
167- 'PostbackContent'
168- elsif key == :beacon
169- 'BeaconContent'
170- elsif key == :link
171- 'LinkContent'
172- elsif key == :delivery
173- 'PnpDelivery'
174- elsif value . is_a? ( Hash ) && value [ :type ] # Event etc.
175- pascalize ( value [ :type ] ) + singularize ( pascalize ( key ) )
176- elsif value . is_a? ( Array ) && key . to_s . end_with? ( 's' )
177- pascalize ( singularize ( key ) )
178- else
179- singularize ( pascalize ( key ) )
180- end
181-
182- full_class_name = "Line::Bot::V2::Webhook::#{ class_name } "
183- Object . const_defined? ( full_class_name ) ? full_class_name : nil
184- end
185-
186- def pascalize ( str )
187- str . to_s . gsub ( /(?:^|_)([a-z])/ ) { ::Regexp . last_match ( 1 ) . upcase }
188- end
189-
190- def singularize ( str )
191- str = str . to_s
192-
193- if str . end_with? ( 'ies' )
194- str . sub ( /ies$/ , 'y' )
195- elsif str . end_with? ( 'ves' )
196- str . sub ( /ves$/ , 'f' )
197- elsif str . end_with? ( 'oes' ) || str . end_with? ( 'xes' ) || str . end_with? ( 'ses' )
198- str . sub ( /es$/ , '' )
199- elsif str . end_with? ( 's' )
200- str . sub ( /s$/ , '' )
201- else
202- str
203- end
204- end
205-
206- def deep_hash_to_struct ( obj )
207- case obj
208- when Hash
209- keys = obj . keys . map ( &:to_sym )
210- struct_class = Struct . new ( *keys )
211- struct_class . new ( *obj . values . map { |value | deep_hash_to_struct ( value ) } )
212- when Array
213- obj . map { |item | deep_hash_to_struct ( item ) }
214- else
215- if obj . respond_to? ( :instance_variables )
216- obj . instance_variables . each do |var |
217- value = obj . instance_variable_get ( var )
218- if value . is_a? ( Hash ) || value . is_a? ( Array ) || value . respond_to? ( :instance_variables )
219- obj . instance_variable_set ( var , deep_hash_to_struct ( value ) )
220- end
221- end
222- end
223- obj
224- end
225- end
22691 end
22792 end
22893 end
0 commit comments