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

Inherits:
Command
  • Object
show all
Defined in:
lib/convenient_service/examples/standard/v1/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.



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

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.



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

def default_value
  @default_value
end

#json_stringObject (readonly)

Returns the value of attribute json_string.



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

def json_string
  @json_string
end

Instance Method Details

#callObject



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

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

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