Skip to content

Commit 6818c05

Browse files
committed
Refactor JSON parser class
1 parent 32ab1fb commit 6818c05

1 file changed

Lines changed: 8 additions & 20 deletions

File tree

lib/webmention/parsers/json_parser.rb

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,18 @@ class JsonParser < Parser
1010

1111
# @return [Array<String>] An array of absolute URLs.
1212
def results
13-
@results ||= UrlExtractor.extract(doc)
13+
@results ||= extract_urls_from(JSON.parse(response_body))
1414
end
1515

1616
private
1717

18-
# @return [Array, Hash]
19-
def doc
20-
@doc ||= JSON.parse(response_body)
21-
end
22-
23-
module UrlExtractor
24-
# @param *objs [Array<Hash, Array, String, Integer, Boolean, nil>]
25-
#
26-
# @return [Array<String>]
27-
def self.extract(*objs)
28-
objs.flat_map { |obj| values_from(obj) }
29-
end
30-
31-
# @param obj [Hash, Array, String, Integer, Boolean, nil]
32-
#
33-
# @return [Array<String>, String, nil]
34-
def self.values_from(obj)
35-
return obj.flat_map { |value| extract(value) }.compact if obj.is_a?(Array)
36-
return extract(obj.values) if obj.is_a?(Hash)
18+
# @param *objs [Array<Hash, Array, String, Integer, Boolean, nil>]
19+
#
20+
# @return [Array<String>]
21+
def extract_urls_from(*objs)
22+
objs.flat_map do |obj|
23+
return obj.flat_map { |value| extract_urls_from(value) }.compact if obj.is_a?(Array)
24+
return extract_urls_from(obj.values) if obj.is_a?(Hash)
3725

3826
obj if obj.is_a?(String) && obj.match?(Parser::URI_REGEXP)
3927
end

0 commit comments

Comments
 (0)