bodhi-slam 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6df30b96a6aec463e6caa1ccbd279e47d27a57a2
4
- data.tar.gz: 6852921c1be4570e84dab5869713af2f2f95bd81
3
+ metadata.gz: 0787ec3a9705faa25dab09c506a0a220e8d7d9a8
4
+ data.tar.gz: 7e5686d4a4417d53c0743596ef2b9e758c878a7b
5
5
  SHA512:
6
- metadata.gz: 7a04d8bf2df4d929e2d533283610f9d93eef65479383250d93c91e4cd22a27ae2b1ed14fdbe3c476b1576561acd85b84bd19e0d9479c18c7a88a2e66ff892d6c
7
- data.tar.gz: 1df3159b31651ba636903099ac5f7ac5c1dd85bca80866c7de94be3ba1be5ac52c7e34e387ecd1a1cd591a770f44f8b714d7356e6eeee081edc30395720e79eb
6
+ metadata.gz: bea65f3aed37d29efd1ef2fff6ab918341037656b2db0720000fc35d5aaa2645f60acc77b86bdab2c9714b81bb209af68ee7d1678b4d3258e6c8e1b0a9c96d99
7
+ data.tar.gz: 623c0e593b4e1b465bd3a4b5ad4890618b7d8e841c031367b18ca3a7efb33901c60bdc6e8bfdbb8ffe5f139a7a0e715e17da48773f6a468ea607c604161440a9
@@ -32,9 +32,7 @@ module Bodhi
32
32
  raise Bodhi::ApiErrors.new(body: response.body, status: response.status), "status: #{response.status}, body: #{response.body}"
33
33
  end
34
34
 
35
- # Parse the result body and update records with their sys_id
36
- response_body = JSON.parse(response.body)
37
- results = response_body.zip(records)
35
+ results = response.body.zip(records)
38
36
  results.each do |response, record|
39
37
  if response["location"]
40
38
  record.sys_id = response["location"].match(/(?<id>[a-zA-Z0-9]{24})/)[:id]
@@ -3,19 +3,28 @@ require 'bodhi-slam/validations'
3
3
  module Bodhi
4
4
  class Context
5
5
  include Bodhi::Validations
6
- attr_reader :connection, :server, :namespace,
7
- :credentials, :credentials_type, :credentials_header
8
-
9
- validates :server, required: true, is_not_blank: true, url: true
10
- validates :namespace, required: true, is_not_blank: true
11
-
12
- def initialize(params)
6
+ attr_reader :connection, :server, :namespace, :credentials, :credentials_type, :credentials_header
7
+
8
+ validates :server, required: true, is_not_blank: true, url: true
9
+ validates :namespace, required: true, is_not_blank: true
10
+
11
+ def self.global_context
12
+ @@current_context ||= Bodhi::Context.new
13
+ end
14
+
15
+ def self.global_context=(context)
16
+ @@current_context = context
17
+ end
18
+
19
+ def initialize(params={})
13
20
  @connection = Faraday.new(url: params[:server]) do |faraday|
14
21
  faraday.request :multipart
15
22
  faraday.request :url_encoded
16
- faraday.adapter :net_http_persistent
17
23
 
24
+ faraday.adapter :net_http_persistent
18
25
  #faraday.adapter Faraday.default_adapter
26
+
27
+ faraday.response :json, :content_type => /\bjson$/
19
28
  #faraday.response :logger
20
29
  end
21
30
  @server = params[:server]
@@ -42,7 +42,7 @@ module Bodhi
42
42
  raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
43
43
  end
44
44
 
45
- JSON.parse(result.body).collect{ |enum| Bodhi::Enumeration.new(enum) }
45
+ result.body.collect{ |enum| Bodhi::Enumeration.new(enum) }
46
46
  end
47
47
 
48
48
  # Returns a Hash of all Bodhi::Enumerations in the cache
@@ -97,7 +97,13 @@ module Bodhi
97
97
  elsif options[:is_email]
98
98
  generator = lambda{ [*0..5].sample.times.collect{ /\p{Alnum}{5,10}@\p{Alnum}{5,10}\.\p{Alnum}{2,3}/i.random_example } }
99
99
  elsif options[:matches]
100
- generator = lambda{ [*0..5].sample.times.collect{ Regexp.new(options[:matches]).random_example } }
100
+ generator = lambda do
101
+ begin
102
+ [*0..5].sample.times.collect{ Regexp.new(options[:matches]).random_example }
103
+ rescue
104
+ nil
105
+ end
106
+ end
101
107
  elsif options[:length]
102
108
  min = JSON.parse(options[:length]).first
103
109
  max = JSON.parse(options[:length]).last
