Class: Vtiger::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/vtiger/base.rb

Direct Known Subclasses

Commands

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_keyObject

Returns the value of attribute access_key.



73
74
75
# File 'lib/vtiger/base.rb', line 73

def access_key
  @access_key
end

#campaigndbObject

Returns the value of attribute campaigndb.



73
74
75
# File 'lib/vtiger/base.rb', line 73

def campaigndb
  @campaigndb
end

#endpoint_urlObject

Returns the value of attribute endpoint_url.



73
74
75
# File 'lib/vtiger/base.rb', line 73

def endpoint_url
  @endpoint_url
end

#md5Object

Returns the value of attribute md5.



73
74
75
# File 'lib/vtiger/base.rb', line 73

def md5
  @md5
end

#session_nameObject

Returns the value of attribute session_name.



73
74
75
# File 'lib/vtiger/base.rb', line 73

def session_name
  @session_name
end

#tokenObject

Returns the value of attribute token.



73
74
75
# File 'lib/vtiger/base.rb', line 73

def token
  @token
end

#urlObject

Returns the value of attribute url.



73
74
75
# File 'lib/vtiger/base.rb', line 73

def url
  @url
end

#useridObject

Returns the value of attribute userid.



73
74
75
# File 'lib/vtiger/base.rb', line 73

def userid
  @userid
end

#usernameObject

Returns the value of attribute username.



73
74
75
# File 'lib/vtiger/base.rb', line 73

def username
  @username
end

Instance Method Details

#accessdatabase(dbhost, dbname, dbuser, dbpasswd) ⇒ Object

self.session_name=result[“sessionName”]

puts JSON.pretty_generate(result)


255
256
257
258
259
260
261
# File 'lib/vtiger/base.rb', line 255

def accessdatabase(dbhost, dbname, dbuser,dbpasswd)
  #select vtiger_contactdetails.email, vtiger_contactdetails.firstname, vtiger_contactdetails.lastname,  vtiger_campaigncontrel.campaignid from vtiger_contactdetails left join vtiger_campaigncontrel on vtiger_contactdetails.contactid=vtiger_campaigncontrel.contactid where vtiger_campaigncontrel.campaignid='14' and emailoptout=0;
  #self.campaigndb=CampaignList.new
  CampaignList.scott_connect(dbhost, dbname, dbuser,dbpasswd)
  
  
end

#accessdatabase2(dbhost, dbname, dbuser, dbpasswd) ⇒ Object



262
263
264
265
266
267
268
# File 'lib/vtiger/base.rb', line 262

def accessdatabase2(dbhost, dbname, dbuser,dbpasswd)
  #select vtiger_contactdetails.email, vtiger_contactdetails.firstname, vtiger_contactdetails.lastname,  vtiger_campaigncontrel.campaignid from vtiger_contactdetails left join vtiger_campaigncontrel on vtiger_contactdetails.contactid=vtiger_campaigncontrel.contactid where vtiger_campaigncontrel.campaignid='14' and emailoptout=0;
  #self.campaigndb=CampaignList.new
  CampaignList.scott_connect2(dbhost, dbname, dbuser,dbpasswd)
  
  
end

#add_object(object_map, hashv, element) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/vtiger/base.rb', line 153

def add_object(object_map,hashv,element)
  object_map=object_map.merge hashv
  # 'tsipid'=>"1234"
  tmp=self.json_please(object_map)
  input_array ={'operation'=>'create','elementType'=>"#{element}",'sessionName'=>"#{self.session_name}", 'element'=>tmp} # removed the true
 # puts "input array:"  + input_array.to_s   #&username=#{self.username}&accessKey=#{self.md5}
  # scott not working -- JSON.generate(input_array,{'array_nl'=>'true'})
  result = http_crm_post("operation=create",input_array)
 # self.session_name=result["result"]["sessionName"]
  # puts JSON.pretty_generate(result)
  success=result['success']
  id =result["result"]['id'] if success
  return success,id
end

#addobject(options) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/vtiger/base.rb', line 206

def addobject(options)
          puts "in addobject"
          object_map= { 'assigned_user_id'=>"#{self.userid}",'lastname'=>"#{options[:contact]}",'cf_554'=>"1234"}
          # 'tsipid'=>"1234"
          tmp=self.json_please(object_map)
          input_array ={'operation'=>'create','elementType'=>"#{options[:element_type]}",'sessionName'=>"#{self.session_name}", 'element'=>tmp} # removed the true
         # puts "input array:"  + input_array.to_s   #&username=#{self.username}&accessKey=#{self.md5}
          # scott not working -- JSON.generate(input_array,{'array_nl'=>'true'})
          result = http_crm_post("operation=create",input_array)
         # self.session_name=result["result"]["sessionName"]
         #  puts JSON.pretty_generate(result)
end

#challenge(options) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/vtiger/base.rb', line 75

