sugarcrm_rest 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1d9ac199257b3eab145c9a2d82bb98e8795ed2a814c625c9f7d383617ed67864
4
+ data.tar.gz: 5bc4f5a0649e27256b7ccba89c3c590437cb1117e6b4d8bcba8fae1fafb744e8
5
+ SHA512:
6
+ metadata.gz: d8f46251b0a852c12a0490660b3bfaeeba382967cbe13ca4c88917c5652979d2a5bfb32d4f6a5ba88f0ca674d0c2cafe30bc8758cddc8422a14862fadf1ea5d4
7
+ data.tar.gz: 7c4b971aef3185a82d17f3f6067013e8e56cca32c4f595d3ea931eeb0c7d9906994e527169c3062e4ce7b6dcc78f466f95f27ed4757dadd9bace4aa717dd7a36
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.6
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in sugarcrm_rest.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 sruthipn
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,50 @@
1
+ # SugarcrmRest
2
+
3
+
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sugarcrm_rest'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sugarcrm_rest
20
+
21
+ ## Usage
22
+ ```r
23
+ connect=SugarcrmRest::Connect.new(consumer_key,consumer_secret,username,password,platform,url) #Provide the connection object
24
+ SugarcrmRest::Fetch_Data.fetch_single_record(connect,module_name,id) #Give the record with specified id from specified module
25
+ SugarcrmRest::Fetch_Data.fetch_all_data(connect,module_name,offset) #Fetch data from specified module according to offset
26
+ SugarcrmRest::Fetch_Data.fetch_data_with_filters(connect,module_name,filter_json,fields=["no"],offset=0) #Return a json object of data having specified fields by applying specified filters
27
+ SugarcrmRest::Fetch_Data.upload_document(connect,id,filename) #upload the file having specified filename and id to doccument module
28
+
29
+ SugarcrmRest::Update_Sugar.create_new_record(connect,module_name,params) #Creating new record in specified module
30
+ SugarcrmRest::Update_Sugar.update_data(connect,module_name,params,id) #update fields of specified id and module
31
+ SugarcrmRest::Update_Sugar.delete_record(connect,module_name,id) #Delete specified record
32
+
33
+
34
+
35
+
36
+
37
+ ```
38
+ ## Development
39
+
40
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+
42
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sugarcrm_rest.
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sugarcrm_rest"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,253 @@
1
+ require "sugarcrm_rest/version"
2
+
3
+ module SugarcrmRest
4
+
5
+ class Connect
6
+ attr_reader :consumer_key,:consumer_secret,:username,:password,:platform,:url
7
+ def initialize(consumer_key,consumer_secret,username,password,platform,url)
8
+ @consumer_key=consumer_key
9
+ @consumer_secret=consumer_secret
10
+ @username=username
11
+ @password=password
12
+ @platform=platform
13
+ @url=url
14
+ end
15
+ end
16
+ #class for authenticattion
17
+ class Get_Token_Process_Url
18
+ $count=0
19
+
20
+ def self.authenticate(connect)
21
+ begin
22
+ url =''+"#{connect.url}"+'oauth2/token'
23
+ oauth2_token_arguments = {"grant_type" =>"password", "client_id" =>connect.consumer_key, "client_secret" =>connect.consumer_secret, "username" =>connect.username, "password" =>connect.password, "platform" =>connect.platform}
24
+ uri = URI.parse url
25
+ http = Net::HTTP.new uri.host, uri.port
26
+ http.use_ssl = true
27
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
28
+ request = Net::HTTP::Post.new uri.request_uri
29
+ request["Content-Type"] = "application/x-www-form-urlencoded"
30
+ request.body = URI.encode_www_form(oauth2_token_arguments)
31
+ response = http.request request
32
+ token = nil
33
+ if response.kind_of? Net::HTTPSuccess
34
+ token = JSON.parse response.body
35
+ end
36
+ p token['access_token']
37
+ return token['access_token']
38
+ end
39
+ rescue Exception=>e
40
+ p $count
41
+ retry if ($count += 1) < 3
42
+
43
+ end
44
+
45
+
46
+
47
+ def self.execute_uri(connect,url)
48
+ begin
49
+ uri = URI.parse url
50
+ http = Net::HTTP.new uri.host, uri.port
51
+ http.use_ssl = true
52
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
53
+ request = Net::HTTP::Get.new uri.request_uri
54
+ request["Content-Type"] = "application/json"
55
+ request["OAuth-Token"] = authenticate(connect)
56
+ response = http.request request
57
+ end
58
+ rescue Exception=>e
59
+
60
+ p $count
61
+ retry if ($count += 1) < 3
62
+ end
63
+
64
+ end
65
+
66
+
67
+
68
+
69
+
70
+ class Fetch_Data
71
+
72
+ #Fetch a single record by the given id from given module
73
+ def self.fetch_single_record(connect,module_name,id)
74
+ url = ''+"#{connect.url}"+"#{module_name}/#{id}"
75
+ response = Get_Token_Process_Url.execute_uri(connect,url)
76
+ if response.kind_of? Net::HTTPSuccess
77
+ root = JSON.parse response.body
78
+ return root
79
+ end
80
+ end
81
+ #fetch all data from agiven module
82
+ def self.fetch_all_data(connect,module_name,offset)
83
+ url = ''+"#{connect.url}"+"#{module_name}"+"?offset="+"#{offset}"+''
84
+ response = Get_Token_Process_Url.execute_uri(connect,url)
85
+ if response.kind_of? Net::HTTPSuccess
86
+ root = JSON.parse response.body
87
+ p root
88
+ return root
89
+ end
90
+ end
91
+
92
+ def self.build_filter(filter_json)
93
+ filter_hash = JSON.parse(filter_json)
94
+ filter_keys=JSON.parse(filter_json).keys
95
+ filter="?"
96
+ and_op=""
97
+ count=0
98
+ filter_keys.each do |k|
99
+ f1=filter_hash[k]
100
+ value=filter_hash[k].keys
101
+ filter=filter+"filter[0]["+"#{value[0]}"+"]["+"#{k}"+"]="+"#{f1[value[0]]}"
102
+ filter=filter+"&" if (count += 1) < filter_keys.length
103
+
104
+ end
105
+ return filter
106
+
107
+ end
108
+ def self.adding_fields(connect,module_name,filter,fields)
109
+ offset=0
110
+ count=0
111
+ if offset==0
112
+ @array_all_claimants = Array.new
113
+ end
114
+ filter=filter+"&fields="
115
+ fields.each do |f|
116
+ filter=filter+f
117
+
118
+ filter=filter+"," if (count += 1) < fields.length
119
+ end
120
+
121
+ result=Hash.new
122
+ i=0
123
+ while offset >= 0 do
124
+ url =''+"#{connect.url}"+"#{module_name}"+"#{filter}"+'&max_num=100&offset='+offset.to_s
125
+ p url
126
+ response = Get_Token_Process_Url.execute_uri(connect,url)
127
+ if response.kind_of? Net::HTTPSuccess
128
+ root = JSON.parse response.body
129
+ offset=root['next_offset']
130
+
131
+ root['records'].each do |r|
132
+ result[i]={fields[0]=>r[fields[0]]}
133
+ for f in 1..fields.length-1
134
+ result[i]=result[i].merge(fields[f]=>r[fields[f]])
135
+ end
136
+ i=i+1
137
+ end
138
+ end
139
+ end
140
+ return result.to_json
141
+ end
142
+
143
+ def self.fetch_data_with_filters(connect,module_name,filter_json,fields=["no"],offset=0)
144
+ filter=build_filter(filter_json)
145
+
146
+
147
+ if (fields[0]=="no")
148
+
149
+ url =''+"#{connect.url}"+"#{module_name}"+"#{filter}"+'&max_num=100&offset='+offset.to_s
150
+
151
+ response = Get_Token_Process_Url.execute_uri(connect,url)
152
+ if response.kind_of? Net::HTTPSuccess
153
+ root = JSON.parse response.body
154
+ return root
155
+ end
156
+ else
157
+ return adding_fields(connect,module_name,filter,fields)
158
+ end
159
+
160
+ end
161
+
162
+
163
+ def upload_document(connect,id,filename)
164
+
165
+ @token=Get_Token_Process_Url.authenticate
166
+ puts @token
167
+ path = File.join(__dir__,filename)
168
+ f=File.new(path,'rb')
169
+ response = RestClient::Request.execute(
170
+ :url => "#{connect.url}/Documents/#{id}/file/filename",
171
+ :method => :post,
172
+ :headers => {:'OAuth-Token' => @token},
173
+ :payload => {:filename => f},
174
+ :verify_ssl => false
175
+ )
176
+
177
+ if(response.code==200)
178
+ root = JSON.parse response.body
179
+ record=root['record']
180
+ end
181
+ return record['id']
182
+
183
+
184
+ end
185
+
186
+
187
+
188
+ end
189
+
190
+
191
+
192
+ class Update_Sugar
193
+
194
+ def self.create_new_record(connect,module_name,params)
195
+
196
+ url=url =''+"#{connect.url}"+module_name
197
+ uri = URI.parse url
198
+ http = Net::HTTP.new(uri.host, uri.port)
199
+ request = Net::HTTP::Post.new(uri)
200
+ request["OAuth-Token"] = Get_Token_Process_Url.authenticate(connect)
201
+ http.use_ssl = true
202
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
203
+ request.add_field('Content-Type','application/json')
204
+ request.body=params.to_json
205
+ response = http.request request
206
+ p response.to_s
207
+ if response.kind_of? Net::HTTPSuccess
208
+ root = JSON.parse response.body
209
+ end
210
+ return root
211
+ end
212
+
213
+
214
+ def self.update_data(connect,module_name,params,id)
215
+ url=''+"#{connect.url}"+module_name+'/'+id
216
+ p url
217
+ uri = URI.parse url
218
+ http = Net::HTTP.new(uri.host, uri.port)
219
+ request = Net::HTTP::Put.new(uri)
220
+ request["OAuth-Token"] = Get_Token_Process_Url.authenticate(connect)
221
+ http.use_ssl = true
222
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
223
+ request.add_field('Content-Type','application/json')
224
+ request.body=params.to_json
225
+
226
+ response = http.request request
227
+ p response.to_s
228
+ if response.kind_of? Net::HTTPSuccess
229
+ root = JSON.parse response.body
230
+ end
231
+ return root
232
+ end
233
+
234
+ def self.delete_record(connect,module_name,id)
235
+ url=''+"#{connect.url}"+module_name+'/'+id
236
+ p url
237
+ uri = URI.parse url
238
+ http = Net::HTTP.new(uri.host, uri.port)
239
+ request = Net::HTTP::Delete.new(uri)
240
+ request["OAuth-Token"] = Get_Token_Process_Url.authenticate(connect)
241
+ http.use_ssl = true
242
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
243
+ request.add_field('Content-Type','application/json')
244
+
245
+ response = http.request request
246
+ p response.to_s
247
+ if response.kind_of? Net::HTTPSuccess
248
+ root = JSON.parse response.body
249
+ end
250
+ return root
251
+ end
252
+ end
253
+ end
@@ -0,0 +1,3 @@
1
+ module SugarcrmRest
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "sugarcrm_rest/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sugarcrm_rest"
8
+ spec.version = SugarcrmRest::VERSION
9
+ spec.authors = ["Sruthi PN"]
10
+ spec.email = ["pn.sruthinarayanan@gmail.com"]
11
+
12
+ spec.summary = %q{A gem for communicating with sugarcrm }
13
+
14
+ spec.homepage = "https://bitbucket.org/sruthipn96/sugarcrm_rest"
15
+ spec.license = "MIT"
16
+
17
+
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.17"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+
32
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sugarcrm_rest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sruthi PN
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-03-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - pn.sruthinarayanan@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/sugarcrm_rest.rb
72
+ - lib/sugarcrm_rest/version.rb
73
+ - sugarcrm_rest.gemspec
74
+ homepage: https://bitbucket.org/sruthipn96/sugarcrm_rest
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubygems_version: 3.0.3
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: A gem for communicating with sugarcrm
97
+ test_files: []