@@ -111,7 +117,13 @@ module Bodhi
111
117
  elsif options[:is_email]
112
118
  generator = lambda{ /\p{Alnum}{5,10}@\p{Alnum}{5,10}\.\p{Alnum}{2,3}/i.random_example }
113
119
  elsif options[:matches]
114
- generator = lambda{ Regexp.new(options[:matches]).random_example }
120
+ generator = lambda do
121
+ begin
122
+ Regexp.new(options[:matches]).random_example
123
+ rescue
124
+ nil
125
+ end
126
+ end
115
127
  elsif options[:length]
116
128
  min = JSON.parse(options[:length]).first
117
129
  max = JSON.parse(options[:length]).last
@@ -102,7 +102,7 @@ module Bodhi
102
102
  raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
103
103
  end
104
104
 
105
- profile = Bodhi::Profile.new(JSON.parse(result.body))
105
+ profile = Bodhi::Profile.new(result.body)
106
106
  profile.bodhi_context = context
107
107
  profile
108
108
  end
@@ -90,8 +90,7 @@ module Bodhi
90
90
  raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
91
91
  end
92
92
 
93
- resources = JSON.parse(result.body)
94
- resources.map{ |attributes| klass.factory.build(context, attributes) }
93
+ result.body.map{ |attributes| klass.new(attributes) }
95
94
  end
96
95
 
97
96
  def first
@@ -112,8 +111,7 @@ module Bodhi
112
111
  raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
113
112
  end
114
113
 
115
- resources = JSON.parse(result.body)
116
- resources.map{ |attributes| klass.factory.build(context, attributes) }.first
114
+ result.body.map{ |attributes| klass.new(attributes) }.first
117
115
  end
118
116
 
119
117
  def last
@@ -134,8 +132,7 @@ module Bodhi
134
132
  raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
135
133
  end
136
134
 
137
- resources = JSON.parse(result.body)
138
- resources.map{ |attributes| klass.factory.build(context, attributes) }.last
135
+ result.body.map{ |attributes| klass.new(attributes) }.last
139
136
  end
140
137
 
141
138
  def from(context)
@@ -23,7 +23,11 @@ module Bodhi
23
23
  end
24
24
 
25
25
  # Counts all of the Resources records and returns the result
26
- def count(context)
26
+ def count(context=nil)
27
+ if context.nil?
28
+ context = Bodhi::Context.global_context
29
+ end
30
+
27
31
  if context.invalid?
28
32
  raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
29
33
  end
@@ -37,7 +41,22 @@ module Bodhi
37
41
  raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
38
42
  end
39
43
 
40
- JSON.parse(result.body)
44
+ result.body
45
+ end
46
+
47
+ def create!(params, context=nil)
48
+ if context.nil?
49
+ context = Bodhi::Context.global_context
50
+ end
51
+
52
+ if context.invalid?
53
+ raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
54
+ end
55
+
56
+ record = self.new(params)
57
+ record.bodhi_context = context
58
+ record.save!
59
+ return record
41
60
  end
42
61
 
43
62
  # Returns a single resource from the Bodhi Cloud that matches the given +id+
@@ -45,7 +64,11 @@ module Bodhi
45
64
  # context = Bodhi::Context.new
46
65
  # id = Resource.factory.create(context).sys_id
47
66
  # obj = Resource.find(context, id)
48
- def find(context, id)
67
+ def find(id, context=nil)
68
+ if context.nil?
69
+ context = Bodhi::Context.global_context
70
+ end
71
+
49
72
  if context.invalid?
50
73
  raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
51
74
  end
@@ -63,7 +86,7 @@ module Bodhi
63
86
  raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
64
87
  end
65
88
 
66
- record = Object.const_get(name).new(JSON.parse(result.body))
89
+ record = Object.const_get(name).new(result.body)
67
90
  record.bodhi_context = context
68
91
  record
69
92
  end
@@ -72,7 +95,11 @@ module Bodhi
72
95
  #
73
96
  # context = Bodhi::Context.new
74
97
  # Resource.find_all(context) # => [#<Resource:0x007fbff403e808>, #<Resource:0x007fbff403e808>, ...]
75
- def find_all(context)
98
+ def find_all(context=nil)
99
+ if context.nil?
100
+ context = Bodhi::Context.global_context
101
+ end
102
+
76
103
  if context.invalid?
77
104
  raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
78
105
  end
@@ -91,11 +118,12 @@ module Bodhi
91
118
  end
92
119
 
93
120
  page += 1
94
- records << JSON.parse(result.body)
121
+ records << result.body
95
122
  end while records.size == 100
96
123
 
97
124
  records.flatten.collect{ |record| Object.const_get(name).new(record) }
