Class: ElasticGraph::GraphQL::ScalarCoercionAdapters::TimeZone

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/graphql/scalar_coercion_adapters/time_zone.rb

Constant Summary collapse

SUGGESTER =
::DidYouMean::SpellChecker.new(dictionary: VALID_TIME_ZONES.to_a)

Class Method Summary collapse

Class Method Details

.coerce_input(value, ctx) ⇒ Object

Raises:

  • (::GraphQL::CoercionError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/elastic_graph/graphql/scalar_coercion_adapters/time_zone.rb', line 18

def self.coerce_input(value, ctx)
  return value if value.nil? || VALID_TIME_ZONES.include?(value)

  suggestions = SUGGESTER.correct(value).map(&:inspect)
  suggestion_sentence =
    if suggestions.size >= 3
      *initial, final = suggestions
      " Possible alternatives: #{initial.join(", ")}, or #{final}."
    elsif suggestions.size == 1
      " Possible alternative: #{suggestions.first}."
    elsif suggestions.size > 0
      " Possible alternatives: #{suggestions.join(" or ")}."
    end

  raise ::GraphQL::CoercionError,
    "Could not coerce value #{value.inspect} to TimeZone: must be a valid IANA time zone identifier " \
    "(such as `America/Los_Angeles` or `UTC`).#{suggestion_sentence}"
end

.coerce_result(value, ctx) ⇒ Object



37
38
39
40
# File 'lib/elastic_graph/graphql/scalar_coercion_adapters/time_zone.rb', line 37

def self.coerce_result(value, ctx)
  return value if value.nil? || VALID_TIME_ZONES.include?(value)
  nil
end