sugarcrm_v10 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +117 -0
- data/Rakefile +10 -0
- data/lib/sugarcrm_v10.rb +402 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 06ca102c42b10633cd05923819b2051c4a268256
|
4
|
+
data.tar.gz: 84cd5503d66b49c8149a372bbf45cdfa1e34ec13
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 557c8186cd54318abae7a9846173d4b40b27c8c6e324b36c0c9f9be17b312351adb283ee3dd41a6a4b12d12fe7dfe074c7da7e083163743f842f8b2708badba5
|
7
|
+
data.tar.gz: e368aea76a879c89513508b0b3920e8e1fb83aaad50a666afacec6a8e7ec5141d60d3bd209cae08a78a666e22330e616a6d4ad5a1e5584cc1b95f744f0f52272
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Pournamithayyil
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
SugarCRM REST v10
|
2
|
+
Summary:
|
3
|
+
An effective tool to interact with SugarCRM through the REST v10 api
|
4
|
+
Features:
|
5
|
+
• Supports creating, updating, retrieving records from modules in SugarCRM
|
6
|
+
• Lists records filtered by an expression
|
7
|
+
• Access to related modules
|
8
|
+
• Supports uploading and downloading documents via document module
|
9
|
+
• Access API methods directly on the SugarCRM.connection object
|
10
|
+
|
11
|
+
Synopsis:
|
12
|
+
Establishing a connection:
|
13
|
+
SugarCRM::Connection.connect(<host>,<consumer_key>,<consumer_secret>,<username>,<password>,<platform>)
|
14
|
+
SugarCRM::Connection.connect(https://instance.sugarcrm.com,”testkey”,”sugar”,”admin”,”xxxx”,”sugarplatform”)
|
15
|
+
|
16
|
+
Create a new record to a module:
|
17
|
+
SugarCRM::Connection.create_record(<module>,<record>)
|
18
|
+
SugarCRM::Connection.create_record(“Contacts”,{
|
19
|
+
:first_name => “Shanker”,
|
20
|
+
:last_name => “Raj”,
|
21
|
+
})
|
22
|
+
|
23
|
+
Retrieve all records from a module:
|
24
|
+
SugarCRM::Connection.fetch_all(<module>,<max_num>,<offset>)
|
25
|
+
#retrieves first 20 records
|
26
|
+
SugarCRM::Connection.fetch_all(“Accounts”)
|
27
|
+
#retrieves 10 records with offset 5
|
28
|
+
SugarCRM::Connection.fetch_all(“Accounts”,10,5)
|
29
|
+
|
30
|
+
Retrieve records by fields:
|
31
|
+
SugarCRM::Connection.fetch_any_fields(<module>,<max_num>,<offset><fields>)
|
32
|
+
#retrieves name and email addresses of 30 records with offset 2
|
33
|
+
SugarCRM::Connection.fetch_any_fields(“Contacts”,30,2,[”name”,”email_address”])
|
34
|
+
|
35
|
+
Retrieve a single record from a module:
|
36
|
+
SugarCRM::Connection.fetch_single_record(<module>,<id>)
|
37
|
+
#retrieves record with id 3
|
38
|
+
SugarCRM::Connection.fetch_single_record(“Accounts”,3)
|
39
|
+
#retrieves only first_name of the record
|
40
|
+
SugarCRM::Connection.fetch_single_record(“Accounts”,3).first_name
|
41
|
+
|
42
|
+
Update a record of a module:
|
43
|
+
SugarCRM::Connection.update_record(<module>,<id>,<data>)
|
44
|
+
#updates billing address of a user
|
45
|
+
SugarCRM::Connection.update_record(“Users”,2,{
|
46
|
+
:billing_address => “abc”
|
47
|
+
})
|
48
|
+
|
49
|
+
Retrieve records by conditions:
|
50
|
+
SugarCRM::Connection.filter_records(<module>,<max_num>,<offset>,<fields>,<filter>)
|
51
|
+
|
52
|
+
#retrieves first 30 records with first_name ‘Ann’
|
53
|
+
SugarCRM::Connection.filter_records(“Contacts”, 30,0, {
|
54
|
+
:first_name =>"Ann"
|
55
|
+
})
|
56
|
+
#retrieves all ‘address’ and ‘phone’ whose first_name starts with‘A’
|
57
|
+
SugarCRM::Connection.filter_records(“Contacts”,nil,nil, [”phone”,”address”],{
|
58
|
+
:first_name =>{"$starts”=> ”Ann"
|
59
|
+
}})
|
60
|
+
|
61
|
+
Filter records with and/or sub-expression types :
|
62
|
+
SugarCRM::Connection.filter_by_and_or(<module>,<max_num>,<offset>,<fields>,<filter>)
|
63
|
+
#retrieves first 4 records whose last name is Hona and primary address state is CA
|
64
|
+
SugarCRM::Connection.filter_by_and_or("Accounts",4,0,[
|
65
|
+
“name”,”title”
|
66
|
+
],{
|
67
|
+
"$and"=>{"last_name"=>"Hona",
|
68
|
+
"primary_address_state"=>"CA"
|
69
|
+
}})
|
70
|
+
|
71
|
+
#Add a meeting to a contact :
|
72
|
+
SugarCRM::Connection.create_related(<module>,<id>,<data>,<link_name>)
|
73
|
+
SugarCRM::Connection.create_related("Contacts",3,{
|
74
|
+
"name"=>"Test Meeting4",
|
75
|
+
"description"=>"SugarMeeting4",
|
76
|
+
"duration_hours"=>2
|
77
|
+
},"meetings")
|
78
|
+
|
79
|
+
Retrieve related module fields :
|
80
|
+
SugarCRM::Connection.fetch_related(<module>,<id>,<fields>,<link_name>)
|
81
|
+
#retrieves all document id related to a contact
|
82
|
+
SugarCRM::Connection.fetch_related("Contacts",9,”id”,"documents")
|
83
|
+
|
84
|
+
Download a document via document module :
|
85
|
+
SugarCRM::Connection. download_document(<document_id>,<path_to_save>)
|
86
|
+
SugarCRM::Connection. download_document(“2”,’E:/’)
|
87
|
+
|
88
|
+
Upload a file :
|
89
|
+
SugarCRM::Connection. upload_document(<options>)
|
90
|
+
SugarCRM::Connection. upload_document ("name" => 'E:\ filename.pdf',
|
91
|
+
"status_id" => "Active",
|
92
|
+
"doc_type" => "Forms",
|
93
|
+
"category_id" => "Application",
|
94
|
+
"document_name" => "filename.pdf",
|
95
|
+
"template_type" => "Forms",
|
96
|
+
"revision" => 1,
|
97
|
+
"active_date" => Date.today,
|
98
|
+
"subcategory_id" => "Other",
|
99
|
+
)
|
100
|
+
Using gem in Console :
|
101
|
+
1. Type ‘irb’ in command prompt
|
102
|
+
2. Use ‘require sugarcrm’ to require the gem
|
103
|
+
3. Connect to SugarCRM instance by calling SugarCRM::Connection.connect providing the arguments(refer the documentation above)
|
104
|
+
4. Now you can access all the above API methods directly on the SugarCRM.connection object
|
105
|
+
Sugarcrm gem with Concurrent Sessions :
|
106
|
+
The SugarCRM gem works with more than one session concurrently. Call SugarCRM::Connection.connect on each and access the methods on its object as shown below:
|
107
|
+
Sugarcrm_conn1 = SugarCRM::Connection.connect(url1,..)
|
108
|
+
Sugarcrm_conn1.fetch_all(“Accounts”)
|
109
|
+
Sugarcrm_conn2 = SugarCRM::Connection.connect(url2,..)
|
110
|
+
Sugarcrm_conn1.fetch_all(“Accounts”)
|
111
|
+
|
112
|
+
Install :
|
113
|
+
$ gem install sugarcrm_v10
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
data/Rakefile
ADDED
data/lib/sugarcrm_v10.rb
ADDED
@@ -0,0 +1,402 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'uri'
|
3
|
+
require 'net/https'
|
4
|
+
require 'json'
|
5
|
+
require 'rest-client'
|
6
|
+
require 'ostruct'
|
7
|
+
require 'win32ole'
|
8
|
+
require 'open-uri'
|
9
|
+
require 'pdf-reader'
|
10
|
+
require 'logger'
|
11
|
+
require 'benchmark'
|
12
|
+
|
13
|
+
module SugarCRM;
|
14
|
+
|
15
|
+
class Connection
|
16
|
+
|
17
|
+
def self.connect(url,consumer_key,consumer_secret,username,password,platform)
|
18
|
+
begin
|
19
|
+
$consumer_key = consumer_key
|
20
|
+
$consumer_secret = consumer_secret
|
21
|
+
$username = username
|
22
|
+
$password = password
|
23
|
+
$platform = platform
|
24
|
+
|
25
|
+
$url=url
|
26
|
+
uri = URI.parse(url)
|
27
|
+
|
28
|
+
if uri.is_a?(URI::HTTP) && !uri.host.nil?
|
29
|
+
|
30
|
+
oauth2_token_arguments = {"grant_type" =>"password", "client_id" =>consumer_key, "client_secret" =>consumer_secret, "username" =>username, "password" =>password, "platform" =>platform}
|
31
|
+
|
32
|
+
http = Net::HTTP.new uri.host, uri.port
|
33
|
+
http.use_ssl = true
|
34
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
35
|
+
request = Net::HTTP::Post.new uri.request_uri
|
36
|
+
request["Content-Type"] = "application/x-www-form-urlencoded"
|
37
|
+
request.body = URI.encode_www_form(oauth2_token_arguments)
|
38
|
+
response = http.request request
|
39
|
+
$token = nil
|
40
|
+
|
41
|
+
if response.kind_of? Net::HTTPSuccess
|
42
|
+
$token = JSON.parse response.body
|
43
|
+
return SugarCRM::Connection
|
44
|
+
else
|
45
|
+
raise "Unable to connect to sugarcrm"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
rescue Exception=>e
|
49
|
+
puts e
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.get_refreshtoken() #generates new token
|
54
|
+
|
55
|
+
uri = URI.parse($url)
|
56
|
+
oauth2_token_arguments = {"grant_type" =>"refresh_token", "client_id" =>$consumer_key, "client_secret" =>$consumer_secret,"refresh_token"=>$token["refresh_token"]}
|
57
|
+
http = Net::HTTP.new uri.host, uri.port
|
58
|
+
http.use_ssl = true
|
59
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
60
|
+
request = Net::HTTP::Post.new uri.request_uri
|
61
|
+
request["Content-Type"] = "application/x-www-form-urlencoded"
|
62
|
+
request.body = URI.encode_www_form(oauth2_token_arguments)
|
63
|
+
response = http.request request
|
64
|
+
if response.kind_of? Net::HTTPSuccess
|
65
|
+
$token = JSON.parse response.body
|
66
|
+
return $token["access_token"]
|
67
|
+
else
|
68
|
+
return nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.execute_uri(url) #getrequest
|
73
|
+
|
74
|
+
uri = URI.parse url
|
75
|
+
http = Net::HTTP.new uri.host, uri.port
|
76
|
+
http.use_ssl = true
|
77
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
78
|
+
request = Net::HTTP::Get.new uri.request_uri
|
79
|
+
request["Content-Type"] = "application/json"
|
80
|
+
request["OAuth-Token"] = $token["access_token"]
|
81
|
+
response = http.request request
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.execute_postreq(url,record)
|
86
|
+
|
87
|
+
uri = URI.parse url
|
88
|
+
http = Net::HTTP.new uri.host, uri.port
|
89
|
+
http.use_ssl = true
|
90
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
91
|
+
request = Net::HTTP::Post.new (url)
|
92
|
+
request["OAuth-Token"] = $token["access_token"]
|
93
|
+
request.add_field('Content-Type', 'application/json')
|
94
|
+
request.body = record.to_json
|
95
|
+
response = http.request(request)
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.execute_putreq(url,record)
|
100
|
+
|
101
|
+
uri = URI.parse url
|
102
|
+
http = Net::HTTP.new uri.host, uri.port
|
103
|
+
http.use_ssl = true
|
104
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
105
|
+
request = Net::HTTP::Put.new (url)
|
106
|
+
request["OAuth-Token"] = $token["access_token"]
|
107
|
+
request.add_field('Content-Type', 'application/json')
|
108
|
+
request.body = record.to_json
|
109
|
+
response = http.request(request)
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
def self.fetch_single_record(module_name,id) # returns a single record with given id
|
115
|
+
begin
|
116
|
+
|
117
|
+
if get_refreshtoken()==nil
|
118
|
+
connect($url,$consumer_key,$consumer_secret,$username,$password,$platform)
|
119
|
+
end
|
120
|
+
|
121
|
+
id=id.to_s
|
122
|
+
uri = URI.parse($url)
|
123
|
+
url = "https://"+uri.host+"/rest/v10/"+module_name+"/#{id}"
|
124
|
+
response = execute_uri(url)
|
125
|
+
raise "Module or record id not found" unless response.is_a? Net::HTTPSuccess
|
126
|
+
resp = JSON.parse response.body
|
127
|
+
return res= OpenStruct.new(resp)
|
128
|
+
rescue =>e
|
129
|
+
puts e
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
def self.fetch_all(module_name,max_num=20,offset=0) # returns a set of records with all fields.
|
135
|
+
begin
|
136
|
+
if get_refreshtoken()==nil
|
137
|
+
connect($url,$consumer_key,$consumer_secret,$username,$password,$platform)
|
138
|
+
end
|
139
|
+
raise "Maximum number/offset should be a numeric value" unless max_num.is_a? Integer
|
140
|
+
raise "Maximum number/offset should be a numeric value" unless offset.is_a? Integer
|
141
|
+
uri=URI.parse($url)
|
142
|
+
url="https://"+uri.host+"/rest/v10/"+module_name+"?max_num=#{max_num}&offset=#{offset}"
|
143
|
+
response=execute_uri(url)
|
144
|
+
raise "Module '#{module_name}' not found" unless response.is_a? Net::HTTPSuccess
|
145
|
+
resp= JSON.parse response.body
|
146
|
+
records=resp["records"]
|
147
|
+
raise "No records found!" if records.empty?
|
148
|
+
return records
|
149
|
+
rescue Exception => e
|
150
|
+
puts e
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def self.fetch_any_fields(module_name,max_num=20,offset=0,fields) #returns a set of records , passing required fields and module name
|
155
|
+
|
156
|
+
begin
|
157
|
+
if get_refreshtoken()==nil
|
158
|
+
connect($url,$consumer_key,$consumer_secret,$username,$password,$platform)
|
159
|
+
end
|
160
|
+
uri = URI.parse($url)
|
161
|
+
raise "Maximum number/offset should be a numeric value" unless max_num.is_a? Integer
|
162
|
+
raise "Maximum number/offset should be a numeric value" unless offset.is_a? Integer
|
163
|
+
|
164
|
+
if fields.kind_of?(Array)
|
165
|
+
@field=fields[0]
|
166
|
+
i=1
|
167
|
+
while i<fields.length
|
168
|
+
@field=@field+","+fields[i]
|
169
|
+
i+=1
|
170
|
+
end
|
171
|
+
else
|
172
|
+
@field=fields
|
173
|
+
end
|
174
|
+
url = "https://"+uri.host+"/rest/v10/"+module_name+"?max_num=#{max_num}&"+"fields=#{@field}&"+"offset=#{offset}"
|
175
|
+
response = execute_uri(url)
|
176
|
+
raise "Module '#{module_name}' not found" unless response.is_a? Net::HTTPSuccess
|
177
|
+
|
178
|
+
resp = JSON.parse response.body
|
179
|
+
records=resp["records"]
|
180
|
+
raise "No records found!" if records.empty?
|
181
|
+
return records
|
182
|
+
rescue Exception=>e
|
183
|
+
puts e
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
|
188
|
+
def self.create_record(module_name,record) #creates a new record
|
189
|
+
begin
|
190
|
+
if get_refreshtoken()==nil
|
191
|
+
connect($url,$consumer_key,$consumer_secret,$username,$password,$platform)
|
192
|
+
end
|
193
|
+
uri = URI.parse($url)
|
194
|
+
url = "https://"+uri.host+"/rest/v10/"+module_name
|
195
|
+
response = execute_postreq(url,record)
|
196
|
+
raise "Module '#{module_name}' not found" unless response.is_a? Net::HTTPSuccess
|
197
|
+
rec= JSON.parse response.body
|
198
|
+
id=rec["id"]
|
199
|
+
return id
|
200
|
+
rescue Exception =>e
|
201
|
+
puts e
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
def self.update_record(module_name,id,record) #updates the given record
|
206
|
+
begin
|
207
|
+
if get_refreshtoken()==nil
|
208
|
+
connect($url,$consumer_key,$consumer_secret,$username,$password,$platform)
|
209
|
+
end
|
210
|
+
updated = Hash.new
|
211
|
+
id = id.to_s
|
212
|
+
uri = URI.parse($url)
|
213
|
+
url = "https://"+uri.host+"/rest/v10/"+module_name+"/#{id}"
|
214
|
+
response = execute_putreq(url,record)
|
215
|
+
raise "Module or record id not found" unless response.is_a? Net::HTTPSuccess
|
216
|
+
rec= JSON.parse response.body
|
217
|
+
return rec
|
218
|
+
rescue Exception=>e
|
219
|
+
puts e
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
|
224
|
+
def self.filter_by_and_or(module_name,max_num=20,offset=2,fields=nil,filter) # executes and/or nested conditions
|
225
|
+
begin
|
226
|
+
if get_refreshtoken()==nil
|
227
|
+
connect($url,$consumer_key,$consumer_secret,$username,$password,$platform)
|
228
|
+
end
|
229
|
+
|
230
|
+
@filter1=""
|
231
|
+
@filter2=""
|
232
|
+
filter.each do |key,value|
|
233
|
+
value.each do |k,v|
|
234
|
+
if v.class==Hash
|
235
|
+
v.each do |x,y|
|
236
|
+
@filter1 = "&filter"+"#{[0]}"+"[#{key}]"+"#{[0]}"+"[#{k}]"+"[#{x}]"+"="+"#{y}"+@filter1
|
237
|
+
end
|
238
|
+
else
|
239
|
+
@filter2 = "&filter"+"#{[0]}"+"[#{key}]"+"#{[0]}"+"[#{k}]"+"="+"#{v}"+@filter2
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
@filter= @filter1+@filter2
|
244
|
+
if fields!=nil
|
245
|
+
if fields.kind_of?(Array)
|
246
|
+
@field=fields.join(",")
|
247
|
+
else
|
248
|
+
@field=fields
|
249
|
+
end
|
250
|
+
end
|
251
|
+
uri = URI.parse($url)
|
252
|
+
url = "https://"+uri.host+"/rest/v10/"+module_name+"?max_num=#{max_num}"+"&offset=#{offset}"+"#{@filter}"+"&fields=#{@field}"
|
253
|
+
response = execute_uri(url)
|
254
|
+
raise "Module not found" unless response.is_a? Net::HTTPSuccess
|
255
|
+
resp= JSON.parse response.body
|
256
|
+
records= resp["records"]
|
257
|
+
raise "No records found!" if records.empty?
|
258
|
+
return records
|
259
|
+
rescue Exception =>e
|
260
|
+
puts e
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
def self.filter_records(module_name,max_num=20,offset=0,fields=nil,filter) #filter records with conditions { $starts,$contains ,$lt etc..}
|
265
|
+
|
266
|
+
|
267
|
+
begin
|
268
|
+
if get_refreshtoken()==nil
|
269
|
+
connect($url,$consumer_key,$consumer_secret,$username,$password,$platform)
|
270
|
+
end
|
271
|
+
raise " Add filter conditions" unless filter.class==Hash
|
272
|
+
|
273
|
+
@filter2=""
|
274
|
+
@filter1=""
|
275
|
+
filter.each do |k,v|
|
276
|
+
if v.class == Hash
|
277
|
+
v.each do |key,value|
|
278
|
+
@filter1 = "&filter"+"#{[0]}"+"[#{k}]"+"[#{key}]"+"="+"#{value}" + @filter1
|
279
|
+
end
|
280
|
+
else
|
281
|
+
@filter2 = "&filter"+"#{[0]}"+"[#{k}]"+"="+"#{v}" + @filter2
|
282
|
+
end
|
283
|
+
end
|
284
|
+
@filter = @filter1 + @filter2
|
285
|
+
|
286
|
+
if fields!=nil
|
287
|
+
if fields.kind_of?(Array)
|
288
|
+
@field=fields.join(",")
|
289
|
+
|
290
|
+
else
|
291
|
+
@field=fields
|
292
|
+
end
|
293
|
+
end
|
294
|
+
uri = URI.parse($url)
|
295
|
+
url = "https://"+uri.host+"/rest/v10/"+module_name+"?max_num=#{max_num}"+"#{@filter}"+"&offset=#{offset}"+"&fields=#{@field}"
|
296
|
+
response = execute_uri(url)
|
297
|
+
raise "Module not found" unless response.is_a? Net::HTTPSuccess
|
298
|
+
resp= JSON.parse response.body
|
299
|
+
records= resp["records"]
|
300
|
+
|
301
|
+
raise "No records found!" if records.empty?
|
302
|
+
return records
|
303
|
+
rescue Exception =>e
|
304
|
+
puts e
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
def self.fetch_related(module_name,id,fields=nil,link_name) # fetches related module fields
|
309
|
+
begin
|
310
|
+
if get_refreshtoken()==nil
|
311
|
+
connect($url,$consumer_key,$consumer_secret,$username,$password,$platform)
|
312
|
+
end
|
313
|
+
uri = URI.parse($url)
|
314
|
+
if fields.kind_of?(Array)
|
315
|
+
@field=fields.join(",")
|
316
|
+
|
317
|
+
else
|
318
|
+
@field=fields
|
319
|
+
end
|
320
|
+
url = "https://"+uri.host+"/rest/v10/#{module_name}/#{id}/link/#{link_name}"+"&fields=#{@field}"
|
321
|
+
response = execute_uri(url)
|
322
|
+
raise "Module or record id not found" unless response.is_a? Net::HTTPSuccess
|
323
|
+
resp = JSON.parse response.body
|
324
|
+
records = resp["records"]
|
325
|
+
raise "No records found!" if records.empty?
|
326
|
+
return records
|
327
|
+
rescue Exception =>e
|
328
|
+
puts e
|
329
|
+
end
|
330
|
+
end
|
331
|
+
|
332
|
+
def self.create_related(module_name,id,record,link_name) # Creates a single record and relate it to this module
|
333
|
+
begin
|
334
|
+
if get_refreshtoken()==nil
|
335
|
+
connect($url,$consumer_key,$consumer_secret,$username,$password,$platform)
|
336
|
+
end
|
337
|
+
uri = URI.parse($url)
|
338
|
+
url = "https://"+uri.host+"/rest/v10/#{module_name}/#{id}/link/#{link_name}"
|
339
|
+
response = execute_postreq(url,record)
|
340
|
+
raise "Module or record id not found" unless response.is_a? Net::HTTPSuccess
|
341
|
+
resp = JSON.parse response.body
|
342
|
+
id = resp["id"]
|
343
|
+
return id
|
344
|
+
rescue =>e
|
345
|
+
puts e
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
|
350
|
+
def self.download_document(doc_id,path) #downloads a document via document module
|
351
|
+
begin
|
352
|
+
if get_refreshtoken()==nil
|
353
|
+
connect($url,$consumer_key,$consumer_secret,$username,$password,$platform)
|
354
|
+
end
|
355
|
+
uri= URI.parse($url)
|
356
|
+
rec = fetch_single_record("Documents",doc_id)
|
357
|
+
doc_rev_id = rec['document_revision_id']
|
358
|
+
file_name = rec['document_name']
|
359
|
+
url = "https://"+uri.host+"/rest/v10/DocumentRevisions/#{doc_rev_id}/file/filename"
|
360
|
+
response = execute_uri(url)
|
361
|
+
raise "Document id not found" unless response.is_a? Net::HTTPSuccess
|
362
|
+
file_contents = response.body
|
363
|
+
file_contents.force_encoding(Encoding::ISO_8859_1).encode(Encoding::UTF_8)
|
364
|
+
file = path + file_name
|
365
|
+
IO.binwrite file,file_contents
|
366
|
+
rescue =>e
|
367
|
+
puts e
|
368
|
+
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
372
|
+
|
373
|
+
def self.upload_document(hash_options) #uploads a file to Documents module
|
374
|
+
begin
|
375
|
+
if get_refreshtoken()==nil
|
376
|
+
connect($url,$consumer_key,$consumer_secret,$username,$password,$platform)
|
377
|
+
end
|
378
|
+
uri = URI.parse($url)
|
379
|
+
|
380
|
+
revision =revision.to_i
|
381
|
+
request_opt = JSON.generate(hash_options)
|
382
|
+
res = RestClient.post 'https://'+uri.host+'/rest/v10/Documents', request_opt, :content_type => 'application/json', :'OAuth-Token' => $token['access_token'], :'Cache-Control' => 'no-cache', :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_NONE
|
383
|
+
root = JSON.parse res.body
|
384
|
+
@id = root['id']
|
385
|
+
document_id = @id
|
386
|
+
path=hash_options["name"]
|
387
|
+
response = RestClient.post "https://"+ uri.host+"/rest/v10/Documents/#{document_id}/file/filename", :filename => File.new(path, 'rb'),
|
388
|
+
:format => 'sugar-html-json',
|
389
|
+
:'OAuth-Token' => $token['access_token'],
|
390
|
+
:'Cache-Control' => 'no-cache',
|
391
|
+
:use_ssl => true,
|
392
|
+
:verify_mode => OpenSSL::SSL::VERIFY_NONE
|
393
|
+
|
394
|
+
return document_id
|
395
|
+
rescue Exception =>e
|
396
|
+
puts "Couldn't upload the file"
|
397
|
+
end
|
398
|
+
end
|
399
|
+
|
400
|
+
end
|
401
|
+
end
|
402
|
+
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sugarcrm_v10
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pournamithayyil
|
8
|
+
- Ashique Shereef
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2019-03-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.17'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.17'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: minitest
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '5.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '5.0'
|
56
|
+
description:
|
57
|
+
email:
|
58
|
+
- pournami1707@gmail.com
|
59
|
+
- ashiqshereef96@gmail.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/sugarcrm_v10.rb
|
69
|
+
homepage:
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata: {}
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 2.5.2
|
90
|
+
signing_key:
|
91
|
+
specification_version: 4
|
92
|
+
summary: Interacts with Sugarcrm via REST v10 api
|
93
|
+
test_files: []
|