bodhi-slam 0.0.2 → 0.0.4

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: aebbd3db6ff0172b4f7c75c0904c0bd32342cae8
4
- data.tar.gz: 21fa5160b6874bf4e1a0c03cafef586fb0c7b001
3
+ metadata.gz: a1190f2e6cece5ef43cb0e44f2ae900b1d84768a
4
+ data.tar.gz: a1f3103fe42f11f76a4b6385cc608cd195fe1f4d
5
5
  SHA512:
6
- metadata.gz: 5ede1f19f9b62ae410eff5cabf7b776c1e9bbb2d01a55652e754925ed59635b9e3f1545b3a0489775d7ca8beb1ee9b1256a745e05cccfcb8ab8021f3b4bf8559
7
- data.tar.gz: ab20c11285b9aac39b5e4452c52abf54352735d58b3c1ab0c44f9e7f8d67fb2dac01978a0131b297562679e462d9bc76d9d3e4e3ca1ee6789f24304b956f0ce2
6
+ metadata.gz: ea4a4951ac8e977e7ed264b72517306f83f28e25ec381ec32601f7af193158dcf2201d5376bd067b7c980822fed001d8c431672fbd3b82a4baefb16fcf31ecca
7
+ data.tar.gz: d24addea94ee3896b43737fb5f1422979b281367802e1145b875650dd0a51eb951e73d830111e1327ce087bace2240fc43664860ac34b5f6341cc134c02157db
@@ -3,6 +3,9 @@ require "factory_girl"
3
3
  require "json"
4
4
  require "time"
5
5
 
6
+ require 'bodhi-slam/context'
7
+ require 'bodhi-slam/resource'
8
+
6
9
  class BodhiSlam
7
10
  def self.context(params, &block)
8
11
  bodhi_context = BodhiContext.new params
@@ -46,205 +49,87 @@ class BodhiSlam
46
49
  end
47
50
  enumerations = JSON.parse(result.body)
48
51
 
49
- #Create a class & factory for each type
50
- setup_factory = lambda do |name, properties|
51
- FactoryGirl.define do
52
- factory name.to_sym do
53
- properties.each_pair do |k,v|
54
- unless v["system"]
55
-
56
- case v["type"]
57
- when "GeoJSON"
58
- send(k) { {type: "Point", coordinates: [10,20]} } if v["multi"].nil?
59
- send(k) { [*0..5].sample.times.collect{ {type: "Point", coordinates: [10,20]} } } if v["multi"]
60
- when "Boolean"
61
- send(k) { [true, false].sample } if v["multi"].nil?
62
- send(k) { [*0..5].sample.times.collect{ [true, false].sample } } if v["multi"]
63
- when "Enumerated"
64
- enum = enumerations.select{ |enumeration| enumeration["name"] == v["ref"].split('.')[0] }[0]
65
- send(k) { enum["values"].sample[v["ref"].split('.')[1]] } if v["multi"].nil?
66
- send(k) { [*0..5].sample.times.collect{ enum["values"].sample[v["ref"].split('.')[1]] } } if v["multi"]
67
- when "Object"
68
- send(k) { {"foo" => SecureRandom.hex} } if v["multi"].nil?
69
- send(k) { [*0..5].sample.times.collect{ {"foo" => SecureRandom.hex} } } if v["multi"]
70
- when "String"
71
- send(k) { SecureRandom.hex } if v["multi"].nil?
72
- send(k) { [*0..5].sample.times.collect{ SecureRandom.hex } } if v["multi"]
73
- when "DateTime"
74
- send(k) { Time.at(rand * Time.now.to_i).iso8601 } if v["multi"].nil?
75
- send(k) { [*0..5].sample.times.collect{ Time.at(rand * Time.now.to_i).iso8601 } } if v["multi"]
76
- when "Integer"
77
- send(k) { SecureRandom.random_number(100) } if v["multi"].nil?
78
- send(k) { [*0..5].sample.times.collect{ SecureRandom.random_number(100) } } if v["multi"]
79
- when "Real"
80
- send(k) { SecureRandom.random_number } if v["multi"].nil?
81
- send(k) { [*0..5].sample.times.collect{ SecureRandom.random_number } } if v["multi"]
82
- else # Its an embedded type
83
- send(k) { FactoryGirl.build(v["type"]).attributes } if v["multi"].nil?
84
- send(k) { [*0..5].sample.times.collect{ FactoryGirl.build(v["type"]).attributes } } if v["multi"]
85
- end
86
-
87
- end
88
- end
89
- end
90
- end
91
- end
92
-
93
- create_type = lambda do |type|
94
- if type["package"] != "system"
95
- properties = type["properties"].keys.collect{ |key| key.to_sym }
96
- klass = Object.const_set(type["name"], Class.new {
97
- include BodhiResource
98
- attr_accessor *properties
99
- })
100
- klass.define_singleton_method(:find) do |id, context|
101
- result = context.connection.get do |request|
102
- request.url "/#{context.namespace}/resources/#{klass.name}/#{id}"
103
- request.headers[context.credentials_header] = context.credentials
104
- end
105
-
106
- if result.status != 200
107
- errors = JSON.parse result.body
108
- errors.each{|error| error['status'] = result.status } if errors.is_a? Array
109
- errors["status"] = result.status if errors.is_a? Hash
110
- raise errors.to_s
111
- end
112
-
113
- object_hash = JSON.parse(result.body)
114
- object_hash["bodhi_context"] = context
115
- return FactoryGirl.build(klass.name, object_hash)
116
- end
117
-
118
- setup_factory.call(klass.name, type["properties"]) unless FactoryGirl.factories.registered?(klass.name)
119
- puts "Created Class & Factory for: #{klass.name}"
120
- end
121
- end
122
-
123
52
  embedded_types = types.select{ |type| type["embedded"] }
