Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 2
version: 2.1
jobs:
"Test against Ruby 2.4":
test-ruby-2-4:
docker:
- image: circleci/ruby:2.4.9
working_directory: ~/intercom-ruby
Expand All @@ -27,7 +27,7 @@ jobs:
|| { echo "FAIL: bundler mirror not pointing at Socket Firewall"; exit 1; }
- run: bundle install
- run: bundle exec rake
"Test against Ruby 2.5":
test-ruby-2-5:
docker:
- image: circleci/ruby:2.5.7
working_directory: ~/intercom-ruby
Expand All @@ -50,7 +50,7 @@ jobs:
|| { echo "FAIL: bundler mirror not pointing at Socket Firewall"; exit 1; }
- run: bundle install
- run: bundle exec rake
"Test against Ruby 2.6":
test-ruby-2-6:
docker:
- image: circleci/ruby:2.6.5
working_directory: ~/intercom-ruby
Expand All @@ -75,15 +75,17 @@ jobs:
- run: bundle exec rake

workflows:
version: 2
build_and_test:
jobs:
- "Test against Ruby 2.4":
- test-ruby-2-4:
name: "Test against Ruby 2.4"
context:
- socket-firewall
- "Test against Ruby 2.5":
- test-ruby-2-5:
name: "Test against Ruby 2.5"
context:
- socket-firewall
- "Test against Ruby 2.6":
- test-ruby-2-6:
name: "Test against Ruby 2.6"
context:
- socket-firewall
55 changes: 31 additions & 24 deletions lib/intercom/lib/dynamic_accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,46 @@ def define_accessors(attribute, value, object)
private

def define_flat_store_based_accessors(attribute, value, object)
object.instance_eval %Q"
def #{attribute}=(value)
mark_field_as_changed!(:#{attribute})
@#{attribute} = Intercom::Lib::FlatStore.new(value)
ivar = ivar_name(attribute, object)
object.singleton_class.class_eval do
define_method("#{attribute}=") do |val|
mark_field_as_changed!(attribute.to_sym)
instance_variable_set(ivar, Intercom::Lib::FlatStore.new(val))
end
def #{attribute}
@#{attribute}
end
"
define_method(attribute) { instance_variable_get(ivar) }
end
end

def define_date_based_accessors(attribute, value, object)
object.instance_eval %Q"
def #{attribute}=(value)
mark_field_as_changed!(:#{attribute})
@#{attribute} = value.nil? ? nil : value.to_i
ivar = ivar_name(attribute, object)
object.singleton_class.class_eval do
define_method("#{attribute}=") do |val|
mark_field_as_changed!(attribute.to_sym)
instance_variable_set(ivar, val.nil? ? nil : val.to_i)
end
def #{attribute}
@#{attribute}.nil? ? nil : Time.at(@#{attribute})
define_method(attribute) do
time = instance_variable_get(ivar)
time.nil? ? nil : Time.at(time)
end
"
end
end

def define_standard_accessors(attribute, value, object)
object.instance_eval %Q"
def #{attribute}=(value)
mark_field_as_changed!(:#{attribute})
@#{attribute} = value
end
def #{attribute}
@#{attribute}
end
"
ivar = ivar_name(attribute, object)
object.singleton_class.class_eval do
define_method("#{attribute}=") do |val|
mark_field_as_changed!(attribute.to_sym)
instance_variable_set(ivar, val)
end
define_method(attribute) { instance_variable_get(ivar) }
end
end

# Keys such as "pt-BR" are not valid identifiers, so normalise them for the ivar name.
def ivar_name(attribute, object)
ivar = "@#{attribute.to_s.gsub(/\W/, '_')}"
object.register_attribute_key(ivar, attribute) if ivar != "@#{attribute}"
ivar
end

end
Expand Down
12 changes: 10 additions & 2 deletions lib/intercom/traits/api_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def from_hash(hash)

def to_hash
instance_variables_excluding_dirty_tracking_field.each_with_object({}) do |variable, hash|
hash[variable.to_s.delete('@')] = instance_variable_get(variable)
hash[attribute_key(variable)] = instance_variable_get(variable)
end
end

Expand All @@ -46,10 +46,18 @@ def to_json(*args)
next if variable == :@client

value = instance_variable_get(variable)
hash[variable.to_s.delete('@')] = value.respond_to?(:to_json) ? value.to_json(*args) : value
hash[attribute_key(variable)] = value.respond_to?(:to_json) ? value.to_json(*args) : value
end
end

def register_attribute_key(ivar, attribute)
(@attribute_keys ||= {})[ivar.to_sym] = attribute.to_s
end

def attribute_key(variable)
(@attribute_keys || {}).fetch(variable, variable.to_s.delete('@'))
end

def to_submittable_hash
submittable_hash = {}
to_hash.each do |attribute, value|
Expand Down
4 changes: 2 additions & 2 deletions lib/intercom/traits/dirty_tracking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def mark_field_as_changed!(field_name)

def field_changed?(field_name)
@changed_fields ||= Set.new
field = instance_variable_get("@#{field_name}")
field = instance_variable_get("@#{field_name.to_s.gsub(/\W/, '_')}")
if field.respond_to?(:field_changed?)
field.to_hash.any? do |attribute, _|
field.field_changed?(attribute)
Expand All @@ -33,7 +33,7 @@ def field_changed?(field_name)
end

def instance_variables_excluding_dirty_tracking_field
instance_variables.reject{|var| var == :@changed_fields}
instance_variables.reject{|var| var == :@changed_fields || var == :@attribute_keys}
end
end
end
Expand Down
20 changes: 19 additions & 1 deletion spec/unit/intercom/article_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,22 @@
client.articles.delete(article)
end
end
end

describe "Parsing an Article" do
it "handles hyphenated locale keys in translated content" do
article = Intercom::Article.from_api(
"id" => "1",
"type" => "article",
"translated_content" => {
"type" => "article_translated_content",
"fr" => { "type" => "article_content", "title" => "Bonjour" },
"pt-BR" => { "type" => "article_content", "title" => "Ola" }
}
)
_(article.translated_content.fr.title).must_equal "Bonjour"
_(article.translated_content.public_send("pt-BR").title).must_equal "Ola"
article.translated_content.public_send("pt-BR").title = "Oi"
_(article.to_submittable_hash).must_equal("translated_content" => { "pt-BR" => { "title" => "Oi" } })
end
end
end
Loading