def challenge(options)
    #   puts "in challenge"
       self.url=options[:url] || Vtiger::Api.api_settings[:url]
       self.username = options[:username]|| Vtiger::Api.api_settings[:username]
       self.access_key = options[:key] || Vtiger::Api.api_settings[:key]
       self.endpoint_url="http://#{self.url}/webservice.php?"
       operation = "operation=getchallenge&username=#{self.username}"; 
        #puts "challenge: " + self.endpoint_url + operation
        r=http_ask_get(self.endpoint_url+operation)
        self.token = r["result"]["token"] #if r["success"]==true

         create_digest
      #  puts "digest is: #{self.md5} token #{self.token}" 
        self.token!=nil
end

#create_digestObject



90
91
92
93
94
95
# File 'lib/vtiger/base.rb', line 90

def create_digest
  #access key from my_preferences page of vtiger
  digest_string="#{self.token}#{self.access_key}"
  self.md5=Digest::MD5.hexdigest(digest_string)
 # puts "#{self.url}: string #{digest_string} results in digest: "+self.md5 + " access key: "+ self.access_key + " token: " + self.token
end

#describe_object(options) ⇒ Object

puts JSON.pretty_generate(result)



196
197
198
199
200
201
202
203
204
205
# File 'lib/vtiger/base.rb', line 196

def describe_object(options)
          puts "in describe object"
           #&username=#{self.username}&accessKey=#{self.md5}
            # scott not working -- JSON.generate(input_array,{'array_nl'=>'true'})
            result = http_ask_get(self.endpoint_url+"operation=describe&sessionName=#{self.session_name}&elementType=#{options[:element_type]}")
           # puts JSON.pretty_generate(result)
         
          puts "#{result.inspect}"    #scott  tmp=JSON.generate(object_map)
           
end

#get_contacts_by_cf(field, value) ⇒ Object



281
282
283
284
# File 'lib/vtiger/base.rb', line 281

def get_contacts_by_cf(field,value)
  #self.campaigndb.find_contacts_by_campaign(self.campaigndb,campaignid)
  CampaignList.find_contacts_by_customfield(field,value)
end

#get_contacts_by_email_and_keynull(field, value) ⇒ Object



285
286
287
288
# File 'lib/vtiger/base.rb', line 285

def get_contacts_by_email_and_keynull(field,value)
  #self.campaigndb.find_contacts_by_campaign(self.campaigndb,campaignid)
  CampaignList.find_contacts_by_email_and_keynull(field,value)
end

#get_contacts_from_campaign(campaignid) ⇒ Object



276
277
278
279
280
# File 'lib/vtiger/base.rb', line 276

def get_contacts_from_campaign(campaignid)
  #self.campaigndb.find_contacts_by_campaign(self.campaigndb,campaignid)
  CampaignList.find_contacts_by_campaign(campaignid)
  
end

#get_list_from_campaign(campaignid, type) ⇒ Object



269
270
271
272
273
274
275
# File 'lib/vtiger/base.rb', line 269

def get_list_from_campaign(campaignid,type)
  #self.campaigndb.find_contacts_by_campaign(self.campaigndb,campaignid)
  list=CampaignList.find_leads_by_campaign(campaignid) if type=='Leads'
  list=CampaignList.find_accounts_by_campaign(campaignid) if type=='Accounts'
  list=CampaignList.find_contacts_by_campaign(campaignid) if type=='Contacts'
  list
end

#http_ask_get(input_url) ⇒ Object

r



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/vtiger/base.rb', line 112

def http_ask_get(input_url)
 # puts "about to HTTP.get on '#{input_url}'"
 # url=ERB::Util.url_encode(input_url)
 # resp= Net::HTTP.get(URI.parse(url))
  url = URI.parse(input_url)
 # puts "inspect url: " + url.inspect
      req = Net::HTTP::Get.new("#{url.path}?#{url.query}")
      resp = Net::HTTP.start(url.host, url.port) {|http|
    #    puts "url path is #{url.path}"
        http.request(req)
      }
   # puts "HTTP_ASK_GET" + resp.body.to_s


 # puts "resp: " + resp 
  self.json_parse resp.body
       #   r
end

#http_crm_post(operation, body) ⇒ Object

puts “#Vtiger::Base.selfself.url: string #digest_string results in digest: ”+self.md5 + “ access key: ”+ self.access_key + “ token: ” + self.token



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/vtiger/base.rb', line 96

def http_crm_post(operation, body)
  response = nil
  response_header = {"Content-type" => "application/x-www-form-urlencoded"}
  #puts " endpoint: #{self.endpoint_url}"
   t=URI.split(self.endpoint_url.to_s)
  # puts "host is: " + t[2]   #FIX THIS.
   ht =Net::HTTP.start(t[2],80)
   body_enc=body.url_encode
  # puts "attemping post: #{self.endpoint_url}#{operation} body: #{body} body_enc= #{body_enc}"
   resp=ht.post(self.endpoint_url+operation,body_enc,response_header)

   # p response.body.to_s
   self.json_parse resp.body
   # r=JSON.parse response.body
  #  r
end