124
53
  normal_types = types.select{ |type| !type["embedded"] }
125
54
 
126
- embedded_types.each{ |type| create_type.call(type) }
127
- normal_types.each{ |type| create_type.call(type) }
55
+ embedded_types.each{ |type| create_type(type, enumerations) }
56
+ normal_types.each{ |type| create_type(type, enumerations) }
128
57
 
129
58
  #Party!
130
59
  end
131
- end
132
-
133
-
134
- class BodhiContext
135
- attr_reader :connection, :server, :namespace,
136
- :credentials, :credentials_type, :credentials_header
137
-
138
- def initialize(params)
139
- params.symbolize_keys!
140
-
141
- @connection = Faraday.new(url: params[:server]) do |faraday|
142
- faraday.request :url_encoded # form-encode POST params
143
- #faraday.response :logger # log requests to STDOUT
144
- faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
145
- end
146
- @server = params[:server]
147
- @namespace = params[:namespace]
148
-
149
- if params[:cookie]
150
- @credentials = params[:cookie]
151
- @credentials_header = "Cookie"
152
- @credentials_type = "HTTP_COOKIE"
153
- else
154
- @credentials = @connection.basic_auth params[:username], params[:password]
155
- @credentials_header = "Authorization"
156
- @credentials_type = "HTTP_BASIC"
157
- end
158
- end
159
-
160
- def attributes
161
- attributes = Hash.new
162
- self.instance_variables.each do |variable|
163
- attribute_name = variable.to_s.delete('@').to_sym
164
- attributes[attribute_name] = send(attribute_name) unless [:connection, :credentials_header].include?(attribute_name)
165
- end
166
- attributes
167
- end
168
60
 
169
- def validate!
170
- raise ArgumentError, "Server URL must be a String" unless server.is_a?(String)
171
- raise ArgumentError, "Namespace name must be a String" unless namespace.is_a?(String)
172
- end
173
- end
61
+ # - Create a BodhiResource from the given type definition and enumerations
62
+ def self.create_type(type, enumerations)
63
+ if type["package"] != "system"
64
+ properties = type["properties"].keys.collect{ |key| key.to_sym }
65
+ klass = Object.const_set(type["name"], Class.new {
66
+ include BodhiResource
67
+ attr_accessor *properties
68
+ })
69
+ klass.define_singleton_method(:find) do |id, context|
70
+ result = context.connection.get do |request|
71
+ request.url "/#{context.namespace}/resources/#{klass.name}/#{id}"
72
+ request.headers[context.credentials_header] = context.credentials
73
+ end
174
74
 
