foreman_api 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/foreman_api.gemspec CHANGED
@@ -9,7 +9,6 @@ Gem::Specification.new do |gem|
9
9
  gem.homepage = "http://github.com/mbacovsky/foreman_api"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
12
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
12
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
13
  gem.name = "foreman_api"
15
14
  gem.require_paths = ["lib"]
@@ -7,32 +7,28 @@ module ForemanApi
7
7
  class Base
8
8
  attr_reader :client
9
9
 
10
- def initialize(config = ForemanApi.client_config)
11
- @client = RestClient::Resource.new(config[:base_url],
10
+ def initialize(config = ForemanApi.client_config, options = { })
11
+ @client = RestClient::Resource.new config[:base_url],
12
12
  :user => config[:username],
13
13
  :password => config[:password],
14
14
  :oauth => config[:oauth],
15
15
  :headers => { :content_type => 'application/json',
16
- :accept => 'application/json' })
16
+ :accept => 'application/json' }
17
17
  end
18
18
 
19
- def call(method, path, payload = nil)
20
- if payload && (method == :post || method == :put)
21
- # TODO: is this neccessary? RestClient converts it automaticaly for me.
22
- payload = payload.to_json
23
- end
24
- ret = client[path].send(*([method, payload].compact))
25
- data = begin
26
- JSON.parse(ret.body)
27
- rescue JSON::ParserError
28
- ret.body
29
- end
30
- return data, ret
19
+ def call(method, path, options = { })
20
+ payload, headers, params = options.values_at :payload, :headers, :params
21
+ headers[:params] = params if params
22
+
23
+ args = [method]
24
+ args << payload.to_json if [:post, :put].include?(method)
25
+ args << headers if headers
26
+ process_data client[path].send(*args)
31
27
  end
32
28
 
33
29
  def validate_params!(options, valid_keys)
34
30
  return unless options.is_a?(Hash)
35
- invalid_keys = options.keys - (valid_keys.is_a?(Hash) ? valid_keys.keys : valid_keys)
31
+ invalid_keys = options.keys.map(&:to_s) - (valid_keys.is_a?(Hash) ? valid_keys.keys : valid_keys)
36
32
  raise ArgumentError, "Invalid keys: #{invalid_keys.join(", ")}" unless invalid_keys.empty?
37
33
 
38
34
  if valid_keys.is_a? Hash
@@ -44,5 +40,16 @@ module ForemanApi
44
40
  end
45
41
  end
46
42
 
43
+ protected
44
+
45
+ def process_data(response)
46
+ data = begin
47
+ JSON.parse(response.body)
48
+ rescue JSON::ParserError
49
+ response.body
50
+ end
51
+ return data, response
52
+ end
53
+
47
54
  end
48
55
  end
@@ -2,26 +2,26 @@ module ForemanApi
2
2
  module Resources
3
3
  class Architecture < ForemanApi::Base
4
4
 
5
- def index
6
- call(:get, "/api/architectures")
5
+ def index(headers = {})
6
+ call(:get, "/api/architectures", :headers => headers)
7
7
  end
8
8
 
9
- def show(id)
10
- call(:get, "/api/architectures/#{id}")
9
+ def show(id, headers = {})
10
+ call(:get, "/api/architectures/#{id}", :headers => headers)
11
11
  end
12
12
 
13
- def create(params = {})
13
+ def create(params = {}, headers = {})
14
14
  validate_params!(params, {"architecture"=>["name"]})
15
- call(:post, "/api/architectures", params)
15
+ call(:post, "/api/architectures", :payload => params, :headers => headers)
16
16
  end
17
17
 
18
- def update(id, params = {})
18
+ def update(id, params = {}, headers = {})
19
19
  validate_params!(params, {"architecture"=>["name"]})
20
- call(:put, "/api/architectures/#{id}", params)
20
+ call(:put, "/api/architectures/#{id}", :payload => params, :headers => headers)
21
21
  end
22
22
 
23
- def destroy(id)
24
- call(:delete, "/api/architectures/#{id}")
23
+ def destroy(id, headers = {})
24
+ call(:delete, "/api/architectures/#{id}", :headers => headers)
25
25
  end
26
26
 
27
27
  end
@@ -2,26 +2,26 @@ module ForemanApi
2
2
  module Resources
3
3
  class Bookmark < ForemanApi::Base
4
4
 
5
- def index
6
- call(:get, "/api/bookmarks")
5
+ def index(headers = {})
6
+ call(:get, "/api/bookmarks", :headers => headers)
7
7
  end
8
8
 
9
- def show(id)
10
- call(:get, "/api/bookmarks/#{id}")
9
+ def show(id, headers = {})
10
+ call(:get, "/api/bookmarks/#{id}", :headers => headers)
11
11
  end
12
12
 
13
- def create(params = {})
13
+ def create(params = {}, headers = {})
14
14
  validate_params!(params, {"bookmark"=>["name", "controller", "query"]})
15
- call(:post, "/api/bookmarks", params)
15
+ call(:post, "/api/bookmarks", :payload => params, :headers => headers)
16
16
  end