#json_parse(incoming) ⇒ Object

self.session_name=result[“sessionName”]

puts JSON.pretty_generate(result)


218
219
220
221
222
# File 'lib/vtiger/base.rb', line 218

def json_parse(incoming)
   json = StringIO.new(incoming)
   parser = Yajl::Parser.new
   hash = parser.parse(json)
end

#json_please(object_map) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/vtiger/base.rb', line 223

def json_please(object_map)
   if defined? RAILS_ENV
       #puts "in JSON code rails env: #{RAILS_ENV}"
        str=object_map.to_json
    else
     # puts "rails env is not defined"
   #   json = StringIO.new()
      str = Yajl::Encoder.encode(object_map)
      
   #    parser = Yajl::Parser.new
    #   tmp = parser.parse(object_map.to_s)
        #   object_map.to_json    #  can remove eventually this if statements
      
     # tmp=YAJL.generate(object_map)   #scott  tmp=JSON.generate(object_map)
    end
    str
end

#large_query(countquery, query) ⇒ Object

r



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/vtiger/base.rb', line 130

def large_query(countquery, query)
     qaction_string=ERB::Util.url_encode("#{countquery};")
    res=http_ask_get(self.endpoint_url+"operation=query&sessionName=#{self.session_name}&query="+qaction_string)
  #    puts "action string:" +action_string
  puts "success: #{res['success']} res class #{res} count #{res['result']}"
      temp=res['result'][0]    
      puts "  temp: #{temp} class #{temp.class} inspect #{temp.inspect}"
      count=temp['count'].to_i
      s=count/100
      puts "s is #{s}"
      output=[]
      finalres=true
      0.upto(s) { |i| puts i 
          action_string=ERB::Util.url_encode("#{query} limit #{i*100},100;")
          puts "COUNT :#{i} #{action_string}"
          res = http_ask_get(self.endpoint_url+"operation=query&sessionName=#{self.session_name}&query="+action_string)
          values=res["result"] if res["success"]==true
          finalres=finalres && res["success"]
          values.each {|i|  output << i }
         # output << values
        }  
     return finalres, output
end

#login(options) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/vtiger/base.rb', line 167

def (options)
 # puts "in login"
  input_array ={'operation'=>'login', 'username'=>self.username, 'accessKey'=>self.md5} # removed the true
  #puts "input array:"  + input_array.to_s   #&username=#{self.username}&accessKey=#{self.md5}
  # scott not working -- JSON.generate(input_array,{'array_nl'=>'true'})
  result = http_crm_post("operation=login",input_array)
  self.session_name=result["result"]["sessionName"] if result["result"]!=nil
  self.userid = result["result"]["userId"] if result["result"]!=nil
#  puts "session name is: #{self.session_name} userid #{self.userid}"
  self.userid!=nil
end

#query(options) ⇒ Object



187
188
189
190
191
192
193
194
195
# File 'lib/vtiger/base.rb', line 187

def query(options)
        puts "in query object"
         #&username=#{self.username}&accessKey=#{self.md5}
          # scott not working -- JSON.generate(input_array,{'array_nl'=>'true'})
          action_string=ERB::Util.url_encode("#{options[:query]}")
          result = http_ask_get(self.endpoint_url+"operation=query&sessionName=#{self.session_name}&query="+action_string)
          # http_ask_get(self.endpoint_url+"operation=query&sessionName=#{self.session_name}&userId=#{self.userid}&query="+action_string)
       #   puts JSON.pretty_generate(result)
end

#retrieve_object(objid) ⇒ Object



178
179
180
181
182
183
184
185
186
# File 'lib/vtiger/base.rb', line 178

def retrieve_object(objid)
            puts "in retrieve object"
             #&username=#{self.username}&accessKey=#{self.md5}
              # scott not working -- JSON.generate(input_array,{'array_nl'=>'true'})
              result = http_ask_get(self.endpoint_url+"operation=retrieve&sessionName=#{self.session_name}&id=#{objid}")
           #   puts JSON.pretty_generate(result)
               values=result["result"]
               values
end

#updateobject(values) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/vtiger/base.rb', line 240

def updateobject(values)
            #puts "in updateobject"
           # object_map= { 'assigned_user_id'=>"#{self.userid}"}.merge  values.to_hash
           object_map= {'id'=>"#{self.object_id}" }.merge values.to_hash
            #object_map['assigned_user_id']="#{self.userid}"
            # 'tsipid'=>"1234"
           # puts "object map is #{object_map.to_s}"
            tmp=self.json_please(object_map)
            input_array ={'operation'=>'update','sessionName'=>"#{self.session_name}", 'element'=>tmp} # removed the true
         #   puts "input array:"  + input_array.to_s   #&username=#{self.username}&accessKey=#{self.md5}
            # scott not working -- JSON.generate(input_array,{'array_nl'=>'true'})
            result = http_crm_post("operation=update",input_array)
           # self.session_name=result["result"]["sessionName"]
           #  puts JSON.pretty_generate(result)
end