75
+ if result.status != 200
76
+ errors = JSON.parse result.body
77
+ errors.each{|error| error['status'] = result.status } if errors.is_a? Array
78
+ errors["status"] = result.status if errors.is_a? Hash
79
+ raise errors.to_s
80
+ end
175
81
 
176
- module BodhiResource
177
- SYSTEM_ATTRIBUTES = [:bodhi_context, :sys_created_at, :sys_version, :sys_modified_at, :sys_modified_by,
178
- :sys_namespace, :sys_created_by, :sys_type_version, :sys_id]
179
- attr_accessor *SYSTEM_ATTRIBUTES
180
-
181
- # - Returns a Hash of the Objects form attributes
182
- def attributes
183
- attributes = Hash.new
184
- self.instance_variables.each do |variable|
185
- attribute_name = variable.to_s.delete('@').to_sym
186
- attributes[attribute_name] = send(attribute_name) unless SYSTEM_ATTRIBUTES.include?(attribute_name)
187
- end
188
- attributes
189
- end
190
-
191
- # - Converts all the Objects attributes to JSON
192
- def to_json
193
- attributes = Hash.new
194
- self.instance_variables.each do |variable|
195
- attribute_name = variable.to_s.delete('@').to_sym
196
- attributes[attribute_name] = send(attribute_name)
197
- end
198
- attributes.to_json
199
- end
82
+ object_hash = JSON.parse(result.body)
83
+ object_hash["bodhi_context"] = context
84
+ return FactoryGirl.build(klass.name, object_hash)
85
+ end
200
86
 
201
- def save!
202
- result = bodhi_context.connection.post do |request|
203
- request.url "/#{bodhi_context.namespace}/resources/#{self.class}"
204
- request.headers['Content-Type'] = 'application/json'
205
- request.headers[bodhi_context.credentials_header] = bodhi_context.credentials
206
- request.body = attributes.to_json
207
- end
208
-
209
- if result.status != 201
210
- errors = JSON.parse result.body
211
- errors.each{|error| error['status'] = result.status } if errors.is_a? Array
212
- errors["status"] = result.status if errors.is_a? Hash
213
- raise errors.to_s
214
- end
215
-
216
- if result.headers['location']
217
- @sys_id = result.headers['location'].match(/(?<id>[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12})/)[:id]
87
+ create_factory(klass.name, type["properties"], enumerations) unless FactoryGirl.factories.registered?(klass.name)
88
+ puts "Created Class & Factory for: #{klass.name}"
218
89
  end
219
90
  end
220
-
221
- def delete!
222
- result = bodhi_context.connection.delete do |request|
223
- request.url "/#{bodhi_context.namespace}/resources/#{self.class}/#{sys_id}"
224
- request.headers[bodhi_context.credentials_header] = bodhi_context.credentials
225
- end
226
91
 
