Handle hyphens in locale
when fetching Articles with translated_content
#610
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Certain locales include hyphens, which causes trouble (i.e. a
SyntaxError
) when dynamically defining method names.This change converts the hyphen to an underscore for safety.
Why?
Without this, calling e.g.
intercom_client.articles.all.to_a
will raise aSyntaxError
if the articles have translated content, as it tries to define a method with a hyphen for certain locales (e.g.pt-BR
).How?
The update checks for
-
in attributes and, if present, replaces with_
before defining accessors and using the setter.I experimented with pushing this lower (into the
DynamicAccessors
) but theApiResource#initialize_property
immediately callsset_property
using the sameattribute
name, so we'd then end up having to do this sanitization in multiple places which felt weird.Other thoughts
When writing the specs I was tempted to add a new directory, e.g.
spec/fixtures/articles/
, and store the sample article JSON there vs. pasting it directly inspec_helper
. For simplicity/consistency I mimicked the current approach but wanted to share my instinct in case useful input 😅.I also considered trimming the test article hash down to just a couple representative locales to keep it shorter (i.e. a local without a hyphen and a locale with a hyphen), but I thought it better to keep the representation as a mirror of what is actually returned.