bodhi-slam 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 918cd6ede8d2d5cd62621b10fa6c80db870ef708
4
- data.tar.gz: 26cbbadd0c463339016f7b60d4e87993373aaf5a
3
+ metadata.gz: d83b1c6ce8f644e9108c2c8fc4c3b0996851d572
4
+ data.tar.gz: 75a57b10b96e572fad43651f55d2d037950f16e6
5
5
  SHA512:
6
- metadata.gz: 0d8e3134e705a6aeafe8fbea15831e1e3aefe5dca26e466c6484420531e5da67bb7436761c1c80db6077441cc8b3437726ad1d99250ec8fab3b72f055590b197
7
- data.tar.gz: 3db2df8a8b36503709c1c9589a574977024d19b2bb8ac78eb9780001e592269d1be7f8c197867514d50644220fad3d5a89d35fc77a547b1d08d0dad1b00b9658
6
+ metadata.gz: 8ce0f4f00314d5d366450444f39d2883314352a679a08876580252417df685f680a105e4525f6d9a7707d43f4652552357d538d2e0bd3cf5175cb0e6d0591d50
7
+ data.tar.gz: 0a5b7e3955f4b3a331e2aba1741d6439cccd81c9ed353b3f00351b110eaef575d4ca60ddc10a919e8a0a8a48c80e0d360c056672cb595e636b6174817249cc72
data/lib/bodhi-slam.rb CHANGED
@@ -6,14 +6,15 @@ require "SecureRandom"
6
6
 
7
7
  require 'bodhi-slam/validations'
8
8
  require 'bodhi-slam/errors'
9
+ require 'bodhi-slam/context'
9
10
 
10
11
  require 'bodhi-slam/batches'
11
- require 'bodhi-slam/context'
12
12
  require 'bodhi-slam/enumerations'
13
13
  require 'bodhi-slam/factory'
14
14
  require 'bodhi-slam/resource'
15
15
  require 'bodhi-slam/types'
16
16
  require 'bodhi-slam/users'
17
+ require 'bodhi-slam/profiles'
17
18
 
18
19
  class BodhiSlam
19
20
  # Defines a context to interact with the Bodhi API
@@ -0,0 +1,110 @@
1
+ module Bodhi
2
+ class Profile
3
+ include Bodhi::Validations
4
+
5
+ ATTRIBUTES = [:name, :namespace, :dml, :subspace, :parent, :invoke]
6
+ attr_accessor *ATTRIBUTES
7
+ attr_accessor :bodhi_context
8
+
9
+ validates :name, type: "String", required: true, is_not_blank: true
10
+ validates :namespace, type: "String", required: true
11
+ validates :dml, type: "Object", required: true
12
+
13
+ def initialize(params={})
14
+ # same as symbolize_keys!
15
+ params = params.reduce({}) do |memo, (k, v)|
16
+ memo.merge({ k.to_sym => v})
17
+ end
18
+
19
+ # set attributes
20
+ ATTRIBUTES.each do |attribute|
21
+ send("#{attribute}=", params[attribute])
22
+ end
23
+ end
24
+
25
+ # Returns a Hash of the Objects form attributes
26
+ #
27
+ # s = SomeResource.factory.build({foo:"test", bar:12345})
28
+ # s.attributes # => { foo: "test", bar: 12345 }
29
+ def attributes
30
+ result = Hash.new
31
+ ATTRIBUTES.each do |attribute|
32
+ result[attribute] = send(attribute)
33
+ end
34
+ result
35
+ end
36
+
37
+ # Returns all the Objects attributes as JSON.
38
+ # It converts any nested Objects to JSON if they respond to +to_json+
39
+ #
40
+ # s = SomeResource.factory.build({foo:"test", bar:12345})
41
+ # s.to_json # => "{ 'foo':'test', 'bar':12345 }"
42
+ def to_json(base=nil)
43
+ super if base
44
+ attributes.to_json
45
+ end
46
+
47
+ # Saves the resource to the Bodhi Cloud. Raises ArgumentError if record could not be saved.
48
+ #
49
+ # obj = Resouce.new
50
+ # obj.save!
51
+ # obj.persisted? # => true
52
+ def save!
53
+ result = bodhi_context.connection.post do |request|
54
+ request.url "/#{bodhi_context.namespace}/profiles"
55
+ request.headers['Content-Type'] = 'application/json'
56
+ request.headers[bodhi_context.credentials_header] = bodhi_context.credentials
57
+ request.body = attributes.to_json
58
+ end
59
+
60
+ if result.status != 201
61
+ raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
62
+ end
63
+ end
64
+
65
+ def delete!
66
+ result = bodhi_context.connection.delete do |request|
67
+ request.url "/#{bodhi_context.namespace}/profiles/#{name}"
68
+ request.headers[bodhi_context.credentials_header] = bodhi_context.credentials
69
+ end
70
+
71
+ if result.status != 204
72
+ raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
73
+ end
74
+ end
75
+
76
+ # Returns a factory for the Bodhi::User class
77
+ def self.factory
78
+ @factory ||= Bodhi::Factory.new(Bodhi::Profile).tap do |factory|
79
+ factory.add_generator(:name, type: "String", required: true, is_not_blank: true)
80
+ factory.add_generator(:namespace, type: "String", required: true)
81
+ factory.add_generator(:dml, type: "Object", required: true)
82
+ end
83
+ end
84
+
85
+ # Queries the Bodhi API for the given +user_name+ and
86
+ # returns a Bodhi::Profile
87
+ #
88
+ # context = BodhiContext.new(valid_params)
89
+ # profile = Bodhi::Profile.find(context, "Profile1")
90
+ # profile # => #<Bodhi::Profile:0x007fbff403e808 @name="Profile1">
91
+ def self.find(context, profile_name)
92
+ if context.invalid?
93
+ raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
94
+ end
95
+
96
+ result = context.connection.get do |request|
97
+ request.url "/#{context.namespace}/profiles/#{profile_name}"
98
+ request.headers[context.credentials_header] = context.credentials
99
+ end
100
+
101
+ if result.status != 200
102
+ raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
103
+ end
104
+
105
+ profile = Bodhi::Profile.new(JSON.parse(result.body))
106
+ profile.bodhi_context = context
107
+ profile
108
+ end
109
+ end
110
+ end
@@ -124,7 +124,7 @@ module Bodhi
124
124
  # type # => #<Bodhi::Type:0x007fbff403e808 @name="MyTypeName">
