spectre-core 1.11.0 → 1.12.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,29 +1,39 @@
1
- module Spectre
2
- module Diagnostic
3
- module Stopwatch
4
- @@duration = 0.0
5
-
6
- class << self
7
- def start_watch
8
- @@start_time = Time.now
9
- end
10
-
11
- def stop_watch
12
- @@end_time = Time.now
13
- end
14
-
15
- def measure
16
- start_watch
17
- yield
18
- stop_watch
19
- end
20
-
21
- def duration
22
- @@end_time - @@start_time
23
- end
24
- end
25
-
26
- Spectre.delegate :start_watch, :stop_watch, :duration, :measure, to: Stopwatch
27
- end
28
- end
29
- end
1
+ require_relative '../spectre'
2
+
3
+ module Spectre
4
+ module Diagnostic
5
+ module Stopwatch
6
+ @@duration = 0.0
7
+
8
+ class << self
9
+ def start_watch
10
+ @@start_time = Time.now
11
+ end
12
+
13
+ def stop_watch
14
+ @@end_time = Time.now
15
+ end
16
+
17
+ def measure
18
+ start_watch
19
+ yield
20
+ stop_watch
21
+ end
22
+
23
+ def duration
24
+ @@end_time - @@start_time
25
+ end
26
+
27
+ def started_at
28
+ @@start_time
29
+ end
30
+
31
+ def finished_at
32
+ @@end_time
33
+ end
34
+ end
35
+
36
+ Spectre.delegate :start_watch, :stop_watch, :duration, :measure, to: self
37
+ end
38
+ end
39
+ end
@@ -1,26 +1,30 @@
1
- require 'ostruct'
2
-
3
- def to_recursive_ostruct(hash)
4
- OpenStruct.new(hash.each_with_object({}) do |(key, val), memo|
5
- memo[key] = val.is_a?(Hash) ? to_recursive_ostruct(val) : val
6
- end)
7
- end
8
-
9
- module Spectre
10
- module Environment
11
- class << self
12
- @@environment = OpenStruct.new
13
-
14
- def env
15
- @@environment
16
- end
17
- end
18
-
19
- Spectre.register do |config|
20
- @@environment = to_recursive_ostruct(config)
21
- @@environment.freeze
22
- end
23
-
24
- Spectre.delegate :env, to: Environment
25
- end
26
- end
1
+ require_relative '../spectre'
2
+
3
+ require 'ostruct'
4
+
5
+ def to_recursive_ostruct(hash)
6
+ OpenStruct.new(
7
+ hash.each_with_object({}) do |(key, val), memo|
8
+ memo[key] = val.is_a?(Hash) ? to_recursive_ostruct(val) : val
9
+ end
10
+ )
11
+ end
12
+
13
+ module Spectre
14
+ module Environment
15
+ class << self
16
+ @@environment = OpenStruct.new
17
+
18
+ def env
19
+ @@environment
20
+ end
21
+ end
22
+
23
+ Spectre.register do |config|
24
+ @@environment = to_recursive_ostruct(config)
25
+ @@environment.freeze
26
+ end
27
+
28
+ Spectre.delegate :env, to: self
29
+ end
30
+ end
@@ -1,134 +1,133 @@
1
- require 'securerandom'
2
- require 'json'
3
- require 'date'
4
- require 'ostruct'
5
- require 'jsonpath'
6
-
7
- class ::String
8
- def as_json
9
- JSON.parse(self, object_class: OpenStruct)
10
- end
11
-
12
- def as_date
13
- DateTime.parse(self)
14
- end
15
-
16
- def as_timestamp
17
- DateTime.parse(self).to_time.to_i
18
- end
19
-
20
- def with mapping
21
- return self unless mapping and mapping.is_a? Hash
22
-
23
- new_string = self
24
-
25
- mapping.each do |key, value|
26
- new_string = new_string.gsub('#{' + key.to_s + '}', value.to_s)
27
- end
28
-
29
- new_string
30
- end
31
-
32
- def trim size = 50
33
- if (self.length + 3) > size
34
- return self[0..size-4] + '...'
35
- end
36
-
37
- self
38
- end
39
-
40
- def pick path
41
- raise ArgumentError.new("`path' must not be nil or empty") if path.nil? or path.empty?
42
-
43
- begin
44
- JsonPath.on(self, path)
45
-
46
- rescue MultiJson::ParseError
47
- # do nothing and return nil
48
- end
49
- end
50
-
51
- # File helpers
52
-
53
- def content with: nil
54
- fail "'#{self}' is not a file path, or the file does not exist." if !File.exists? self
55
- file_content = File.read(self)
56
-
57
- if with
58
- file_content.with(with)
59
- else
60
- file_content
61
- end
62
- end
63
-
64
- def file_size
65
- fail "'#{self}' is not a file path, or the file does not exist." if !File.exists? self
66
- File.size(self)
67
- end
68
-
69
- def exists?
70
- File.exists? self
71
- end
72
-
73
- def remove!
74
- fail "'#{self}' is not a file path, or the file does not exist." if !File.exists? self
75
- File.delete self
76
- end
77
- end
78
-
79
-
80
- class ::OpenStruct
81
- def to_json *args, **kwargs
82
- self.to_h.inject({}) { |memo, (k,v)| memo[k] = v.is_a?(OpenStruct) ? v.to_h : v; memo }.to_json(*args, **kwargs)
83
- end
84
-
85
- def pick path
86
- raise ArgumentError.new("`path' must not be nil or empty") if path.nil? or path.empty?
87
-
88
- JsonPath.on(self, path)
89
- end
90
-
91
- def default_to! defaults
92
- defaults.each_key do |key|
93
- if not self[key] != nil
94
- self[key] = defaults[key]
95
- end
96
- end
97
- end
98
-
99
- alias :defaults_to! :default_to!
100
- end
101
-
102
-
103
- class ::Hash
104
- def symbolize_keys
105
- self.inject({}) { |memo, (k,v)| memo[k.to_sym] = v; memo }
106
- end
107
-
108
- def default_to! defaults
109
- defaults.each_key do |key|
110
- if not self[key] != nil
111
- self[key] = defaults[key]
112
- end
113
- end
114
- end
115
-
116
- alias :defaults_to! :default_to!
117
- end
118
-
119
-
120
- class ::Array
121
- def last
122
- self[-1]
123
- end
124
- end
125
-
126
-
127
- def uuid length = 5
128
- SecureRandom.uuid().gsub('-', '')[0..length-1]
129
- end
130
-
131
-
132
- def now
133
- Time.now
134
- end
1
+ require_relative '../spectre'
2
+
3
+ require 'securerandom'
4
+ require 'json'
5
+ require 'date'
6
+ require 'ostruct'
7
+ require 'jsonpath'
8
+
9
+ class ::String
10
+ def as_json
11
+ JSON.parse(self, object_class: OpenStruct)
12
+ end
13
+
14
+ def as_date
15
+ DateTime.parse(self)
16
+ end
17
+
18
+ def as_timestamp
19
+ DateTime.parse(self).to_time.to_i
20
+ end
21
+
22
+ def with mapping
23
+ return self unless mapping and mapping.is_a? Hash
24
+
25
+ new_string = self
26
+
27
+ mapping.each do |key, value|
28
+ new_string = new_string.gsub('#{' + key.to_s + '}', value.to_s)
29
+ end
30
+
31
+ new_string
32
+ end
33
+
34
+ def trim size = 50
35
+ if (self.length + 3) > size
36
+ return self[0..size-4] + '...'
37
+ end
38
+
39
+ self
40
+ end
41
+
42
+ def pick path
43
+ raise ArgumentError.new("`path' must not be nil or empty") if path.nil? or path.empty?
44
+
45
+ begin
46
+ JsonPath.on(self, path)
47
+ rescue MultiJson::ParseError
48
+ # do nothing and return nil
49
+ end
50
+ end
51
+
52
+ # File helpers
53
+
54
+ def content with: nil
55
+ fail "'#{self}' is not a file path, or the file does not exist." unless File.exists? self
56
+
57
+ file_content = File.read(self)
58
+
59
+ if with
60
+ file_content.with(with)
61
+ else
62
+ file_content
63
+ end
64
+ end
65
+
66
+ def file_size
67
+ fail "'#{self}' is not a file path, or the file does not exist." unless File.exists? self
68
+
69
+ File.size(self)
70
+ end
71
+
72
+ def exists?
73
+ File.exists? self
74
+ end
75
+
76
+ def remove!
77
+ fail "'#{self}' is not a file path, or the file does not exist." unless File.exists? self
78
+
79
+ File.delete self
80
+ end
81
+ end
82
+
83
+ class ::OpenStruct
84
+ def to_json *args, **kwargs
85
+ self.to_h.inject({}) { |memo, (k,v)| memo[k] = v.is_a?(OpenStruct) ? v.to_h : v; memo }.to_json(*args, **kwargs)
86
+ end
87
+
88
+ def pick path
89
+ raise ArgumentError.new("`path' must not be nil or empty") if path.nil? or path.empty?
90
+
91
+ JsonPath.on(self, path)
92
+ end
93
+
94
+ def default_to! defaults
95
+ defaults.each_key do |key|
96
+ unless self[key] != nil
97
+ self[key] = defaults[key]
98
+ end
99
+ end
100
+ end
101
+
102
+ alias :defaults_to! :default_to!
103
+ end
104
+
105
+ class ::Hash
106
+ def symbolize_keys
107
+ self.inject({}) { |memo, (k,v)| memo[k.to_sym] = v; memo }
108
+ end
109
+
110
+ def default_to! defaults
111
+ defaults.each_key do |key|
112
+ unless self[key] != nil
113
+ self[key] = defaults[key]
114
+ end
115
+ end
116
+ end
117
+
118
+ alias :defaults_to! :default_to!
119
+ end
120
+
121
+ class ::Array
122
+ def last_element
123
+ self[-1]
124
+ end
125
+ end
126
+
127
+ def uuid length = 5
128
+ SecureRandom.uuid().gsub('-', '')[0..length-1]
129
+ end
130
+
131
+ def now
132
+ Time.now
133
+ end
@@ -1,7 +1,9 @@
1
+ require_relative '../http'
2
+
1
3
  module Spectre::Http