227
- if result.status != 204
228
- errors = JSON.parse result.body
229
- errors.each{|error| error['status'] = result.status } if errors.is_a? Array
230
- errors["status"] = result.status if errors.is_a? Hash
231
- raise errors.to_s
92
+ # - Create a Factory with the given name, properties, and available enumerations
93
+ def self.create_factory(name, properties, enumerations)
94
+ FactoryGirl.define do
95
+ factory name.to_sym do
96
+ properties.each_pair do |k,v|
97
+ unless v["system"]
98
+
99
+ case v["type"]
100
+ when "GeoJSON"
101
+ send(k) { {type: "Point", coordinates: [10,20]} } if v["multi"].nil?
102
+ send(k) { [*0..5].sample.times.collect{ {type: "Point", coordinates: [10,20]} } } if v["multi"]
103
+ when "Boolean"
104
+ send(k) { [true, false].sample } if v["multi"].nil?
105
+ send(k) { [*0..5].sample.times.collect{ [true, false].sample } } if v["multi"]
106
+ when "Enumerated"
107
+ enum = enumerations.select{ |enumeration| enumeration["name"] == v["ref"].split('.')[0] }[0]
108
+ send(k) { enum["values"].sample[v["ref"].split('.')[1]] } if v["multi"].nil?
109
+ send(k) { [*0..5].sample.times.collect{ enum["values"].sample[v["ref"].split('.')[1]] } } if v["multi"]
110
+ when "Object"
111
+ send(k) { {"foo" => SecureRandom.hex} } if v["multi"].nil?
112
+ send(k) { [*0..5].sample.times.collect{ {"foo" => SecureRandom.hex} } } if v["multi"]
113
+ when "String"
114
+ send(k) { SecureRandom.hex } if v["multi"].nil?
115
+ send(k) { [*0..5].sample.times.collect{ SecureRandom.hex } } if v["multi"]
116
+ when "DateTime"
117
+ send(k) { Time.at(rand * Time.now.to_i).iso8601 } if v["multi"].nil?
118
+ send(k) { [*0..5].sample.times.collect{ Time.at(rand * Time.now.to_i).iso8601 } } if v["multi"]
119
+ when "Integer"
120
+ send(k) { SecureRandom.random_number(100) } if v["multi"].nil?
121
+ send(k) { [*0..5].sample.times.collect{ SecureRandom.random_number(100) } } if v["multi"]
122
+ when "Real"
123
+ send(k) { SecureRandom.random_number } if v["multi"].nil?
124
+ send(k) { [*0..5].sample.times.collect{ SecureRandom.random_number } } if v["multi"]
125
+ else # Its an embedded type
126
+ send(k) { FactoryGirl.build(v["type"]).attributes } if v["multi"].nil?
127
+ send(k) { [*0..5].sample.times.collect{ FactoryGirl.build(v["type"]).attributes } } if v["multi"]
128
+ end
129
+
130
+ end
131
+ end
132
+ end
232
133
  end
233
134
  end
234
-
235
- def patch!(params)
236
- result = bodhi_context.connection.patch do |request|
237
- request.url "/#{bodhi_context.namespace}/resources/#{self.class}/#{sys_id}"
238
- request.headers['Content-Type'] = 'application/json'
239
- request.headers[bodhi_context.credentials_header] = bodhi_context.credentials
240
- request.body = params
241
- end
242
-
243
- if result.status != 204
244
- errors = JSON.parse result.body
245
- errors.each{|error| error['status'] = result.status } if errors.is_a? Array
246
- errors["status"] = result.status if errors.is_a? Hash
247
- raise errors.to_s
248
- end
249
- end
250
135
  end