125
125
  def self.find(context, type_name)
126
126
  if context.invalid?
127
- raise context.errors, context.errors.to_a.to_s
127
+ raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
128
128
  end
129
129
 
130
130
  result = context.connection.get do |request|
@@ -136,7 +136,7 @@ module Bodhi
136
136
  raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
137
137
  end
138
138
 
139
- type = Bodhi::Type.new(result)
139
+ type = Bodhi::Type.new(JSON.parse(result.body))
140
140
  type.bodhi_context = context
141
141
  type
142
142
  end
@@ -148,7 +148,10 @@ module Bodhi
148
148
  # types = Bodhi::Type.find_all(context)
149
149
  # types # => [#<Bodhi::Type:0x007fbff403e808 @name="MyType">, #<Bodhi::Type:0x007fbff403e808 @name="MyType2">, ...]
150
150
  def self.find_all(context)
151
- raise context.errors unless context.valid?
151
+ if context.invalid?
152
+ raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
153
+ end
154
+
152
155
  page = 1
153
156
  all_records = []
154
157
 
@@ -98,7 +98,7 @@ module Bodhi
98
98
  # user # => #<Bodhi::User:0x007fbff403e808 @username="User1">
99
99
  def self.find(context, user_name)
100
100
  if context.invalid?
101
- raise context.errors, context.errors.to_a.to_s
101
+ raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
102
102
  end
103
103
 
104
104
  result = context.connection.get do |request|
@@ -110,7 +110,7 @@ module Bodhi
110
110
  raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
111
111
  end
112
112
 
113
- user = Bodhi::User.new(result)
113
+ user = Bodhi::User.new(JSON.parse(result.body))
114
114
  user.bodhi_context = context
115
115
  user
116
116
  end
@@ -121,7 +121,7 @@ module Bodhi
121
121
  # user_properties = Bodhi::User.find_me(context)
122
122
  def self.find_me(context)
123
123
  if context.invalid?
124
- raise context.errors, context.errors.to_a.to_s
124
+ raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
125
125
  end
126
126
 
127
127
  result = context.connection.get do |request|
@@ -133,7 +133,7 @@ module Bodhi
133
133
  raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
134
134
  end
135
135
 
136
- result
136
+ JSON.parse(result.body)
137
137
  end
138
138
  end
139
139
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bodhi-slam
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - willdavis
@@ -109,6 +109,7 @@ files:
109
109
  - lib/bodhi-slam/errors/api.rb
110
110
  - lib/bodhi-slam/errors/context.rb
111
111
  - lib/bodhi-slam/factory.rb
112
+ - lib/bodhi-slam/profiles.rb
112
113
  - lib/bodhi-slam/resource.rb
113
114
  - lib/bodhi-slam/types.rb
114
115
  - lib/bodhi-slam/users.rb