2
4
  class SpectreHttpRequest
3
5
  def basic_auth username, password
4
- @__req['basic_auth'] = {} if not @__req.key? 'basic_auth'
6
+ @__req['basic_auth'] = {} unless @__req.key? 'basic_auth'
5
7
 
6
8
  @__req['basic_auth']['username'] = username
7
9
  @__req['basic_auth']['password'] = password
@@ -11,8 +13,9 @@ module Spectre::Http
11
13
  end
12
14
 
13
15
  module BasicAuth
14
- def self.on_req http, net_req, req
16
+ def self.on_req _http, net_req, req
15
17
  return unless req.key? 'basic_auth' and req['auth'] == 'basic_auth'
18
+
16
19
  basic_auth_cfg = req['basic_auth']
17
20
  net_req.basic_auth(basic_auth_cfg['username'], basic_auth_cfg['password'])
18
21
  end
@@ -1,98 +1,101 @@
1
- class HttpRequest
2
- def keystone url, username, password, project, domain, cert
3
- @__req['keystone'] = {} if not @__req.key? 'keystone'
4
-
5
- @__req['keystone']['url'] = url
6
- @__req['keystone']['username'] = username
7
- @__req['keystone']['password'] = password
8
- @__req['keystone']['project'] = project
9
- @__req['keystone']['domain'] = domain
10
- @__req['keystone']['cert'] = cert
11
-
12
- @__req.config['auth'] = 'keystone'
1
+ require_relative '../http'
2
+
3
+ module Spectre::Http
4
+ class SpectreHttpRequest
5
+ def keystone url, username, password, project, domain, cert=nil
6
+ @__req['keystone'] = {} unless @__req.key? 'keystone'
7
+
8
+ @__req['keystone']['url'] = url
9
+ @__req['keystone']['username'] = username
10
+ @__req['keystone']['password'] = password
11
+ @__req['keystone']['project'] = project
12
+ @__req['keystone']['domain'] = domain
13
+ @__req['keystone']['cert'] = cert
14
+
15
+ @__req['auth'] = 'keystone'
16
+ end
13
17
  end
