Class: ConvenientService::Examples::Standard::RequestParams::Utils::JSON::SafeParse

Inherits:
Command
  • Object
show all
Defined in:
lib/convenient_service/examples/standard/request_params/utils/json/safe_parse.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_string, default_value: nil) ⇒ SafeParse

Returns a new instance of SafeParse.



20
21
22
23
# File 'lib/convenient_service/examples/standard/request_params/utils/json/safe_parse.rb', line 20

def initialize(json_string, default_value: nil)
  @json_string = json_string
  @default_value = default_value
end

Instance Attribute Details

#default_valueObject (readonly)

Returns the value of attribute default_value.



18
19
20
# File 'lib/convenient_service/examples/standard/request_params/utils/json/safe_parse.rb', line 18

def default_value
  @default_value
end

#json_stringObject (readonly)

Returns the value of attribute json_string.



18
19
20
# File 'lib/convenient_service/examples/standard/request_params/utils/json/safe_parse.rb', line 18

def json_string
  @json_string
end

Instance Method Details

#callObject



25
26
27
28
29
30
31
32
33
# File 'lib/convenient_service/examples/standard/request_params/utils/json/safe_parse.rb', line 25

def call
  return default_value unless json_string.instance_of?(::String)

  begin
    ::JSON.parse(json_string)
  rescue
    default_value
  end
end