samanage 1.9.33 → 2.0.03

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -2
  3. data/Gemfile.lock +24 -26
  4. data/changelog.md +13 -0
  5. data/lib/samanage.rb +2 -1
  6. data/lib/samanage/api.rb +140 -144
  7. data/lib/samanage/api/attachments.rb +21 -0
  8. data/lib/samanage/api/category.rb +24 -19
  9. data/lib/samanage/api/changes.rb +59 -0
  10. data/lib/samanage/api/comments.rb +13 -13
  11. data/lib/samanage/api/contracts.rb +51 -47
  12. data/lib/samanage/api/custom_fields.rb +23 -19
  13. data/lib/samanage/api/custom_forms.rb +42 -38
  14. data/lib/samanage/api/departments.rb +25 -22
  15. data/lib/samanage/api/groups.rb +43 -39
  16. data/lib/samanage/api/hardwares.rb +57 -53
  17. data/lib/samanage/api/incidents.rb +60 -51
  18. data/lib/samanage/api/mobiles.rb +50 -46
  19. data/lib/samanage/api/other_assets.rb +47 -43
  20. data/lib/samanage/api/requester.rb +7 -7
  21. data/lib/samanage/api/sites.rb +27 -23
  22. data/lib/samanage/api/users.rb +58 -54
  23. data/lib/samanage/api/utils.rb +0 -19
  24. data/lib/samanage/error.rb +20 -20
  25. data/lib/samanage/url_builder.rb +53 -53
  26. data/lib/samanage/version.rb +2 -2
  27. data/samanage.gemspec +2 -2
  28. data/sample_file.txt +1 -0
  29. data/spec/api/samanage_attachment_spec.rb +14 -0
  30. data/spec/api/samanage_category_spec.rb +24 -24
  31. data/spec/api/samanage_change_spec.rb +98 -0
  32. data/spec/api/samanage_comments_spec.rb +25 -32
  33. data/spec/api/samanage_contract_spec.rb +62 -68
  34. data/spec/api/samanage_custom_field_spec.rb +12 -12
  35. data/spec/api/samanage_custom_form_spec.rb +24 -24
  36. data/spec/api/samanage_department_spec.rb +35 -36
  37. data/spec/api/samanage_group_spec.rb +59 -59
  38. data/spec/api/samanage_hardware_spec.rb +80 -85
  39. data/spec/api/samanage_incident_spec.rb +99 -103
  40. data/spec/api/samanage_mobile_spec.rb +70 -74
  41. data/spec/api/samanage_other_asset_spec.rb +72 -76
  42. data/spec/api/samanage_site_spec.rb +45 -45
  43. data/spec/api/samanage_user_spec.rb +117 -100
  44. data/spec/api/samanage_util_spec.rb +4 -9
  45. data/spec/samanage_api_spec.rb +48 -48
  46. data/spec/samanage_category_spec.rb +21 -28
  47. data/spec/samanage_url_builder_spec.rb +15 -15
  48. metadata +15 -11
  49. data/lib/data/gd-class2-root.crt +0 -24
@@ -1,9 +1,9 @@
1
1
  module Samanage
2
- class Api
3
- # Get requester from value (email)
4
- def get_requester_id(value: )
5
- api_call = self.execute(path: "requesters.json?name=#{value}")
6
- api_call[:data].size == 1 ? api_call[:data][0] : nil
7
- end
8
- end
2
+ class Api
3
+ # Get requester from value (email)
4
+ def get_requester_id(value: )
5
+ api_call = self.execute(path: "requesters.json?name=#{value}")
6
+ api_call[:data].size == 1 ? api_call[:data][0] : nil
7
+ end
8
+ end
9
9
  end
@@ -1,29 +1,33 @@
1
1
  module Samanage
2
- class Api
3
- def get_sites(path: PATHS[:site], options: {})
4
- url = Samanage::UrlBuilder.new(path: path, options: options).url
5
- self.execute(path: url)
6
- end
2
+ class Api
3
+ def get_sites(path: PATHS[:site], options: {})
4
+ url = Samanage::UrlBuilder.new(path: path, options: options).url
5
+ self.execute(path: url)
6
+ end
7
7
 