14
- end
15
18
 
19
+ module Keystone
20
+ @@cache = {}
16
21
 
17
- module Spectre::Http::Keystone
18
- @@cache = {}
22
+ def self.on_req _http, net_req, req
23
+ return unless req.key? 'keystone' and req['auth'] == 'keystone'
19
24
 
20
- def self.on_req http, net_req, req
21
- return unless req.key? 'keystone' and req['auth'] == 'keystone'
25
+ keystone_cfg = req['keystone']
22
26
 
23
- keystone_cfg = req['keystone']
27
+ if @@cache.key? keystone_cfg
28
+ token = @@cache[keystone_cfg]
29
+ else
30
+ token, _ = authenticate(
31
+ keystone_cfg['url'],
32
+ keystone_cfg['username'],
33
+ keystone_cfg['password'],
34
+ keystone_cfg['project'],
35
+ keystone_cfg['domain'],
36
+ keystone_cfg['cert'],
37
+ )
24
38
 
25
- if @@cache.key? keystone_cfg
26
- token = @@cache[keystone_cfg]
27
- else
28
- token, _ = authenticate(
29
- keystone_cfg['url'],
30
- keystone_cfg['username'],
31
- keystone_cfg['password'],
32
- keystone_cfg['project'],
33
- keystone_cfg['domain'],
34
- keystone_cfg['cert'],
35
- )
39
+ @@cache[keystone_cfg] = token
40
+ end
36
41
 