@@ -0,0 +1,40 @@
1
+ class BodhiContext
2
+ attr_reader :connection, :server, :namespace,
3
+ :credentials, :credentials_type, :credentials_header
4
+
5
+ def initialize(params)
6
+ params.symbolize_keys!
7
+
8
+ @connection = Faraday.new(url: params[:server]) do |faraday|
9
+ faraday.request :url_encoded # form-encode POST params
10
+ #faraday.response :logger # log requests to STDOUT
11
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
12
+ end
13
+ @server = params[:server]
14
+ @namespace = params[:namespace]
15
+
16
+ if params[:cookie]
17
+ @credentials = params[:cookie]
18
+ @credentials_header = "Cookie"
19
+ @credentials_type = "HTTP_COOKIE"
20
+ else
21
+ @credentials = @connection.basic_auth params[:username], params[:password]
22
+ @credentials_header = "Authorization"
23
+ @credentials_type = "HTTP_BASIC"
24
+ end
25
+ end
26
+
27
+ def attributes
28
+ attributes = Hash.new
29
+ self.instance_variables.each do |variable|
30
+ attribute_name = variable.to_s.delete('@').to_sym
31
+ attributes[attribute_name] = send(attribute_name) unless [:connection, :credentials_header].include?(attribute_name)
32
+ end
33
+ attributes
34
+ end
35
+
36
+ def validate!
37
+ raise ArgumentError, "Server URL must be a String" unless server.is_a?(String)
38
+ raise ArgumentError, "Namespace name must be a String" unless namespace.is_a?(String)
39
+ end
40
+ end
@@ -0,0 +1,75 @@
1
+ module BodhiResource
2
+ SYSTEM_ATTRIBUTES = [:bodhi_context, :sys_created_at, :sys_version, :sys_modified_at, :sys_modified_by,
3
+ :sys_namespace, :sys_created_by, :sys_type_version, :sys_id]
4
+ attr_accessor *SYSTEM_ATTRIBUTES
5
+
6
+ # - Returns a Hash of the Objects form attributes
7
+ def attributes
8
+ attributes = Hash.new
9
+ self.instance_variables.each do |variable|
10
+ attribute_name = variable.to_s.delete('@').to_sym
11
+ attributes[attribute_name] = send(attribute_name) unless SYSTEM_ATTRIBUTES.include?(attribute_name)
12
+ end
13
+ attributes
14
+ end
15
+
16
+ # - Converts all the Objects attributes to JSON
17
+ def to_json
18
+ attributes = Hash.new
19
+ self.instance_variables.each do |variable|
20
+ attribute_name = variable.to_s.delete('@').to_sym
21
+ attributes[attribute_name] = send(attribute_name)
22
+ end
23
+ attributes.to_json
24
+ end
25
+
26
+ def save!
27
+ result = bodhi_context.connection.post do |request|
28
+ request.url "/#{bodhi_context.namespace}/resources/#{self.class}"
29
+ request.headers['Content-Type'] = 'application/json'
30
+ request.headers[bodhi_context.credentials_header] = bodhi_context.credentials
31
+ request.body = attributes.to_json
32
+ end
33
+
34
+ if result.status != 201
35
+ errors = JSON.parse result.body
36
+ errors.each{|error| error['status'] = result.status } if errors.is_a? Array
37
+ errors["status"] = result.status if errors.is_a? Hash
38
+ raise errors.to_s
39
+ end
40
+
41
+ if result.headers['location']
42
+ @sys_id = result.headers['location'].match(/(?<id>[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12})/)[:id]
43
+ end
44
+ end
45
+
46
+ def delete!
47
+ result = bodhi_context.connection.delete do |request|
48
+ request.url "/#{bodhi_context.namespace}/resources/#{self.class}/#{sys_id}"
49
+ request.headers[bodhi_context.credentials_header] = bodhi_context.credentials
50
+ end
51
+
52
+ if result.status != 204
53
+ errors = JSON.parse result.body
54
+ errors.each{|error| error['status'] = result.status } if errors.is_a? Array
55
+ errors["status"] = result.status if errors.is_a? Hash
56
+ raise errors.to_s
57
+ end
58
+ end
59
+
60
+ def patch!(params)
61
+ result = bodhi_context.connection.patch do |request|
62
+ request.url "/#{bodhi_context.namespace}/resources/#{self.class}/#{sys_id}"
63
+ request.headers['Content-Type'] = 'application/json'
64
+ request.headers[bodhi_context.credentials_header] = bodhi_context.credentials
65
+ request.body = params
66
+ end
67
+
68
+ if result.status != 204
69
+ errors = JSON.parse result.body
70
+ errors.each{|error| error['status'] = result.status } if errors.is_a? Array
71
+ errors["status"] = result.status if errors.is_a? Hash
72
+ raise errors.to_s
73
+ end
74
+ end
75
+ 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.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Will Davis
@@ -59,6 +59,8 @@ extensions: []
59
59
  extra_rdoc_files: []
60
60
  files:
61
61
  - lib/bodhi-slam.rb
62
+ - lib/bodhi-slam/context.rb
63
+ - lib/bodhi-slam/resource.rb
62
64
  homepage:
63
65
  licenses:
64
66
  - MIT
@@ -79,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
81
  version: '0'
80
82
  requirements: []
81
83
  rubyforge_project:
82
- rubygems_version: 2.4.5
84
+ rubygems_version: 2.2.2
83
85
  signing_key:
84
86
  specification_version: 4
85
87
  summary: Generate data and push to the Bodhi API