8
- def collect_sites(options: {})
9
- page = 1
10
- sites = Array.new
11
- total_pages = self.get_sites[:total_pages]
12
- 1.upto(total_pages) do |page|
13
- puts "Collecting Sites page: #{page}/#{total_pages}" if options[:verbose]
14
- sites += self.execute(http_method: 'get', path: "sites.json?page=#{page}")[:data]
15
- end
16
- sites
17
- end
8
+ def collect_sites(options: {})
9
+ sites = Array.new
10
+ total_pages = self.get_sites[:total_pages]
11
+ 1.upto(total_pages) do |page|
12
+ puts "Collecting Sites page: #{page}/#{total_pages}" if options[:verbose]
13
+ self.execute(http_method: 'get', path: "sites.json?page=#{page}")[:data].each do |site|
14
+ if block_given?
15
+ yield site
16
+ end
17
+ sites << site
18
+ end
19
+ end
20
+ sites
21
+ end
18
22
 
19
- def create_site(payload: , options: {})
20
- self.execute(path: PATHS[:site], http_method: 'post', payload: payload)
21
- end
23
+ def create_site(payload: , options: {})
24
+ self.execute(path: PATHS[:site], http_method: 'post', payload: payload)
25
+ end
22
26
 
23
- def delete_site(id: )
24
- self.execute(path: "sites/#{id}.json", http_method: 'delete')
25
- end
27
+ def delete_site(id: )
28
+ self.execute(path: "sites/#{id}.json", http_method: 'delete')
29
+ end
26
30
 
27
- alias_method :sites, :collect_sites
28
- end
31
+ alias_method :sites, :collect_sites
32
+ end
29
33
  end
@@ -1,73 +1,77 @@
1
1
  module Samanage
2
- class Api
2
+ class Api
3
3
 
4
- # Get users, using URL builder
5
- def get_users(path: PATHS[:user], options: {})
4
+ # Get users, using URL builder
5
+ def get_users(path: PATHS[:user], options: {})
6
6
  url = Samanage::UrlBuilder.new(path: path, options: options).url
7
7
  self.execute(path: url)
8
- end
8
+ end
9
9
 
10
10
  # Returns all users in the account
11
- def collect_users(options: {})
12
- page = 1
13
- users = Array.new
14
- total_pages = self.get_users[:total_pages]
15
- 1.upto(total_pages) do |page|
16
- puts "Collecting Users page: #{page}/#{total_pages}" if options[:verbose]
17
- users += self.execute(http_method: 'get', path: "users.json?page=#{page}")[:data]
18
- end
19
- users
20
- end
11
+ def collect_users(options: {})
12
+ users = Array.new
13
+ total_pages = self.get_users[:total_pages]
14
+ 1.upto(total_pages) do |page|
15
+ puts "Collecting Users page: #{page}/#{total_pages}" if options[:verbose]
16
+ self.execute(http_method: 'get', path: "users.json?page=#{page}")[:data].each do |user|
17
+ if block_given?
18
+ yield user
19
+ end
20
+ users << user
21
+ end
22
+ end
23
+ users
24
+ end
21
25
 
22
- # Create user given JSON
23
- def create_user(payload: , options: {})
24
- self.execute(path: PATHS[:user], http_method: 'post', payload: payload)
25
- end
26
+ # Create user given JSON
27
+ def create_user(payload: , options: {})
28
+ self.execute(path: PATHS[:user], http_method: 'post', payload: payload)
29
+ end
26
30
 
27
31
  # Return user by ID
28
- def find_user(id: )
29
- path = "users/#{id}.json"
30
- self.execute(path: path)
31
- end
32
+ def find_user(id: )
33
+ path = "users/#{id}.json"
34
+ self.execute(path: path)
35
+ end
32
36
 
