samanage 1.6.9 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/samanage.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  $LOAD_PATH << File.dirname(__FILE__)
2
- require 'http'
2
+ require 'httparty'
3
3
 
4
4
  require 'samanage/api'
5
5
  require 'samanage/api/users'
data/lib/samanage/api.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module Samanage
2
2
  class Api
3
+ include HTTParty
3
4
  PATHS = {
4
5
  hardware: 'hardwares.json',
5
6
  user: 'users.json',
@@ -16,10 +17,10 @@ module Samanage
16
17
  def initialize(token: nil, datacenter: nil, development_mode: false)
17
18
  self.token = token if token
18
19
  if !datacenter.nil? && datacenter.to_s.downcase != 'eu'
19
- datacenter = nil
20
+ self.datacenter = nil
20
21
  end
22
+ self.base_url = "https://api#{self.datacenter}.samanage.com/"
21
23
  self.datacenter = datacenter if datacenter
22
- self.base_url = "https://api#{datacenter}.samanage.com/"
23
24
  self.content_type = 'json'
24
25
  self.admins = []
25
26
  if development_mode
@@ -46,22 +47,35 @@ module Samanage
46
47
  end
47
48
 
48
49
  # Calling execute without a method defaults to GET
49
- def execute(http_method: 'get', path: nil, payload: nil, verbose: nil, ssl_fix: false, token: nil)
50
+ def execute(http_method: 'get', path: nil, payload: nil, verbose: nil, ssl_fix: false)
51
+ if payload.class == String
52
+ begin
53
+ payload = JSON.parse(payload)
54
+ rescue => e
55
+ puts "Invalid JSON: #{payload.inspect}"
56
+ raise Samanage::Error(error: e, response: nil)
57
+ end
58
+ end
50
59
  token = token ||= self.token
51
60
  unless verbose.nil?
52
61
  verbose = '?layout=long'
53
62
  end
54
63
 
55
- api_call = HTTP.headers(
64
+ headers = {
56
65
  'Accept' => "application/vnd.samanage.v2.0+#{self.content_type}#{verbose}",
57
66
  'Content-type' => "application/#{self.content_type}",
58
- 'X-Samanage-Authorization' => 'Bearer ' + token
59
- )
60
- ctx = OpenSSL::SSL::SSLContext.new
61
- if ssl_fix
62
- ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
67
+ 'X-Samanage-Authorization' => 'Bearer ' + self.token
68
+ }
69
+ full_path = self.base_url + path
70
+ case http_method.to_s.downcase
71
+ when 'get'
72
+ api_call = HTTParty.get(full_path, headers: headers)
73
+ when 'post'
74
+ api_call = HTTParty.post(full_path, query: payload, headers: headers)
75
+ when 'put'
76
+ api_call = HTTParty.put(full_path, query: payload, headers: headers)
63
77
  end
64
- api_call = api_call.public_send(http_method.to_sym, self.base_url + Addressable::URI.encode_component(path, Addressable::URI::CharacterClasses::QUERY).gsub('+',"%2B"), :body => payload, ssl_context: ctx)
78
+
65
79
  response = Hash.new
66
80
  response[:code] = api_call.code.to_i
67
81
  response[:json] = api_call.body
@@ -71,9 +85,6 @@ module Samanage
71
85
  response[:total_pages] = 1 if response[:total_pages] == 0
72
86
  response[:total_count] = api_call.headers['X-Total-Count'].to_i
73
87
 
74
- # puts "Body Class: #{api_call.body.class}"
75
- # puts "#{api_call.body}"
76
- # Raise error if not Authentication or 200,201 == Success,Okay
77
88
  # Error cases
78
89
  case response[:code]
79
90
  when 200..201
@@ -82,7 +93,7 @@ module Samanage
82
93
  when 401
83
94
  response[:data] = api_call.body
84
95
  error = response[:response]
85
- self.authorized =false
96
+ self.authorized = false
86
97
  raise Samanage::AuthorizationError.new(error: error,response: response)
87
98
  when 404
88
99
  response[:data] = api_call.body
@@ -97,11 +108,6 @@ module Samanage
97
108
  error = response[:response]
98
109
  raise Samanage::InvalidRequest.new(error: error, response: response)
99
110
  end
100
- rescue HTTP::ConnectionError => e
101
- error = e.class
102
- response = nil
103
- raise Samanage::Error.new(error: error, response: response)
104
- # Always return response hash
105
111
  response
106
112
  end
107
113
 
data/samanage.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  $:.push File.expand_path("../lib", __FILE__)
2
2
  Gem::Specification.new do |s|
3
3
  s.name = 'samanage'
4
- s.version = '1.6.9'
4
+ s.version = '1.7.0'
5
5
  s.date = Date.today.strftime("%Y-%m-%d")
6
6
  s.summary = "Samanage Ruby Gem"
7
7
  s.description = "Connect to Samanage using Ruby!"
@@ -12,6 +12,6 @@ Gem::Specification.new do |s|
12
12
  s.license = 'MIT'
13
13
  s.require_paths = ["lib"]
14
14
  s.required_ruby_version = '>= 2.3'
15
- s.add_development_dependency 'http', ['~> 2.2']
16
- s.add_runtime_dependency 'http', ['~> 2.2']
15
+ s.add_development_dependency 'httparty', ['~> 2.2']
16
+ s.add_runtime_dependency 'httparty', ['~> 2.2']
17
17
  end
@@ -19,7 +19,7 @@ describe Samanage::Api do
19
19
  comment: {
20
20
  body: rand_text,
21
21
  }
22
- }.to_json
22
+ }
23
23
  api_call = @controller.create_comment(incident_id: incident_id, comment: comment)
