Module: Jekyll::GitHubMetadata::Sanitizer

Extended by:
Sanitizer
Included in:
Sanitizer
Defined in:
lib/jekyll-github-metadata/sanitizer.rb

Instance Method Summary collapse

Instance Method Details

#sanitize(resource) ⇒ Object

Sanitize an object. When the resource is either ‘false`, `true`, `nil` or a number,

it returns the resource as-is. When the resource is an array,
it maps over the entire array, sanitizing each of its values.
When the resource responds to the #to_hash method, it returns
the value of #sanitize_resource (see below). If none of the
aforementioned conditions are met, the return value of #to_s
is used.

resource - an Object

Returns the sanitized resource. rubocop:disable Metrics/CyclomaticComplexity



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jekyll-github-metadata/sanitizer.rb', line 20

def sanitize(resource)
  case resource
  when Array
    resource.map { |item| sanitize(item) }
  when Numeric, Time
    resource
  when FalseClass
    false
  when TrueClass
    true
  when NilClass
    nil
  when String
    resource
  else
    if resource.respond_to?(:to_hash)
      sanitize_resource(resource)
    else
      resource.to_s
    end
  end
end

#sanitize_resource(resource) ⇒ Object

Sanitize the Sawyer Resource or Hash Note: the object must respond to :to_hash for this to work. Consider using #sanitize instead of this method directly.

resource - an Object which responds to :to_hash

Returns the sanitized sawyer resource or hash as a hash.



51
52
53
54
55
56
# File 'lib/jekyll-github-metadata/sanitizer.rb', line 51

def sanitize_resource(resource)
  resource.to_hash.each_with_object({}) do |(k, v), result|
    result[k.to_s] = sanitize(v)
    result
  end
end