33
- # Email is unique so compare first for exact match only. Returns nil or the id
34
- def find_user_id_by_email(email: )
35
- api_call = self.check_user(value: email)
36
- api_call[:data].select{|u| u['email'].to_s.downcase == email.to_s.downcase}.first.to_h['id']
37
- end
37
+ # Email is unique so compare first for exact match only. Returns nil or the id
38
+ def find_user_id_by_email(email: )
39
+ api_call = self.check_user(value: email)
40
+ api_call[:data].select{|u| u['email'].to_s.downcase == email.to_s.downcase}.first.to_h['id']
41
+ end
38
42
 
39
- # Returns nil if no matching group_id
40
- def find_user_group_id_by_email(email: )
41
- user = self.check_user(value: email)
42
- group_ids = user[:data].select{|u| u['email'].to_s.downcase == email.to_s.downcase}.first.to_h['group_ids'].to_a
43
- group_ids.each do |group_id|
44
- group = self.find_group(id: group_id)
45
- if group[:data]['is_user'] && email == group[:data]['email']
46
- return group_id
47
- end
48
- end
49
- return nil
50
- end
43
+ # Returns nil if no matching group_id
44
+ def find_user_group_id_by_email(email: )
45
+ user = self.check_user(value: email)
46
+ group_ids = user[:data].select{|u| u['email'].to_s.downcase == email.to_s.downcase}.first.to_h['group_ids'].to_a
47
+ group_ids.each do |group_id|
48
+ group = self.find_group(id: group_id)
49
+ if group[:data]['is_user'] && email == group[:data]['email']
50
+ return group_id
51
+ end
52
+ end
53
+ return nil
54
+ end
51
55
 
52
56
  # Check for user by field (ex: users.json?field=value)
53
- def check_user(field: 'email', value: )
54
- if field.to_s.downcase == 'email'
55
- value = value.to_s.gsub("+",'%2B')
56
- end
57
- url = "users.json?#{field}=#{value}"
58
- self.execute(path: url)
59
- end
57
+ def check_user(field: 'email', value: )
58
+ if field.to_s.downcase == 'email'
59
+ value = value.to_s.gsub("+",'%2B')
60
+ end
61
+ url = "users.json?#{field}=#{value}"
62
+ self.execute(path: url)
63
+ end
60
64
 
61
65
  # Update user by id
62
- def update_user(payload: , id: )
63
- path = "users/#{id}.json"
64
- self.execute(path: path, http_method: 'put', payload: payload)
65
- end
66
+ def update_user(payload: , id: )
67
+ path = "users/#{id}.json"
68
+ self.execute(path: path, http_method: 'put', payload: payload)
69
+ end
66
70
 
67
- def delete_user(id: )
71
+ def delete_user(id: )
68
72
  self.execute(path: "users/#{id}.json", http_method: 'delete')
69
- end
73
+ end
70
74
 
71
- alias_method :users, :collect_users
72
- end
75
+ alias_method :users, :collect_users
76
+ end
73
77
  end
@@ -7,24 +7,5 @@ module Samanage
7
7
  raise Samanage::Error.new(error: 'Invalid Email', response: {}) unless user_id
8
8
  self.execute(http_method: 'put', path: "users/#{user_id}.json?send_activation_email=1&add_callbacks=1")
9
9
  end
10
-
11
- # Downloads a Samanage Attachment object can overwrite default filename and path (Object/ObjectID/Original_Filename)
12
- def download_attachment(attachment: {}, filename: nil, path: nil)
13
- attachable_type = attachment['attachable_type']
14
- attachable_id = attachment['attachable_id'].to_s
15
- filename = filename || attachment['filename']
16
- url = attachment['url']
17
-
18
- file_path = path ? path : File.join(Dir.pwd,attachable_type,attachable_id)
19
- unless File.directory?(file_path)
20
- FileUtils.mkpath(file_path)
21
- end
22
-
23
- exact_path = File.join(file_path,filename)
24
- downloaded_attachment = open(exact_path, "wb+") do |file|
25
- file << open(url).read
26
- end
27
- downloaded_attachment
28
- end
29
10
  end
30
11
  end
@@ -1,28 +1,28 @@
1
1
  module Samanage