24
24
 
25
25
  expect(api_call.dig(:data,'body')).to eq(rand_text)
@@ -1,5 +1,4 @@
1
1
  require 'samanage'
2
-
3
2
  describe Samanage::Api do
4
3
  context 'Hardware' do
5
4
  describe 'API Functions' do
@@ -20,16 +19,16 @@ describe Samanage::Api do
20
19
  expect(hardwares).to be_an(Array)
21
20
  expect(hardwares.size).to eq(hardware_count)
22
21
  end
23
- it 'create_hardware(payload: json): creates a hardware' do
22
+ it 'create_hardware(payload: payload): creates a hardware' do
24
23
  hardware_name = "samanage-ruby-#{(rand*10**10).ceil}"
25
24
  serial_number = (0...50).map { ('a'..'z').to_a[rand(26)] }.join
26
- json = {
25
+ payload = {
27
26
  :hardware => {
28
27
  :name => hardware_name,
29
28
  :bio => {:ssn => serial_number},
30
29
  }
31
30
  }
32
- hardware_create = @controller.create_hardware(payload: json.to_json)
31
+ hardware_create = @controller.create_hardware(payload: payload)
33
32
 
34
33
  expect(hardware_create[:data]['id']).to be_an(Integer)
35
34
  expect(hardware_create[:data]['name']).to eq(hardware_name)
@@ -37,12 +36,12 @@ describe Samanage::Api do
37
36
  end
38
37
  it 'create_hardware: fails if no serial' do
39
38
  hardware_name = "samanage-ruby-#{(rand*10**10).ceil}"
40
- json = {
39
+ payload = {
41
40
  :hardware => {
42
41
  :name => hardware_name,
43
42
  }
44
43
  }
45
- expect{@controller.create_hardware(payload: json.to_json)}.to raise_error(Samanage::InvalidRequest)
44
+ expect{@controller.create_hardware(payload: payload)}.to raise_error(Samanage::InvalidRequest)
46
45
  end
47
46
  it 'find_hardware: returns a hardware card by known id' do
48
47
  hardwares = @controller.collect_hardwares
@@ -75,12 +74,12 @@ describe Samanage::Api do
75
74
  hardwares = @controller.collect_hardwares
76
75
  sample_id = hardwares.sample['id']
77
76
  new_name = (0...50).map { ('a'..'z').to_a[rand(26)] }.join
78
- json = {
77
+ payload = {
79
78
  :hardware => {
80
79
  :name => new_name
81
80
  }
82
81
  }
83
- hardware_update = @controller.update_hardware(payload: json.to_json, id: sample_id)
82
+ hardware_update = @controller.update_hardware(payload: payload, id: sample_id)
84
83
  expect(hardware_update[:data]["name"]).to eq(new_name)
85
84
  expect(hardware_update[:code]).to eq(200).or(201)
86
85
  end
@@ -29,7 +29,7 @@ describe Samanage::Api do
29
29
  :description => "Description"
30
30
  }
31
31
  }
32
- incident_create = @controller.create_incident(payload: json.to_json)
32
+ incident_create = @controller.create_incident(payload: json)
33
33
 
34
34
  expect(incident_create[:data]['id']).to be_an(Integer)
35
35
  expect(incident_create[:data]['name']).to eq(incident_name)
@@ -43,7 +43,7 @@ describe Samanage::Api do
43
43
  :description => "Description"
44
44
  }
45
45
  }
46
- expect{@controller.create_incident(payload: json.to_json)}.to raise_error(Samanage::InvalidRequest)
46
+ expect{@controller.create_incident(payload: json)}.to raise_error(Samanage::InvalidRequest)
47
47
  end
48
48
  it 'find_incident: returns a incident card by known id' do
49
49
  incidents = @controller.collect_incidents
@@ -69,7 +69,7 @@ describe Samanage::Api do
69
69
  :description => description
70
70
  }
71
71
  }
72
- incident_update = @controller.update_incident(payload: incident_json.to_json, id: sample_id)
72
+ incident_update = @controller.update_incident(payload: incident_json, id: sample_id)
73
73
  expect(incident_update[:data]['description']).to eq(description)
74
74
  expect(incident_update[:code]).to eq(200).or(201)
75
75
  end
@@ -30,7 +30,7 @@ describe Samanage::Api do
30
30
  serial_number: serial_number,
31
31
  }
32
32
  }
33
- mobile_create = @controller.create_mobile(payload: json.to_json)
33
+ mobile_create = @controller.create_mobile(payload: json)
34
34
 
35
35
  expect(mobile_create[:data]['id']).to be_an(Integer)
