Skip to content

Commit

Permalink
Make json the default string representation
Browse files Browse the repository at this point in the history
For Product, Resource and type override the to_s so that it will return pretty
json instead of the name of the class.
  • Loading branch information
chrisst committed Feb 1, 2019
1 parent d627d4a commit c1e0976
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
4 changes: 4 additions & 0 deletions api/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def api_name
name.downcase
end

def to_s
JSON.pretty_generate(self)
end

def to_json(opts = nil)
json_out = {}

Expand Down
4 changes: 4 additions & 0 deletions api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ def validate
end
end

def to_s
JSON.pretty_generate(self)
end

def to_json(opts = nil)
# ignore fields that will contain references to parent resources
ignored_fields = %i[@__product @__parent @__resource @api_name @collection_url_response]
Expand Down
2 changes: 1 addition & 1 deletion api/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def to_json(opts = nil)
instance_variables.each do |v|
if v == :@conflicts && instance_variable_get(v).empty?
# ignore empty conflict arrays
elsif instance_variable_get(v) == false
elsif instance_variable_get(v) == false || instance_variable_get(v).nil?
# ignore false booleans as non-existence indicates falsey
elsif !ignored_fields.include? v
json_out[v] = instance_variable_get(v)
Expand Down

0 comments on commit c1e0976

Please sign in to comment.