Class: SugarcrmRest::Fetch_Data

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

Class Method Summary collapse

Class Method Details

.adding_fields(connect, module_name, filter, fields) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/sugarcrm_rest.rb', line 99

def self.adding_fields(connect,module_name,filter,fields)
	offset=0
	count=0
	if offset==0
		@array_all_claimants = Array.new
	end
	filter=filter+"&fields="
	fields.each do |f|
		filter=filter+f
		
		filter=filter+"," if (count += 1) < fields.length
	end
	
	result=Hash.new
	i=0
	while offset >= 0 do
		url =''+"#{connect.url}"+"#{module_name}"+"?filter=#{filter}"+'&max_num=100&offset='+offset.to_s
		
		response = Get_Token_Process_Url.execute_uri(connect,url)
		if response.kind_of? Net::HTTPSuccess
			root = JSON.parse response.body 
			offset=root['next_offset'] 
				 
			root['records'].each do |r|
				result[i]={fields[0]=>r[fields[0]]}
				for f in 1..fields.length-1
					result[i]=result[i].merge(fields[f]=>r[fields[f]])
				end
				i=i+1
			end 
		end
	end
	return result.to_json
end

.fetch_all_data(connect, module_name, offset = 0, max_num = 100) ⇒ Object

fetch all data from agiven module



89
90
91
92
93
94
95
96
97
# File 'lib/sugarcrm_rest.rb', line 89

def self.fetch_all_data(connect,module_name,offset=0,max_num=100)
	url = ''+"#{connect.url}"+"#{module_name}"+"?offset="+"#{offset}"+"&max_num="+"+#{max_num}"+''
	response = Get_Token_Process_Url.execute_uri(connect,url)
	if response.kind_of? Net::HTTPSuccess
		root = JSON.parse response.body
		
		return root
	end
end

.fetch_data_with_filters(connect, module_name, filter_json, offset = 0, fields = ["no"]) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/sugarcrm_rest.rb', line 134

def self.fetch_data_with_filters(connect,module_name,filter_json,offset=0,fields=["no"])
	filter=filter_json
	
	
	if (fields[0]=="no")
	
		url =''+"#{connect.url}"+"#{module_name}"+"?filter=#{filter_json}"+'&max_num=100&offset='+offset.to_s
		
		response = Get_Token_Process_Url.execute_uri(connect,url)
	
		root = JSON.parse response.body 
	
		if response.kind_of? Net::HTTPSuccess
			root = JSON.parse response.body 
			return root 
		end 
	else
		return adding_fields(connect,module_name,filter,fields)
	end
	
end

.fetch_single_record(connect, module_name, id) ⇒ Object

Fetch a single record by the given id from given module



80
81
82
83
84
85
86
87
# File 'lib/sugarcrm_rest.rb', line 80

def self.fetch_single_record(connect,module_name,id)  
	url = ''+"#{connect.url}"+"#{module_name}/#{id}"
	response = Get_Token_Process_Url.execute_uri(connect,url)
	if response.kind_of? Net::HTTPSuccess
		root = JSON.parse response.body
		return root
	end
end

.upload_document(connect, id, path) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/sugarcrm_rest.rb', line 157

def self.upload_document(connect,id,path)

	@token=Get_Token_Process_Url.authenticate(connect)
	f=File.new(path,'rb')
	response =  RestClient::Request.execute(
 				:url => "#{connect.url}/Documents/#{id}/file/filename", 
 				:method => :post, 
			 	:headers => {:'OAuth-Token' => @token},
			 	:payload => {:filename => f},
 				:verify_ssl => false
			)
			
		 if(response.code==200)
			root = JSON.parse response.body 
			record=root['record']
		end
			return record['id']
		

end