Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/fields translations #30

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions lib/api_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def self.string_to_method_name(s)

def self.string_to_symbol(s)
s.gsub!(/[()%]*/, '')
s.gsub(' ', '_').downcase.to_sym
s.gsub(/[\s\/]/, '_').downcase.to_sym
end

def self.symbol_to_string(sym)
sym.class == Symbol ? self.camelize_with_space(sym.to_s) : self.camelize_with_space(sym)
end

end
end
8 changes: 4 additions & 4 deletions lib/zoho_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def add_record(module_name, fields_values_hash)
x = REXML::Document.new
element = x.add_element module_name
row = element.add_element 'row', { 'no' => '1' }
fields_values_hash.each_pair { |k, v| add_field(row, ApiUtils.symbol_to_string(k), v) }
fields_values_hash.each_pair { |k, v| add_field(row, k, v, module_name) }
r = self.class.post(create_url(module_name, 'insertRecords'),
:query => { :newFormat => 1, :authtoken => @auth_token,
:scope => 'crmapi', :xmlData => x, :wfTrigger => 'true' },
Expand Down Expand Up @@ -149,8 +149,8 @@ def update_related_records(parent_module, parent_record_id, related_module_field
x = REXML::Document.new
leads = x.add_element related_module_fields[:related_module]
row = leads.add_element 'row', { 'no' => '1' }
related_module_fields[:xml_data].each_pair { |k, v| add_field(row, ApiUtils.symbol_to_string(k), v) }
related_module_fields[:xml_data].each_pair { |k, v| add_field(row, k, v, parent_module) }

r = self.class.post(create_url("#{parent_module}", 'updateRelatedRecords'),
:query => { :newFormat => 1,
:id => parent_record_id,
Expand All @@ -166,7 +166,7 @@ def update_record(module_name, id, fields_values_hash)
x = REXML::Document.new
contacts = x.add_element module_name
row = contacts.add_element 'row', { 'no' => '1' }
fields_values_hash.each_pair { |k, v| add_field(row, ApiUtils.symbol_to_string(k), v) }
fields_values_hash.each_pair { |k, v| add_field(row, k, v, module_name) }
r = self.class.post(create_url(module_name, 'updateRecords'),
:query => { :newFormat => 1, :authtoken => @auth_token,
:scope => 'crmapi', :id => id,
Expand Down
20 changes: 15 additions & 5 deletions lib/zoho_api_field_utils.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
module ZohoApiFieldUtils

@@module_fields = {}
@@module_translation_fields = {}
@@users = []

def add_field(row, field, value)
def add_field(row, field, value, module_name)
r = (REXML::Element.new 'FL')
adjust_tag_case(field)
r.attributes['val'] = adjust_tag_case(field)
r.attributes['val'] = adjust_tag_case(field.to_s, module_name)
r.add_text("#{value}")
row.elements << r
row
end

def adjust_tag_case(tag)
def adjust_tag_case(tag, module_name)
return tag if tag == 'id'
return tag.upcase if tag.downcase.rindex('id')
u_tags = %w[SEMODULE]
return tag.upcase if u_tags.index(tag.upcase)
tag

if @@module_translation_fields[module_name].blank?
tag
else
@@module_translation_fields[module_name][tag] || tag
end
end

def clean_field_name?(field_name)
Expand Down Expand Up @@ -71,6 +76,9 @@ def create_and_add_field_value_pair(field_name, module_name, n, record)
v = n.text == 'null' ? nil : n.text
r = record.merge({ k => v })
r = r.merge({ :id => v }) if primary_key?(module_name, k)

@@module_translation_fields[module_name] ||= {}
@@module_translation_fields[module_name][k.to_s] = field_name.to_s
r
end

Expand All @@ -91,6 +99,8 @@ def extract_field(f, mod_name)
field = ApiUtils.string_to_symbol(f.to_s)
@@module_fields[mod_name] << field if method_name?(field)
@@module_fields[(mod_name.to_s + '_original_name').to_sym] << field
@@module_translation_fields[mod_name] ||= {}
@@module_translation_fields[mod_name][field.to_s] = f.to_s
end

def to_hash(xml_results, module_name)
Expand Down