Module: ZohoApiFieldUtils

Included in:
ZohoApi::Crm
Defined in:
lib/zoho_api_field_utils.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#add_field(row, field, value) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/zoho_api_field_utils.rb', line 6

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

#adjust_tag_case(tag) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/zoho_api_field_utils.rb', line 15

def adjust_tag_case(tag)
  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
end

#clean_field_name?(field_name) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/zoho_api_field_utils.rb', line 23

def clean_field_name?(field_name)
  return false if field_name.nil?
  r = field_name[/[0-9, a-z, A-Z, _]*/]
  field_name.size == r.size
end

#create_and_add_field_value_pair(field_name, module_name, n, record) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/zoho_api_field_utils.rb', line 69

def create_and_add_field_value_pair(field_name, module_name, n, record)
  k = ApiUtils.string_to_symbol(field_name)
  v = n.text == 'null' ? nil : n.text
  r = record.merge({ k => v })
  r = r.merge({ :id => v }) if primary_key?(module_name, k)
  r
end

#extract_field(f, mod_name) ⇒ Object



90
91
92
93
94
# File 'lib/zoho_api_field_utils.rb', line 90

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
end

#extract_fields_from_response(mod_name, module_name, response) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/zoho_api_field_utils.rb', line 82

def extract_fields_from_response(mod_name, module_name, response)
  x = REXML::Document.new(response.body)
  REXML::XPath.each(x, "/#{module_name}/section/FL/@dv") do |field|
    extract_field(field, mod_name)
  end
  @@module_fields[mod_name] << ApiUtils.string_to_symbol(module_name.chop + 'id')
end

#fields(module_name) ⇒ Object



29
30
31
32
# File 'lib/zoho_api_field_utils.rb', line 29

def fields(module_name)
  return user_fields if module_name == 'Users'
  fields_from_record(module_name).nil? ? fields_from_api(module_name) : fields_from_record(module_name)
end

#fields_from_api(module_name) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/zoho_api_field_utils.rb', line 40

def fields_from_api(module_name)
  mod_name = ApiUtils.string_to_symbol(module_name)
  return @@module_fields[mod_name] unless @@module_fields[mod_name].nil?
  r = self.class.post(create_url(module_name, 'getFields'),
                      :query => { :authtoken => @auth_token, :scope => 'crmapi' },
                      :headers => { 'Content-length' => '0' })
  check_for_errors(r)
  update_module_fields(mod_name, module_name, r)
end

#fields_from_record(module_name) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/zoho_api_field_utils.rb', line 50

def fields_from_record(module_name)
  mod_name = ApiUtils.string_to_symbol(module_name)
  return @@module_fields[mod_name] unless @@module_fields[mod_name].nil?
  r = first(module_name)
  return nil if r.nil?
  @@module_fields[mod_name] = r.first.keys
  @@module_fields[mod_name]
end

#fields_original(module_name) ⇒ Object



34
35
36
37
38
# File 'lib/zoho_api_field_utils.rb', line 34

def fields_original(module_name)
  return nil if @@module_fields.nil?
  #return user_fields if module_name == 'Users'
  @@module_fields[module_name + '_original_name']
end

#hashed_field_value_pairs(module_name, n, record) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/zoho_api_field_utils.rb', line 59

def hashed_field_value_pairs(module_name, n, record)
  field_name = n.attribute('val').to_s.gsub('val=', '')
  if @ignore_fields == true
    return clean_field_name?(field_name) == true ?
        create_and_add_field_value_pair(field_name, module_name, n, record)
    : record
  end
  create_and_add_field_value_pair(field_name, module_name, n, record)
end

#reflect_module_fieldsObject



77
78
79
80
# File 'lib/zoho_api_field_utils.rb', line 77

def reflect_module_fields
  @modules.each { |m| fields(m) }
  @@module_fields
end

#to_hash(xml_results, module_name) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/zoho_api_field_utils.rb', line 96

def to_hash(xml_results, module_name)
  r = []
  xml_results.each do |e|
    record = {}
    record[:module_name] = module_name
    e.elements.to_a.each do |n|
      record = hashed_field_value_pairs(module_name, n, record)
    end
    r << record unless record.nil?
  end
  return nil if r == []
  r
end

#to_hash_with_id(xml_results, module_name) ⇒ Object



110
111
112
# File 'lib/zoho_api_field_utils.rb', line 110

def to_hash_with_id(xml_results, module_name)
  to_hash(xml_results, module_name)
end

#update_module_fields(mod_name, module_name, response) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/zoho_api_field_utils.rb', line 114

def update_module_fields(mod_name, module_name, response)
  @@module_fields[mod_name] = []
  @@module_fields[(mod_name.to_s + '_original_name').to_sym] = []
  extract_fields_from_response(mod_name, module_name, response)
  return @@module_fields[mod_name] unless @@module_fields.nil?
  nil
end

#user_fieldsObject



122
123
124
# File 'lib/zoho_api_field_utils.rb', line 122

def user_fields
  @@module_fields[:users] = users[0].keys
end