37
- @@cache[keystone_cfg] = token
42
+ net_req['X-Auth-Token'] = token
38
43
  end
39
44
 
40
- net_req['X-Auth-Token'] = token
41
- end
42
-
43
- private
44
-
45
- def self.authenticate keystone_url, username, password, project, domain, cert
46
- auth_data = {
47
- auth: {
48
- identity: {
49
- methods: ['password'],
50
- password: {
51
- user: {
52
- name: username,
53
- password: password,
54
- domain: {
55
- name: domain,
45
+ private
46
+
47
+ def self.authenticate keystone_url, username, password, project, domain, cert
48
+ auth_data = {
49
+ auth: {
50
+ identity: {
51
+ methods: ['password'],
52
+ password: {
53
+ user: {
54
+ name: username,
55
+ password: password,
56
+ domain: {
57
+ name: domain,
58
+ },
56
59
  },
57
60
  },
58
61
  },
59
- },
60
- scope: {
61
- project: {
62
- name: project,
63
- domain: {
64
- name: domain,
62
+ scope: {
63
+ project: {
64
+ name: project,
65
+ domain: {
66
+ name: domain,
67
+ },
65
68
  },
66
69
  },
67
70
  },
68
- },
69
- }
71
+ }
70
72
 
71
- keystone_url = keystone_url + '/' if !keystone_url.end_with? '/'
73
+ keystone_url = keystone_url + '/' unless keystone_url.end_with? '/'
72
74
 
73
- base_uri = URI(keystone_url)
74
- uri = URI.join(base_uri, 'auth/tokens?nocatalog=true')
75
+ base_uri = URI(keystone_url)
76
+ uri = URI.join(base_uri, 'auth/tokens?nocatalog=true')
75
77
 
76
- http = Net::HTTP.new(base_uri.host, base_uri.port)
78
+ http = Net::HTTP.new(base_uri.host, base_uri.port)
77
79
 
78
- if cert
79
- http.use_ssl = true
80
- http.ca_file = cert
81
- end
80
+ if cert
81
+ http.use_ssl = true
82
+ http.ca_file = cert
83
+ end
82
84
 
83
- req = Net::HTTP::Post.new(uri)
84
- req.body = JSON.pretty_generate(auth_data)
85
- req.content_type = 'application/json'
85
+ req = Net::HTTP::Post.new(uri)
86
+ req.body = JSON.pretty_generate(auth_data)
87
+ req.content_type = 'application/json'
86
88
 
87
- res = http.request(req)
89
+ res = http.request(req)
88
90
 
89
- raise "error while authenticating: #{res.code} #{res.message}\n#{res.body}" if res.code != '201'
91
+ raise "error while authenticating: #{res.code} #{res.message}\n#{res.body}" if res.code != '201'
90
92
 
91
- [
92
- res['X-Subject-Token'],
93
- JSON.parse(res.body),
94
- ]
95
- end
93
+ [
94
+ res['X-Subject-Token'],
95
+ JSON.parse(res.body),
96
+ ]
97
+ end
96
98
 
97
- Spectre::Http.register(self)
99
+ Spectre::Http.register(self)
100
+ end
98
101
  end