Class: SugarcrmRest::Fetch_Data
- Inherits:
-
Object
- Object
- SugarcrmRest::Fetch_Data
- Defined in:
- lib/sugarcrm_rest.rb
Class Method Summary collapse
- .adding_fields(connect, module_name, filter, fields) ⇒ Object
- .build_filter(filter_json) ⇒ Object
-
.fetch_all_data(connect, module_name, offset) ⇒ Object
fetch all data from agiven module.
- .fetch_data_with_filters(connect, module_name, filter_json, fields = ["no"], offset = 0) ⇒ Object
-
.fetch_single_record(connect, module_name, id) ⇒ Object
Fetch a single record by the given id from given module.
Instance Method Summary collapse
Class Method Details
.adding_fields(connect, module_name, filter, fields) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/sugarcrm_rest.rb', line 114 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}"+'&max_num=100&offset='+offset.to_s p url 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 |
.build_filter(filter_json) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/sugarcrm_rest.rb', line 98 def self.build_filter(filter_json) filter_hash = JSON.parse(filter_json) filter_keys=JSON.parse(filter_json).keys filter="?" and_op="" count=0 filter_keys.each do |k| f1=filter_hash[k] value=filter_hash[k].keys filter=filter+"filter[0]["+"#{value[0]}"+"]["+"#{k}"+"]="+"#{f1[value[0]]}" filter=filter+"&" if (count += 1) < filter_keys.length end return filter end |
.fetch_all_data(connect, module_name, offset) ⇒ Object
fetch all data from agiven module
88 89 90 91 92 93 94 95 96 |
# File 'lib/sugarcrm_rest.rb', line 88 def self.fetch_all_data(connect,module_name,offset) url = ''+"#{connect.url}"+"#{module_name}"+"?offset="+"#{offset}"+'' 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, fields = ["no"], offset = 0) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/sugarcrm_rest.rb', line 149 def self.fetch_data_with_filters(connect,module_name,filter_json,fields=["no"],offset=0) filter=build_filter(filter_json) if (fields[0]=="no") url =''+"#{connect.url}"+"#{module_name}"+"#{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 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
79 80 81 82 83 84 85 86 |
# File 'lib/sugarcrm_rest.rb', line 79 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 |
Instance Method Details
#upload_document(connect, id, filename) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/sugarcrm_rest.rb', line 169 def upload_document(connect,id,filename) @token=Get_Token_Process_Url.authenticate puts @token path = File.join(__dir__,filename) 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 |