2
- class Error < StandardError
3
- attr_accessor :status_code, :response, :error
4
-
5
- def initialize(error: nil, response: {})
6
- self.error = error
7
- if response.class == Hash
8
- self.status_code = response[:code]
9
- self.response = response[:data] ||= response[:response]
10
- else
11
- self.status_code = nil
12
- self.response = response
13
- end
2
+ class Error < StandardError
3
+ attr_accessor :status_code, :response, :error
4
+
5
+ def initialize(error: nil, response: {})
6
+ self.error = error
7
+ if response.class == Hash
8
+ self.status_code = response[:code]
9
+ self.response = response[:data] ||= response[:response]
10
+ else
11
+ self.status_code = nil
12
+ self.response = response
13
+ end
14
14
 
15
- puts "[Error] #{self.status_code}: #{self.response}"
16
- end
17
- end
15
+ puts "[Error] #{self.status_code}: #{self.response}"
16
+ end
17
+ end
18
18
 
19
- # API Errors
20
- class SamanageError < Error; end
19
+ # API Errors
20
+ class SamanageError < Error; end
21
21
 
22
- class AuthorizationError < SamanageError; end
22
+ class AuthorizationError < SamanageError; end
23
23
 
24
- class InvalidRequest < SamanageError; end
24
+ class InvalidRequest < SamanageError; end
25
25
 
26
- class NotFound < SamanageError; end
26
+ class NotFound < SamanageError; end
27
27
 
28
28
  end
@@ -1,60 +1,60 @@
1
1
  module Samanage
2
- class UrlBuilder
3
- attr_accessor :url
4
- @url = ''
2
+ class UrlBuilder
3
+ attr_accessor :url
4
+ @url = ''
5
5
 
6
- def initialize(path: nil,options: nil)
7
- self.url = map_path(path: path, options: options)
8
- return url
9
- end
6
+ def initialize(path: nil,options: nil)
7
+ self.url = map_path(path: path, options: options)
8
+ return url
9
+ end
10
10
 
11
11
 
12
- def map_path(path: nil, options: nil)
13
- url = String.new
14
- parameters = String.new
15
- case path
16
- when /user/
17
- url += 'users'
18
- when /hardware/
19
- url += 'hardwares'
20
- when /other_asset/
21
- url += 'other_assets'
22
- when /incident/
23
- url += 'incidents'
24
- when /change/
25
- url += 'changes'
26
- when /custom_field/
27
- url += 'custom_fields'
28
- when /custom_form/
29
- url += 'custom_forms'
30
- when /mobile/
31
- url += 'mobiles'
32
- when /site/
33
- url += 'sites'
34
- when /department/
35
- url += 'departments'
36
- when /contract/
37
- url += 'contracts'
38
- when /group/
39
- url += 'groups'
40
- end
12
+ def map_path(path: nil, options: nil)
13
+ url = String.new
14
+ parameters = String.new
15
+ case path
16
+ when /user/
17
+ url += 'users'
18
+ when /hardware/
19
+ url += 'hardwares'
20
+ when /other_asset/
21
+ url += 'other_assets'
22
+ when /incident/
23
+ url += 'incidents'
24
+ when /change/
25
+ url += 'changes'
26
+ when /custom_field/
27
+ url += 'custom_fields'
28
+ when /custom_form/
29
+ url += 'custom_forms'
30
+ when /mobile/
31
+ url += 'mobiles'
32
+ when /site/
33
+ url += 'sites'
34
+ when /department/
35
+ url += 'departments'
36
+ when /contract/
37
+ url += 'contracts'
38
+ when /group/
39
+ url += 'groups'
40
+ end
41
41
 
42
- if path.match(/(\d)+/)
43
- url += "/" + path.match(/(\d)+/)[0] + ".json"
44
- return url
45
- end
42
+ if path.match(/(\d)+/)
43
+ url += "/" + path.match(/(\d)+/)[0] + ".json"
44
+ return url
45
+ end
46
46
 