36
36
  expect(mobile_create[:data]['manufacturer']).to eq(mobile_name)
@@ -43,7 +43,7 @@ describe Samanage::Api do
43
43
  manufacturer: mobile_name,
44
44
  }
45
45
  }
46
- expect{@controller.create_mobile(payload: json.to_json)}.to raise_error(Samanage::InvalidRequest)
46
+ expect{@controller.create_mobile(payload: json)}.to raise_error(Samanage::InvalidRequest)
47
47
  end
48
48
  it 'find_mobile: returns a mobile card by known id' do
49
49
  mobiles = @controller.collect_mobiles
@@ -68,7 +68,7 @@ describe Samanage::Api do
68
68
  :manufacturer => new_name
69
69
  }
70
70
  }
71
- mobile_update = @controller.update_mobile(payload: json.to_json, id: sample_id)
71
+ mobile_update = @controller.update_mobile(payload: json, id: sample_id)
72
72
  expect(mobile_update[:data]["manufacturer"]).to eq(new_name)
73
73
  expect(mobile_update[:code]).to eq(200).or(201)
74
74
  end
@@ -29,7 +29,7 @@ describe Samanage::Api do
29
29
  }
30
30
  }
31
31
 
32
- other_asset_create = @controller.create_other_asset(payload: json.to_json)
32
+ other_asset_create = @controller.create_other_asset(payload: json)
33
33
  expect(other_asset_create[:data]['id']).to be_an(Integer)
34
34
  expect(other_asset_create[:data]['name']).to eq(other_asset_name)
35
35
  expect(other_asset_create[:code]).to eq(200).or(201)
@@ -45,7 +45,7 @@ describe Samanage::Api do
45
45
  }
46
46
  }
47
47
  json[:other_asset].delete(json[:other_asset].keys.sample) # Delete random sample from the examples above
48
- expect{@controller.create_other_asset(payload: json.to_json)}.to raise_error(Samanage::InvalidRequest)
48
+ expect{@controller.create_other_asset(payload: json)}.to raise_error(Samanage::InvalidRequest)
49
49
  end
50
50
 
51
51
  it 'find_other_asset: returns an other_asset card by known id' do
@@ -72,7 +72,7 @@ describe Samanage::Api do
72
72
  :name => new_name
73
73
  }
74
74
  }
75
- other_asset_update = @controller.update_other_asset(payload: json.to_json, id: sample_id)
75
+ other_asset_update = @controller.update_other_asset(payload: json, id: sample_id)
76
76
  expect(other_asset_update[:data]["name"]).to eq(new_name)
77
77
  expect(other_asset_update[:code]).to eq(200).or(201)
78
78
  end
@@ -29,7 +29,7 @@ describe Samanage::Api do
29
29
  :email => email,
30
30
  }
31
31
  }
32
- user_create = @controller.create_user(payload: json.to_json)
32
+ user_create = @controller.create_user(payload: json)
33
33
  expect(user_create[:data]['email']).to eq(email)
34
34
  expect(user_create[:data]['id']).to be_an(Integer)
35
35
  expect(user_create[:data]['name']).to eq(user_name)
@@ -42,7 +42,7 @@ describe Samanage::Api do
42
42
  :name => user_name,
43
43
  }
44
44
  }
45
- expect{@controller.create_user(payload: json.to_json)}.to raise_error(Samanage::InvalidRequest)
45
+ expect{@controller.create_user(payload: json)}.to raise_error(Samanage::InvalidRequest)
46
46
  end
47
47
  it 'find_user: returns a user card by known id' do
48
48
  users = @controller.collect_users
@@ -78,7 +78,7 @@ describe Samanage::Api do
78
78
  :name => new_name
79
79
  }
80
80
  }
81
- user_update = @controller.update_user(payload: json.to_json, id: sample_id)
81
+ user_update = @controller.update_user(payload: json, id: sample_id)
82
82
  expect(user_update[:data]["name"]).to eq(new_name)
83
83
  expect(user_update[:code]).to eq(200).or(201)
84
84
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: samanage
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.9
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Walls
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-14 00:00:00.000000000 Z
11
+ date: 2017-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: http
14
+ name: httparty
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.2'
27
27
  - !ruby/object:Gem::Dependency
28
- name: http
28
+ name: httparty
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
@@ -50,7 +50,8 @@ files:
50
50
  - Guardfile
51
51
  - LICENSE
52
52
  - README.md
53
- - changelog.txt
53
+ - changelog.md
54
+ - lib/data/cacert.pem
54
55
  - lib/samanage.rb
55
56
  - lib/samanage/api.rb
56
57
  - lib/samanage/api/comments.rb
@@ -64,7 +65,6 @@ files:
64
65
  - lib/samanage/api/users.rb
65
66
  - lib/samanage/error.rb
66
67
  - lib/samanage/url_builder.rb
67
- - lib/test.rb
68
68
  - samanage.gemspec
69
69
  - spec/api/comments_spec.rb
70
70
  - spec/api/samanage_custom_field_spec.rb
data/changelog.txt DELETED
@@ -1 +0,0 @@
1
- 1.6.9 Added CRU support for Mobile Devices