98
125
  end
126
+ alias :all :find_all
99
127
 
100
128
  # Aggregates the given resource based on the supplied +pipeline+
101
129
  #
@@ -119,7 +147,7 @@ module Bodhi
119
147
  raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
120
148
  end
121
149
 
122
- JSON.parse(result.body)
150
+ result.body
123
151
  end
124
152
 
125
153
  # Returns a Bodhi::Query object for quering the given Resource
@@ -156,6 +184,28 @@ module Bodhi
156
184
  module InstanceMethods
157
185
  def id; @sys_id; end
158
186
  def persisted?; !@sys_id.nil?; end
187
+ def new_record?; @sys_id.nil?; end
188
+
189
+ def initialize(params={})
190
+ params.each do |param_key, param_value|
191
+ if param_value.is_a? Hash
192
+ type_validator = self.class.validators[param_key.to_sym].find{ |validator| validator.is_a? Bodhi::TypeValidator }
193
+ if Object.const_defined?(type_validator.type)
194
+ klass = Object.const_get(type_validator.type)
195
+ if klass.ancestors.include?(Bodhi::Resource)
196
+ object = klass.new(param_value)
197
+ send("#{param_key}=", object)
198
+ else
199
+ send("#{param_key}=", param_value)
200
+ end
201
+ else
202
+ send("#{param_key}=", param_value)
203
+ end
204
+ else
205
+ send("#{param_key}=", param_value)
206
+ end
207
+ end
208
+ end
159
209
 
160
210
  # Returns a Hash of the Objects form attributes
161
211
  #
@@ -203,10 +253,18 @@ module Bodhi
203
253
  # obj.save # => true
204
254
  # obj.persisted? # => true
205
255
  def save
206
- if self.invalid?
256
+ if invalid?
207
257
  return false
208
258
  end
209
259
 
260
+ if bodhi_context.nil?
261
+ @bodhi_context = Bodhi::Context.global_context
262
+ end
263
+
264
+ if bodhi_context.invalid?
265
+ raise Bodhi::ContextErrors.new(bodhi_context.errors.messages), bodhi_context.errors.to_a.to_s
266
+ end
267
+
210
268
  result = bodhi_context.connection.post do |request|
211
269
  request.url "/#{bodhi_context.namespace}/resources/#{self.class}"
212
270
  request.headers['Content-Type'] = 'application/json'
@@ -116,7 +116,7 @@ module Bodhi
116
116
  raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
117
117
  end
118
118
 
119
- type = Bodhi::Type.new(JSON.parse(result.body))
119
+ type = Bodhi::Type.new(result.body)
120
120
  type.bodhi_context = context
121
121
  type
122
122
  end
@@ -146,7 +146,7 @@ module Bodhi
146
146
  end
147
147
 
148
148
  page += 1
149
- records = JSON.parse(result.body)
149
+ records = result.body
150
150
  all_records << records
151
151
  end while records.size == 100
152
152
 
@@ -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(JSON.parse(result.body))
113
+ user = Bodhi::User.new(result.body)
114
114
  user.bodhi_context = context
115
115
  user
116
116
  end
@@ -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
- JSON.parse(result.body)
136
+ result.body
137
137
  end
138
138
  end
139
139
  end
@@ -3,7 +3,7 @@ module Bodhi
3
3
  attr_reader :value
4
4
 
5
5
  def initialize(value)
6
- @value = JSON.parse(value)
6
+ @value = value
7
7
  end
8
8
 
9
9
  def validate(record, attribute, value)
data/lib/bodhi-slam.rb CHANGED
@@ -1,5 +1,7 @@
1
- require "faraday"
1
+ require 'faraday'
2
+ require 'faraday_middleware'
2
3
  require 'net/http/persistent'
4
+
3
5
  require "json"
4
6
  require "time"
5
7
  require "SecureRandom"
metadata CHANGED
@@ -1,43 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bodhi-slam
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - willdavis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-02 00:00:00.000000000 Z
11
+ date: 2015-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: net-http-persistent
14
+ name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.9'
19
+ version: '0.9'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.9'
26
+ version: '0.9'
27
27
  - !ruby/object:Gem::Dependency
28
- name: faraday
28
+ name: faraday_middleware
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.9'
33
+ version: '0.10'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.9'
40
+ version: '0.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: net-http-persistent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.9'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.9'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: json
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -189,7 +203,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
203
  version: '0'
190
204
  requirements: []
191
205
  rubyforge_project:
192
- rubygems_version: 2.2.2
206
+ rubygems_version: 2.4.5
193
207
  signing_key:
194
208
  specification_version: 4
195
209
  summary: Ruby bindings for the Bodhi API & factories for random data generation