47
- options.each_pair do |field, value|
48
- if field.to_s == 'id' && value.to_s.match(/(\d)+/)
49
- url += "/#{value}.json"
50
- # Return. Filters not valid on an id
51
- return url
52
- end
53
- sub_param = "?#{field}=#{value}"
54
- parameters += sub_param + '&'
55
- end
56
- url += ".json" + parameters
57
- return url
58
- end
59
- end
47
+ options.each_pair do |field, value|
48
+ if field.to_s == 'id' && value.to_s.match(/(\d)+/)
49
+ url += "/#{value}.json"
50
+ # Return. Filters not valid on an id
51
+ return url
52
+ end
53
+ sub_param = "?#{field}=#{value}"
54
+ parameters += sub_param + '&'
55
+ end
56
+ url += ".json" + parameters
57
+ return url
58
+ end
59
+ end
60
60
  end
@@ -1,3 +1,3 @@
1
1
  module Samanage
2
- VERSION = '1.9.33'
3
- end
2
+ VERSION = '2.0.03'
3
+ end
data/samanage.gemspec CHANGED
@@ -14,6 +14,6 @@ Gem::Specification.new do |s|
14
14
  s.license = 'MIT'
15
15
  s.require_paths = ["lib"]
16
16
  s.required_ruby_version = '>= 2.3'
17
- s.add_development_dependency 'httparty', ['~> 0.15']
18
- s.add_runtime_dependency 'httparty', ['~> 0.15']
17
+ s.add_development_dependency 'httparty', ['0.15.7']
18
+ s.add_runtime_dependency 'httparty', ['0.15.7']
19
19
  end
data/sample_file.txt ADDED
@@ -0,0 +1 @@
1
+ blah blah blah
@@ -0,0 +1,14 @@
1
+
2
+ require 'samanage'
3
+ describe Samanage::Api do
4
+ context 'Attachments' do
5
+ describe 'API Functions' do
6
+ before(:all) do
7
+ TOKEN ||= ENV['SAMANAGE_TEST_API_TOKEN']
8
+ @samanage = Samanage::Api.new(token: TOKEN)
9
+ @users = @samanage.get_users[:data]
10
+ @incidents = @samanage.incidents
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,29 +1,29 @@
1
1
  require 'samanage'
2
2
  describe Samanage::Api do
3
- context 'category' do
4
- before(:all) do
5
- TOKEN ||= ENV['SAMANAGE_TEST_API_TOKEN']
6
- @samanage = Samanage::Api.new(token: TOKEN)
7
- @categories = @samanage.categories
8
- end
9
- it 'collects all categories' do
10
- expect(@categories).to be_an(Array)
11
- end
3
+ context 'category' do
4
+ before(:all) do
5
+ TOKEN ||= ENV['SAMANAGE_TEST_API_TOKEN']
6
+ @samanage = Samanage::Api.new(token: TOKEN)
7
+ @categories = @samanage.categories
8
+ end
9
+ it 'collects all categories' do
10
+ expect(@categories).to be_an(Array)
11
+ end
12
12
 
13
- it 'creates a category' do
14
- category_name = "Category Name ##{(rand*10**4).ceil}"
15
- category_description = "Descrption #{(rand*10**4).ceil}"
16
- payload = {
17
- category: {
18
- name: category_name,
19
- description: category_description
20
- }
21
- }
22
- category_create = @samanage.create_category(payload: payload)
13
+ it 'creates a category' do
14
+ category_name = "Category Name ##{(rand*10**4).ceil}"
15
+ category_description = "Descrption #{(rand*10**4).ceil}"
16
+ payload = {
17
+ category: {
18
+ name: category_name,
19
+ description: category_description
20
+ }
21
+ }
22
+ category_create = @samanage.create_category(payload: payload)
23
23
 
24
- expect(category_create[:data]['id']).to be_an(Integer)
25
- expect(category_create[:data]['name']).to eq(category_name)
26
- expect(category_create[:code]).to eq(200).or(201)
27
- end
28
- end
24
+ expect(category_create[:data]['id']).to be_an(Integer)
25
+ expect(category_create[:data]['name']).to eq(category_name)
26
+ expect(category_create[:code]).to eq(200).or(201)
27
+ end
28
+ end
29
29
  end