17
17
 
18
- def update(id, params = {})
18
+ def update(id, params = {}, headers = {})
19
19
  validate_params!(params, {"bookmark"=>["name", "controller", "query"]})
20
- call(:put, "/api/bookmarks/#{id}", params)
20
+ call(:put, "/api/bookmarks/#{id}", :payload => params, :headers => headers)
21
21
  end
22
22
 
23
- def destroy(id)
24
- call(:delete, "/api/bookmarks/#{id}")
23
+ def destroy(id, headers = {})
24
+ call(:delete, "/api/bookmarks/#{id}", :headers => headers)
25
25
  end
26
26
 
27
27
  end
@@ -2,12 +2,12 @@ module ForemanApi
2
2
  module Resources
3
3
  class Home < ForemanApi::Base
4
4
 
5
- def index
6
- call(:get, "/api")
5
+ def index(headers = {})
6
+ call(:get, "/api", :headers => headers)
7
7
  end
8
8
 
9
- def status
10
- call(:get, "/api/status")
9
+ def status(headers = {})
10
+ call(:get, "/api/status", :headers => headers)
11
11
  end
12
12
 
13
13
  end
@@ -2,31 +2,31 @@ module ForemanApi
2
2
  module Resources
3
3
  class OperatingSystem < ForemanApi::Base
4
4
 
5
- def index
6
- call(:get, "/api/operatingsystems")
5
+ def index(headers = {})
6
+ call(:get, "/api/operatingsystems", :headers => headers)
7
7
  end
8
8
 
9
- def show(id)
10
- call(:get, "/api/operatingsystems/#{id}")
9
+ def show(id, headers = {})
10
+ call(:get, "/api/operatingsystems/#{id}", :headers => headers)
11
11
  end
12
12
 
13
- def create(params = {})
13
+ def create(params = {}, headers = {})
14
14
  validate_params!(params, {"operatingsystem"=>["name", "major", "minor"]})
15
- call(:post, "/api/operatingsystems", params)
15
+ call(:post, "/api/operatingsystems", :payload => params, :headers => headers)
16
16
  end
17
17
 
18
- def update(id, params = {})
18
+ def update(id, params = {}, headers = {})
19
19
  validate_params!(params, {"operatingsystem"=>["name", "major", "minor"]})
20
- call(:put, "/api/operatingsystems/#{id}", params)
20
+ call(:put, "/api/operatingsystems/#{id}", :payload => params, :headers => headers)
21
21
  end
22
22
 
23
- def destroy(id)
24
- call(:delete, "/api/operatingsystems/#{id}")
23
+ def destroy(id, headers = {})
24
+ call(:delete, "/api/operatingsystems/#{id}", :headers => headers)
25
25
  end
26
26
 
27
- def bootfiles(id, params = {})
27
+ def bootfiles(id, params = {}, headers = {})
28
28
  validate_params!(params, ["medium", "architecture"])
29
- call(:get, "/api/operatingsystems/#{id}/bootfiles", :params => params)
29
+ call(:get, "/api/operatingsystems/#{id}/bootfiles", :params => params, :headers => headers)
30
30
  end
31
31
 
32
32
  end
@@ -2,26 +2,26 @@ module ForemanApi
2
2
  module Resources
3
3
  class User < ForemanApi::Base
4
4
 
5
- def index
6
- call(:get, "/api/users")
5
+ def index(headers = {})
6
+ call(:get, "/api/users", :headers => headers)
7
7
  end
8
8
 
9
- def show(id)
10
- call(:get, "/api/users/#{id}")
9
+ def show(id, headers = {})
10
+ call(:get, "/api/users/#{id}", :headers => headers)
11
11
  end
12
12
 
13
- def create(params = {})
14
- validate_params!(params, {"user"=>["login", "firstname", "lastname", "mail", "admin"]})
15
- call(:post, "/api/users", params)
13
+ def create(params = {}, headers = {})
14
+ validate_params!(params, {"user"=>["login", "firstname", "lastname", "mail", "admin", "password", "auth_source_id"]})
15
+ call(:post, "/api/users", :payload => params, :headers => headers)
16
16
  end
17
17
 
18
- def update(id, params = {})
19
- validate_params!(params, {"user"=>["login", "firstname", "lastname", "mail", "admin"]})
20
- call(:put, "/api/users/#{id}", params)
18
+ def update(id, params = {}, headers = {})
19
+ validate_params!(params, {"user"=>["login", "firstname", "lastname", "mail", "admin", "password"]})
20
+ call(:put, "/api/users/#{id}", :payload => params, :headers => headers)
21
21
  end
22
22
 
23
- def destroy(id)
24
- call(:delete, "/api/users/#{id}")
23
+ def destroy(id, headers = {})
24
+ call(:delete, "/api/users/#{id}", :headers => headers)
25
25
  end
26
26
 
27
27
  end
@@ -1,3 +1,3 @@
1
1
  module ForemanApi
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_api
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Martin Ba\xC4\x8Dovsk\xC3\xBD"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-08-09 00:00:00 Z
18
+ date: 2012-08-14 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: json